Commit 0d365048 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Correction de la Factory ConsoleControllerFactory et ajout de la commande run-sql-query.

parent bac272fb
Loading
Loading
Loading
Loading
Loading
+5
Changes for CHANGELOG.md: 5 added lines, 0 removed lines.
Original line number Diff line number Diff line
CHANGELOG
=========

3.1.14
------
- Nouvelle ligne de commande 'run-sql-query' pour exécuter une requête SQL.
- Correction de ConsoleControllerFactory non migrée en ZF3.

3.1.13
------
- AjaxPopover : possibilité de désactiver l'interception du submit ajax en ajoutant la classe 'disable-ajax-submit' sur le formulaire.
+7
Changes for Module.php: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -238,6 +238,13 @@ class Module implements
            ['--path',       "Requis. Chemin vers le script SQL à exécuter."],
            ['--logfile',    "Facultatif. Chemin du fichier des logs d'exécution du script. Par défaut, il est généré."],
            ['--connection', "Facultatif. Identifiant de la connexion Doctrine. Par défaut : 'orm_default'."],

            // command
            'run-sql-query --sql= [--logfile=] [--connection=]' => "Exécuter une requête SQL",
            // parameters
            ['--sql',        "Requis. Requête SQL à exécuter. Ex: \"begin DBMS_MVIEW.REFRESH('MV_RECHERCHE_THESE'); end;\"."],
            ['--logfile',    "Facultatif. Chemin du fichier des logs d'exécution. Par défaut, il est généré."],
            ['--connection', "Facultatif. Identifiant de la connexion Doctrine. Par défaut : 'orm_default'."],
        ];
    }
}
+11 −1
Changes for config/module.config.php: 11 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -251,7 +251,7 @@ return [
    'console' => [
        'router' => [
            'routes' => [
                'run-sql' => [
                'run-sql-script' => [
                    'type' => Simple::class,
                    'options' => [
                        'route'    => 'run-sql-script --path= [--logfile=] [--connection=]',
@@ -261,6 +261,16 @@ return [
                        ],
                    ],
                ],
                'run-sql-query' => [
                    'type' => Simple::class,
                    'options' => [
                        'route'    => 'run-sql-query --sql= [--logfile=] [--connection=]',
                        'defaults' => [
                            'controller' => ConsoleController::class,
                            'action'     => 'runSQLQuery',
                        ],
                    ],
                ],
            ],
        ],
        'view_manager' => [
+76 −27
Changes for src/UnicaenApp/Controller/ConsoleController.php: 76 added lines, 27 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace UnicaenApp\Controller;

use Doctrine\DBAL\Connection;
use Exception;
use Interop\Container\ContainerInterface;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\SQL\RunSQLServiceAwareTrait;
use Zend\Log\Filter\Priority;
@@ -11,60 +12,101 @@ use Zend\Log\Formatter\Simple;
use Zend\Log\Logger;
use Zend\Log\LoggerAwareTrait;
use Zend\Log\Writer\Stream;
use Zend\Mvc\Controller\AbstractConsoleController;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Mvc\Console\Controller\AbstractConsoleController;


class ConsoleController extends AbstractConsoleController
{
    use ServiceLocatorAwareTrait;
    use LoggerAwareTrait;
    use RunSQLServiceAwareTrait;

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var Connection
     */
    protected $connection;

    /**
     * @var Logger
     */
    protected $logger;

    /**
     * @param ContainerInterface $container
     * @return self
     */
    public function setContainer(ContainerInterface $container): self
    {
        $this->container = $container;
        return $this;
    }

    /**
     * @throws Exception
     */
    public function runSQLScriptAction()
    public function runSQLQueryAction()
    {
        $path = $this->params('path');
        $sql = $this->params('sql');
        $connection = $this->params('connection', 'orm_default');
        $logFilepath = $this->params('logfile');
        $serviceName = "doctrine.connection.$connection";

        $request = implode(' ', $this->getRequest()->getContent());
        $this->createLogger();
        $this->logger->info("### Exécution de commandes SQL ###");
        $this->logger->info(date_format(date_create(), 'd/m/Y H:i:s'));

        $logger = $this->createLogger();
        $this->initConnection($connection);

        $logger->info("### Exécution de scripts SQL ###");
        $logger->info(date_format(date_create(), 'd/m/Y H:i:s'));
        $this->runSQLService->setLogger($this->logger);
        $result = $this->runSQLService->runSQLQuery($sql, $this->connection, $logFilepath);

        if (! $this->serviceLocator->has($serviceName)) {
            throw new RuntimeException("Connection Doctrine introuvable : $serviceName");
        if ($result->isSuccess()) {
            $this->logger->info("Exécution terminée avec succès.");
        } else {
            $this->logger->info("OUPS, UNE ERREUR EST SURVENUE !");
        }

        $this->logger->info("Durée : " . $result->getDurationInSec() . " sec");

        if (! $result->isSuccess()) {
            exit(1);
        }
    }
        /** @var Connection $conn */
        $conn = $this->serviceLocator->get($serviceName);

        $logger->info("Connexion : '$serviceName'");
    /**
     * @throws Exception
     */
    public function runSQLScriptAction()
    {
        $path = $this->params('path');
        $connection = $this->params('connection', 'orm_default');
        $logFilepath = $this->params('logfile');

        $this->createLogger();
        $this->logger->info("### Exécution de scripts SQL ###");
        $this->logger->info(date_format(date_create(), 'd/m/Y H:i:s'));

        $this->initConnection($connection);

        $this->runSQLService->setLogger($logger);
        $result = $this->runSQLService->runSQLScript($path, $conn, $logFilepath);
        $this->runSQLService->setLogger($this->logger);
        $result = $this->runSQLService->runSQLScript($path, $this->connection, $logFilepath);

        if ($result->isSuccess()) {
            $logger->info("Exécution terminée avec succès.");
            $this->logger->info("Exécution terminée avec succès.");
        } else {
            $logger->info("OUPS, UNE ERREUR EST SURVENUE !");
            $this->logger->info("OUPS, UNE ERREUR EST SURVENUE !");
        }

        $logger->info("Durée : " . $result->getDurationInSec() . " sec");
        $this->logger->info("Durée : " . $result->getDurationInSec() . " sec");

        if (! $result->isSuccess()) {
            exit(1);
        }
    }

    /**
     * @return Logger
     */
    private function createLogger()
    {
        $filter = new Priority(Logger::INFO);
@@ -76,10 +118,17 @@ class ConsoleController extends AbstractConsoleController
        $writer->addFilter($filter);
        $writer->setFormatter($formatter);

        /** @var Logger $logger */
        $logger = new Logger();
        $logger->addWriter($writer);
        $this->logger = new Logger();
        $this->logger->addWriter($writer);
    }

    protected function initConnection(string $name)
    {
        $serviceName = "doctrine.connection.$name";
        if (! $this->container->has($serviceName)) {
            throw new RuntimeException("Connection Doctrine introuvable : $serviceName");
        }

        return $logger;
        $this->connection = $this->container->get($serviceName);
    }
}
+10 −7
Changes for src/UnicaenApp/Controller/ConsoleControllerFactory.php: 10 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -2,22 +2,26 @@

namespace UnicaenApp\Controller;

use Interop\Container\ContainerInterface;
use UnicaenApp\Service\SQL\RunSQLService;
use Zend\Log\Logger;
use Zend\Log\LoggerInterface;
use Zend\Log\Writer\Noop;
use Zend\Mvc\Controller\ControllerManager;
use Zend\ServiceManager\Factory\FactoryInterface;

class ConsoleControllerFactory
class ConsoleControllerFactory implements FactoryInterface
{
    public function __invoke(ControllerManager $sl)
    /**
     * @inheritDoc
     */
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        /** @var \UnicaenApp\Service\SQL\RunSQLService $runSQLService */
        $runSQLService = $sl->getServiceLocator()->get(RunSQLService::class);
        /** @var RunSQLService $runSQLService */
        $runSQLService = $container->get(RunSQLService::class);

        $controller = new ConsoleController();
        $controller->setLogger($this->createLogger());
        $controller->setServiceLocator($sl->getServiceLocator());
        $controller->setContainer($container);
        $controller->setRunSQLService($runSQLService);

        return $controller;
@@ -28,7 +32,6 @@ class ConsoleControllerFactory
     */
    private function createLogger()
    {
        /** @var Logger $logger */
        $logger = new Logger();
        $logger->addWriter(new Noop());

Loading