Commit ac68ea60 authored by Laurent Lécluse's avatar Laurent Lécluse
Browse files

Suppression de la console

Suppression de la dépendance à Laminas\Console et Laminas\Mvc\Console
parent 3765a9de
Pipeline #12020 failed with stages
in 40 seconds
......@@ -9,8 +9,6 @@ use UnicaenApp\Mvc\Listener\MaintenanceListener;
use UnicaenApp\Mvc\Listener\ModalListener;
use UnicaenApp\Mvc\View\Http\ExceptionStrategy;
use UnicaenApp\Options\ModuleOptions;
use Laminas\Console\Adapter\AdapterInterface as Console;
use Laminas\Console\Request as ConsoleRequest;
use Laminas\EventManager\EventInterface;
use Laminas\Http\Request as HttpRequest;
use Laminas\Mvc\I18n\Translator;
......@@ -19,8 +17,8 @@ use Laminas\ModuleManager\Feature\BootstrapListenerInterface;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;
use Laminas\ModuleManager\Feature\ControllerPluginProviderInterface;
use Laminas\ModuleManager\Feature\ServiceProviderInterface;
use Laminas\Mvc\Console\View\ExceptionStrategy as ConsoleExceptionStrategy;
use Laminas\Mvc\View\Http\ExceptionStrategy as HttpExceptionStrategy;
use UnicaenApp\Mvc\View\Console\ExceptionStrategy as ConsoleExceptionStrategy;
use UnicaenApp\Mvc\View\Http\ExceptionStrategy as HttpExceptionStrategy;
use Laminas\ServiceManager\ServiceManager;
use Laminas\Session\Container;
use Laminas\Session\SessionManager;
......@@ -175,7 +173,7 @@ class Module implements
*/
public function appendSessionRefreshJs(EventInterface $e)
{
if ($e->getRequest() instanceof ConsoleRequest) {
if (Util::isConsole()) {
return;
}
......@@ -228,23 +226,4 @@ class Module implements
{
return [];
}
public function getConsoleUsage(Console $console)
{
return [
// command
'run-sql-script --path= [--logfile=] [--connection=]' => "Exécuter un script SQL",
// parameters
['--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'."],
];
}
}
......@@ -2,7 +2,7 @@
"name": "unicaen/app",
"description": "Module de base des applications unicaen",
"require": {
"php": ">=7.4",
"php": ">=7.4 || >=8.0",
"ext-bcmath": "*",
"ext-intl": "*",
"ext-ldap": "*",
......@@ -12,7 +12,6 @@
"doctrine/doctrine-laminas-hydrator": "^2.0",
"doctrine/doctrine-orm-module": "^4.0",
"laminas/laminas-config": "^3.2",
"laminas/laminas-console": "^2.8",
"laminas/laminas-eventmanager": "^3.2",
"laminas/laminas-form": "^2.17",
"laminas/laminas-http": "^2.10",
......@@ -23,7 +22,6 @@
"laminas/laminas-log": "^2.10",
"laminas/laminas-mail": "^2.10",
"laminas/laminas-mvc": "^3.1",
"laminas/laminas-mvc-console": "^1.2",
"laminas/laminas-mvc-i18n": "^1.1",
"laminas/laminas-mvc-plugin-fileprg": "^1.2",
"laminas/laminas-mvc-plugin-flashmessenger": "^1.1",
......
......@@ -3,8 +3,6 @@
namespace UnicaenApp;
use UnicaenApp\Controller\CacheControllerFactory;
use UnicaenApp\Controller\ConsoleController;
use UnicaenApp\Controller\ConsoleControllerFactory;
use UnicaenApp\Controller\InstadiaControllerFactory;
use UnicaenApp\Form\View\Helper\FormControlGroup;
use UnicaenApp\Form\View\Helper\FormControlGroupFactory;
......@@ -46,7 +44,6 @@ use UnicaenApp\View\Helper\Upload\UploaderHelper;
use UnicaenApp\View\Helper\Upload\UploaderHelperFactory;
use UnicaenApp\View\Helper\UserProfileSelect;
use UnicaenApp\View\Helper\UserProfileSelectFactory;
use Laminas\Mvc\Console\Router\Simple;
return [
'router' => [
......@@ -247,36 +244,7 @@ return [
],
],
],
'console' => [
'router' => [
'routes' => [
'run-sql-script' => [
'type' => Simple::class,
'options' => [
'route' => 'run-sql-script --path= [--logfile=] [--connection=]',
'defaults' => [
'controller' => ConsoleController::class,
'action' => 'runSQLScript',
],
],
],
'run-sql-query' => [
'type' => Simple::class,
'options' => [
'route' => 'run-sql-query --sql= [--logfile=] [--connection=]',
'defaults' => [
'controller' => ConsoleController::class,
'action' => 'runSQLQuery',
],
],
],
],
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
],
],
'service_manager' => [
'factories' => [
'translator' => 'Laminas\I18n\Translator\TranslatorServiceFactory',
......@@ -455,7 +423,6 @@ return [
'factories' => [
'UnicaenApp\Controller\Cache' => CacheControllerFactory::class,
'UnicaenApp\Controller\Instadia' => InstadiaControllerFactory::class,
ConsoleController::class => ConsoleControllerFactory::class,
],
],
......
<?php
namespace UnicaenApp\Controller;
use Doctrine\DBAL\Connection;
use Exception;
use Psr\Container\ContainerInterface;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\SQL\RunSQLServiceAwareTrait;
use Laminas\Log\Filter\Priority;
use Laminas\Log\Formatter\Simple;
use Laminas\Log\Logger;
use Laminas\Log\LoggerAwareTrait;
use Laminas\Log\Writer\Stream;
use Laminas\Mvc\Console\Controller\AbstractConsoleController;
class ConsoleController extends AbstractConsoleController
{
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 runSQLQueryAction()
{
$sql = $this->params('sql');
$connection = $this->params('connection', 'orm_default');
$logFilepath = $this->params('logfile');
$this->createLogger();
$this->logger->info("### Exécution de commandes SQL ###");
$this->logger->info(date_format(date_create(), 'd/m/Y H:i:s'));
$this->initConnection($connection);
$this->runSQLService->setLogger($this->logger);
$result = $this->runSQLService->runSQLQuery($sql, $this->connection, $logFilepath);
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);
}
}
/**
* @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($this->logger);
$result = $this->runSQLService->runSQLScript($path, $this->connection, $logFilepath);
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);
}
}
private function createLogger()
{
$filter = new Priority(Logger::INFO);
$format = '%message%'; // '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
$formatter = new Simple($format);
$writer = new Stream('php://output');
$writer->addFilter($filter);
$writer->setFormatter($formatter);
$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");
}
$this->connection = $this->container->get($serviceName);
}
}
<?php
namespace UnicaenApp\Controller;
use Psr\Container\ContainerInterface;
use UnicaenApp\Service\SQL\RunSQLService;
use Laminas\Log\Logger;
use Laminas\Log\LoggerInterface;
use Laminas\Log\Writer\Noop;
use Laminas\ServiceManager\Factory\FactoryInterface;
class ConsoleControllerFactory implements FactoryInterface
{
/**
* @inheritDoc
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** @var RunSQLService $runSQLService */
$runSQLService = $container->get(RunSQLService::class);
$controller = new ConsoleController();
$controller->setLogger($this->createLogger());
$controller->setContainer($container);
$controller->setRunSQLService($runSQLService);
return $controller;
}
/**
* @return LoggerInterface
*/
private function createLogger()
{
$logger = new Logger();
$logger->addWriter(new Noop());
return $logger;
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ use Laminas\Mvc\MvcEvent;
use Laminas\Stdlib\ResponseInterface as Response;
use Laminas\View\Model\ViewModel;
use UnicaenApp\Exception\ExceptionInterface;
use UnicaenApp\Util;
/**
* Stratégie permettant d'afficher proprement un message d'erreur lorsqu'une exception est levée
......@@ -60,7 +61,7 @@ class ExceptionStrategy extends \Laminas\Mvc\View\Http\ExceptionStrategy
}
// Do nothing if the request is console type
if ($e->getRequest() instanceof \Laminas\Console\Request) {
if (Util::isConsole()) {
return;
}
......
......@@ -774,4 +774,16 @@ class Util
return $content;
}
/**
* Détermine si on est en mode console ou non
*
* @return bool
*/
static public function isConsole(): bool
{
return PHP_SAPI == 'cli';
}
}
......@@ -3,9 +3,9 @@
namespace UnicaenApp\View\Helper;
use Psr\Container\ContainerInterface;
use Laminas\Console\Console;
use Laminas\ServiceManager\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;
use UnicaenApp\Util;
/**
* Description of AppLinkFactory
......@@ -22,7 +22,7 @@ class AppLinkFactory implements FactoryInterface
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$sl = $container;
$router = Console::isConsole() ? 'HttpRouter' : 'Router';
$router = Util::isConsole() ? 'HttpRouter' : 'Router';
$match = $sl->get('application')->getMvcEvent()->getRouteMatch();
$helper = new AppLink();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment