From 30ad25778ebc82b702bad7de01e7b45d3fb961cb Mon Sep 17 00:00:00 2001
From: Bertrand GAUTHIER <bertrand.gauthier@unicaen.fr>
Date: Mon, 13 Nov 2023 15:14:48 +0100
Subject: [PATCH] =?UTF-8?q?Suppression=20de=20la=20d=C3=A9pendance=20avec?=
 =?UTF-8?q?=20unicaen/console.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CHANGELOG.md                                  |  1 +
 Module.php                                    | 17 +++++--------
 .../Mvc/View/Http/ExceptionStrategy.php       |  4 +--
 src/UnicaenApp/Util.php                       | 25 +++++++++++++------
 src/UnicaenApp/View/Helper/AppLinkFactory.php |  4 +--
 5 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2694a8b8..e5d97a19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@ CHANGELOG
 -----
 
 - Suppression du ConsoleController utilisé par une seule appli pour lancer une ligne SQL.
+- Suppression de la dépendance avec unicaen/console.
 
 6.0.10
 -----
diff --git a/Module.php b/Module.php
index cb40f402..72d17d42 100644
--- a/Module.php
+++ b/Module.php
@@ -17,13 +17,10 @@ use Laminas\Validator\AbstractValidator;
 use Laminas\View\Helper\Navigation;
 use Laminas\View\HelperPluginManager;
 use Locale;
-use Unicaen\Console\Console;
-use Unicaen\Console\View\ExceptionStrategy as ConsoleExceptionStrategy;
 use UnicaenApp\Controller\Plugin\MessengerPluginFactory;
 use UnicaenApp\Controller\Plugin\Upload\UploaderPluginFactory;
 use UnicaenApp\Mvc\Listener\MaintenanceListener;
 use UnicaenApp\Mvc\Listener\ModalListener;
-use UnicaenApp\Mvc\View\Http\ExceptionStrategy;
 use UnicaenApp\Mvc\View\Http\ExceptionStrategy as HttpExceptionStrategy;
 use UnicaenApp\Options\ModuleOptions;
 
@@ -93,15 +90,13 @@ class Module implements
 
         $eventManager = $application->getEventManager();
 
-        /** @var HttpExceptionStrategy|ConsoleExceptionStrategy $baseExceptionStrategy */
         if ($e->getRequest() instanceof HttpRequest) {
+            /** @var HttpExceptionStrategy $baseExceptionStrategy */
             $baseExceptionStrategy = $services->get('HttpExceptionStrategy');
-        } else {
-            $baseExceptionStrategy = $services->get('ConsoleExceptionStrategy');
+            $exceptionStrategy = new HttpExceptionStrategy();
+            $exceptionStrategy->setDisplayExceptions($baseExceptionStrategy->displayExceptions());
+            $exceptionStrategy->attach($eventManager);
         }
-        $exceptionStrategy = new ExceptionStrategy();
-        $exceptionStrategy->setDisplayExceptions($baseExceptionStrategy->displayExceptions());
-        $exceptionStrategy->attach($eventManager);
 
         /* Démarrage des mouchards en fonction de la configuration... */
         $services->get('MouchardService')->createMouchardsFromConfig();
@@ -148,7 +143,7 @@ class Module implements
      */
     public function bootstrapSession($e)
     {
-        if (Console::isConsole()) {
+        if (Util::isConsole()) {
             return;
         }
 
@@ -174,7 +169,7 @@ class Module implements
      */
     public function appendSessionRefreshJs(EventInterface $e)
     {
-        if (Console::isConsole()) {
+        if (Util::isConsole()) {
             return;
         }
 
diff --git a/src/UnicaenApp/Mvc/View/Http/ExceptionStrategy.php b/src/UnicaenApp/Mvc/View/Http/ExceptionStrategy.php
index c76df6a9..73ee99ef 100644
--- a/src/UnicaenApp/Mvc/View/Http/ExceptionStrategy.php
+++ b/src/UnicaenApp/Mvc/View/Http/ExceptionStrategy.php
@@ -8,8 +8,8 @@ use Laminas\Mvc\Application;
 use Laminas\Mvc\MvcEvent;
 use Laminas\Stdlib\ResponseInterface as Response;
 use Laminas\View\Model\ViewModel;
-use Unicaen\Console\Console;
 use UnicaenApp\Exception\ExceptionInterface;
+use UnicaenApp\Util;
 
 /**
  * Stratégie permettant d'afficher proprement un message d'erreur lorsqu'une exception est levée
@@ -61,7 +61,7 @@ class ExceptionStrategy extends \Laminas\Mvc\View\Http\ExceptionStrategy
         }
 
         // Do nothing if the request is console type
-        if (Console::isConsole()) {
+        if (Util::isConsole()) {
             return;
         }
 
diff --git a/src/UnicaenApp/Util.php b/src/UnicaenApp/Util.php
index a09ff8fe..17baecd4 100644
--- a/src/UnicaenApp/Util.php
+++ b/src/UnicaenApp/Util.php
@@ -23,7 +23,12 @@ class Util
      */
     static protected $microtime;
 
-
+    /**
+     * Allow overriding whether or not we're in a console env. If set, and
+     * boolean, returns that value from isConsole().
+     * @var bool
+     */
+    protected static $isConsole;
 
     /**
      * Transforme en tableau simple une collection d'objets spécifiée sous forme d'un itérateur.
@@ -775,16 +780,20 @@ class Util
 
         return $content;
     }
-    
-    
-    
+
     /**
-     * Détermine si on est en mode console ou non
-     * 
+     * Check if running in a console environment (CLI)
+     *
+     * By default, returns value of PHP_SAPI global constant. If $isConsole is
+     * set, and a boolean value, that value will be returned.
+     *
      * @return bool
      */
-    static public function isConsole(): bool
+    public static function isConsole()
     {
-        return PHP_SAPI == 'cli';
+        if (null === static::$isConsole) {
+            static::$isConsole = (PHP_SAPI == 'cli');
+        }
+        return static::$isConsole;
     }
 }
diff --git a/src/UnicaenApp/View/Helper/AppLinkFactory.php b/src/UnicaenApp/View/Helper/AppLinkFactory.php
index 45df82b4..e29dcc1c 100644
--- a/src/UnicaenApp/View/Helper/AppLinkFactory.php
+++ b/src/UnicaenApp/View/Helper/AppLinkFactory.php
@@ -5,7 +5,7 @@ namespace UnicaenApp\View\Helper;
 use Psr\Container\ContainerInterface;
 use Laminas\ServiceManager\FactoryInterface;
 use Laminas\ServiceManager\ServiceLocatorInterface;
-use Unicaen\Console\Console;
+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();
 
-- 
GitLab