]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/Controllers/AbstractController.php
Properly use injection
[galette.git] / galette / lib / Galette / Controllers / AbstractController.php
index 3bb7dffc6cbcd4b6c8251845e30e7b256f1c9aaf..d5ff6c1ffa18ab67406ff5176559ad4f71f8ccaf 100644 (file)
@@ -7,7 +7,7 @@
  *
  * PHP version 5
  *
- * Copyright © 2019-2020 The Galette Team
+ * Copyright © 2019-2023 The Galette Team
  *
  * This file is part of Galette (http://galette.tuxfamily.org).
  *
@@ -28,7 +28,7 @@
  * @package   Galette
  *
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2019-2020 The Galette Team
+ * @copyright 2019-2023 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @link      http://galette.tuxfamily.org
  * @since     Available since 0.9.4dev - 2019-12-02
 
 namespace Galette\Controllers;
 
+use Galette\Core\Db;
+use Galette\Core\History;
+use Galette\Core\I18n;
+use Galette\Core\L10n;
+use Galette\Core\Login;
+use Galette\Core\Logo;
+use Galette\Core\Plugins;
+use Galette\Core\Preferences;
+use Galette\Core\PrintLogo;
+use Galette\Entity\FieldsConfig;
+use Galette\Entity\ListsConfig;
 use Psr\Container\ContainerInterface;
-use Slim\Http\Request;
-use Slim\Http\Response;
+use RKA\Session;
+use Slim\Flash\Messages;
+use Slim\Psr7\Request;
+use Slim\Psr7\Response;
+use Slim\Routing\RouteContext;
+use Slim\Routing\RouteParser;
+use DI\Attribute\Inject;
+use Slim\Views\Twig;
 
 /**
  * Galette abstract controller
@@ -47,7 +64,7 @@ use Slim\Http\Response;
  * @name      AbstractController
  * @package   Galette
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2019-2020 The Galette Team
+ * @copyright 2019-2023 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @link      http://galette.tuxfamily.org
  * @since     Available since 0.9.4dev - 2019-12-02
@@ -57,95 +74,95 @@ abstract class AbstractController
 {
     private $container;
     /**
-     * @Inject
-     * @var \Galette\Core\Db
+     * @var Db
      */
-    protected $zdb;
+    #[Inject]
+    protected Db $zdb;
     /**
-     * @Inject
-     * @var \Galette\Core\Login
+     * @var Login
      */
-    protected $login;
+    #[Inject]
+    protected Login $login;
     /**
-     * @Inject
-     * @var \Galette\Core\Preferences
+     * @var Preferences
      */
-    protected $preferences;
+    #[Inject]
+    protected Preferences $preferences;
     /**
-     * @Inject
-     * @var \Slim\Views\Smarty
+     * @var Twig
      */
-    protected $view;
+    #[Inject]
+    protected Twig $view;
     /**
-     * @Inject
-     * @var \Galette\Core\Logo
+     * @var Logo
      */
-    protected $logo;
+    #[Inject]
+    protected Logo $logo;
     /**
-     * @Inject
-     * @var \Galette\Core\PrintLogo
+     * @var PrintLogo
      */
-    protected $print_logo;
+    #[Inject]
+    protected PrintLogo $print_logo;
     /**
-     * @Inject
-     * @var \Galette\Core\Plugins
+     * @var Plugins
      */
-    protected $plugins;
+    #[Inject]
+    protected Plugins $plugins;
     /**
-     * @Inject
-     * @var \Slim\Router
+     * @var RouteParser
      */
-    protected $router;
+    #[Inject]
+    protected RouteParser $routeparser;
     /**
-     * @Inject
-     * @var \Galette\Core\History
+     * @var History
      */
-    protected $history;
+    #[Inject]
+    protected History $history;
     /**
-     * @Inject
-     * @var \Galette\Core\I18n
+     * @var I18n
      */
-    protected $i18n;
+    #[Inject]
+    protected I18n $i18n;
     /**
-     * @Inject("session")
+     * @var L10n
      */
-    protected $session;
+    #[Inject]
+    protected L10n $l10n;
     /**
-     * @Inject
-     * @var \Slim\Flash\Messages
+     * @var Session
      */
-    protected $flash;
+    #[Inject("session")]
+    protected Session $session;
     /**
-     * @Inject
-     * @var \Galette\Entity\FieldsConfig
+     * @var Messages
      */
-    protected $fields_config;
+    #[Inject]
+    protected Messages $flash;
     /**
-     * @Inject
-     * @var \Galette\Entity\ListsConfig
+     * @var FieldsConfig
      */
-    protected $lists_config;
+    #[Inject]
+    protected FieldsConfig $fields_config;
     /**
-     * @Inject
-     * @var array
+     * @var ListsConfig
      */
-    protected $members_fields;
+    #[Inject]
+    protected ListsConfig $lists_config;
     /**
-     * @Inject
      * @var array
      */
-    protected $members_form_fields;
+    #[Inject("members_fields")]
+    protected array $members_fields;
     /**
-     * @Inject
      * @var array
      */
-    protected $members_fields_cats;
-
+    #[Inject("members_form_fields")]
+    protected array $members_form_fields;
     /**
-     * @Inject
-     * @var \Galette\Handlers\NotFound
+     * @var array
      */
-    protected $notFoundHandler;
+    #[Inject("members_fields_cats")]
+    protected array $members_fields_cats;
 
     /**
      * Constructor
@@ -155,25 +172,6 @@ abstract class AbstractController
     public function __construct(ContainerInterface $container)
     {
         $this->container = $container;
-        //set variosu services we need
-        $this->zdb = $container->get('zdb');
-        $this->login = $container->get('login');
-        $this->preferences = $container->get('preferences');
-        $this->view = $container->get('view');
-        $this->logo = $container->get('logo');
-        $this->print_logo = $container->get('print_logo');
-        $this->plugins = $container->get('plugins');
-        $this->router = $container->get('router');
-        $this->history = $container->get('history');
-        $this->i18n = $container->get('i18n');
-        $this->session = $container->get('session');
-        $this->flash = $container->get('flash');
-        $this->fields_config = $container->get('fields_config');
-        $this->lists_config = $container->get('lists_config');
-        $this->notFoundHandler = $container->get('notFoundHandler');
-        $this->members_fields = $container->get('members_fields');
-        $this->members_form_fields = $container->get('members_form_fields');
-        $this->members_fields_cats = $container->get('members_fields_cats');
     }
 
     /**
@@ -182,22 +180,23 @@ abstract class AbstractController
      *
      * @param Request  $request  PSR Request
      * @param Response $response PSR Response
-     * @param array    $args     Request arguments ['r']
      *
-     * @return void
+     * @return Response
      */
-    protected function galetteRedirect(Request $request, Response $response, array $args = [])
+    protected function galetteRedirect(Request $request, Response $response)
     {
         //reinject flash messages so they're not lost
         $flashes = $this->flash->getMessages();
         foreach ($flashes as $type => $messages) {
-            $this->container->get('flash')->addMessage($type, $message);
+            foreach ($messages as $message) {
+                $this->container->get('flash')->addMessage($type, $message);
+            }
         }
 
         if ($this->login->isLogged()) {
             $urlRedirect = null;
             if ($this->session->urlRedirect !== null) {
-                $urlRedirect = $this->getGaletteBaseUrl($request) . $this->session->urlRedirect;
+                $urlRedirect = $this->session->urlRedirect;
                 $this->session->urlRedirect = null;
             }
 
@@ -217,45 +216,55 @@ abstract class AbstractController
                     ) {
                         return $response
                             ->withStatus(301)
-                            ->withHeader('Location', $this->router->pathFor('dashboard'));
+                            ->withHeader('Location', $this->routeparser->urlFor('dashboard'));
                     } else {
                         return $response
                             ->withStatus(301)
-                            ->withHeader('Location', $this->router->pathFor('members'));
+                            ->withHeader('Location', $this->routeparser->urlFor('members'));
                     }
                 } else {
                     return $response
                         ->withStatus(301)
-                        ->withHeader('Location', $this->router->pathFor('dashboard'));
+                        ->withHeader('Location', $this->routeparser->urlFor('dashboard'));
                 }
             }
         } else {
             return $response
                 ->withStatus(301)
-                ->withHeader('Location', $this->router->pathFor('login'));
+                ->withHeader('Location', $this->routeparser->urlFor('login'));
         }
     }
 
     /**
-     * Get base URL fixed for proxies
+     * Get route arguments
+     * php-di bridge pass each variable, not an array of all arguments
      *
      * @param Request $request PSR Request
      *
-     * @return string
+     * @return array
      */
-    private function getGaletteBaseUrl(Request $request)
+    protected function getArgs(Request $request): array
     {
-        $url = preg_replace(
-            [
-                '|index\.php|',
-                '|https?://' . $_SERVER['HTTP_HOST'] . '(:\d+)?' . '|'
-            ],
-            ['', ''],
-            $request->getUri()->getBaseUrl()
-        );
-        if (strlen($url) && substr($url, -1) !== '/') {
-            $url .= '/';
-        }
-        return $url;
+        $routeContext = RouteContext::fromRequest($request);
+        $route = $routeContext->getRoute();
+        $args = $route->getArguments();
+        return $args;
+    }
+
+    /**
+     * Get a JSON response
+     *
+     * @param Response $response Response instance
+     * @param array    $data     Data to send
+     * @param int      $status   HTTP status code
+     *
+     * @return Response
+     */
+    protected function withJson(Response $response, array $data, int $status = 200): Response
+    {
+        $response = $response->withStatus($status);
+        $response = $response->withHeader('Content-Type', 'application/json');
+        $response->getBody()->write(json_encode($data));
+        return $response;
     }
 }