]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Router.php
Refactor API notification usage
[friendica.git] / src / App / Router.php
index f723321ac67cf883bfe8a2ab93c26c5e77c78b7c..450822cf86fdf530212fc6b40ba65c2393a1b1f9 100644 (file)
@@ -39,12 +39,23 @@ class Router
         */
        private $httpMethod;
 
+       /**
+        * @var array Module parameters
+        */
+       private $parameters = [];
+
+       /** @var L10n */
+       private $l10n;
+
        /**
         * @param array $server The $_SERVER variable
+        * @param L10n  $l10n
         * @param RouteCollector|null $routeCollector Optional the loaded Route collector
         */
-       public function __construct(array $server, RouteCollector $routeCollector = null)
+       public function __construct(array $server, L10n $l10n, RouteCollector $routeCollector = null)
        {
+               $this->l10n = $l10n;
+
                $httpMethod = $server['REQUEST_METHOD'] ?? self::GET;
                $this->httpMethod = in_array($httpMethod, self::ALLOWED_METHODS) ? $httpMethod : self::GET;
 
@@ -60,12 +71,21 @@ class Router
         *
         * @throws HTTPException\InternalServerErrorException In case of invalid configs
         */
-       public function addRoutes(array $routes)
+       public function loadRoutes(array $routes)
        {
                $routeCollector = (isset($this->routeCollector) ?
                        $this->routeCollector :
                        new RouteCollector(new Std(), new GroupCountBased()));
 
+               $this->addRoutes($routeCollector, $routes);
+
+               $this->routeCollector = $routeCollector;
+
+               return $this;
+       }
+
+       private function addRoutes(RouteCollector $routeCollector, array $routes)
+       {
                foreach ($routes as $route => $config) {
                        if ($this->isGroup($config)) {
                                $this->addGroup($route, $config, $routeCollector);
@@ -75,10 +95,6 @@ class Router
                                throw new HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
                        }
                }
-
-               $this->routeCollector = $routeCollector;
-
-               return $this;
        }
 
        /**
@@ -91,15 +107,7 @@ class Router
        private function addGroup(string $groupRoute, array $routes, RouteCollector $routeCollector)
        {
                $routeCollector->addGroup($groupRoute, function (RouteCollector $routeCollector) use ($routes) {
-                       foreach ($routes as $route => $config) {
-                               if ($this->isGroup($config)) {
-                                       $this->addGroup($route, $config, $routeCollector);
-                               } elseif ($this->isRoute($config)) {
-                                       $routeCollector->addRoute($config[1], $route, $config[0]);
-                               }else {
-                                       throw new HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
-                               }
-                       }
+                       $this->addRoutes($routeCollector, $routes);
                });
        }
 
@@ -169,19 +177,31 @@ class Router
 
                $cmd = '/' . ltrim($cmd, '/');
 
-               $dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData());
+               $dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData());
 
                $moduleClass = null;
+               $this->parameters = [];
 
                $routeInfo  = $dispatcher->dispatch($this->httpMethod, $cmd);
                if ($routeInfo[0] === Dispatcher::FOUND) {
                        $moduleClass = $routeInfo[1];
+                       $this->parameters = $routeInfo[2];
                } elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
-                       throw new HTTPException\MethodNotAllowedException(L10n::t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
+                       throw new HTTPException\MethodNotAllowedException($this->l10n->t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
                } else {
-                       throw new HTTPException\NotFoundException(L10n::t('Page not found.'));
+                       throw new HTTPException\NotFoundException($this->l10n->t('Page not found.'));
                }
 
                return $moduleClass;
        }
+
+       /**
+        * Returns the module parameters.
+        *
+        * @return array parameters
+        */
+       public function getModuleParameters()
+       {
+               return $this->parameters;
+       }
 }