]> git.mxchange.org Git - friendica.git/blobdiff - src/App/Router.php
Creating interfaces for Config/PConfig & fix tests
[friendica.git] / src / App / Router.php
index 1bdfdcab1d0fac735d338c8fba7baa0e7def8dd6..c9ba21bb3538e2df167dc74176652f2e4ab659c4 100644 (file)
@@ -8,7 +8,8 @@ use FastRoute\Dispatcher;
 use FastRoute\RouteCollector;
 use FastRoute\RouteParser\Std;
 use Friendica\Core\Hook;
-use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Core\L10n;
+use Friendica\Network\HTTPException;
 
 /**
  * Wrapper for FastRoute\Router
@@ -38,6 +39,11 @@ class Router
         */
        private $httpMethod;
 
+       /**
+        * @var array Module parameters
+        */
+       private $parameters = [];
+
        /**
         * @param array $server The $_SERVER variable
         * @param RouteCollector|null $routeCollector Optional the loaded Route collector
@@ -57,27 +63,32 @@ class Router
         *
         * @return self The router instance with the loaded routes
         *
-        * @throws InternalServerErrorException In case of invalid configs
+        * @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);
                        } elseif ($this->isRoute($config)) {
                                $routeCollector->addRoute($config[1], $route, $config[0]);
                        } else {
-                               throw new InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
+                               throw new HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
                        }
                }
-
-               $this->routeCollector = $routeCollector;
-
-               return $this;
        }
 
        /**
@@ -90,15 +101,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 InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
-                               }
-                       }
+                       $this->addRoutes($routeCollector, $routes);
                });
        }
 
@@ -155,7 +158,11 @@ class Router
         *
         * @param string $cmd The path component of the request URL without the query string
         *
-        * @return string|null A Friendica\BaseModule-extending class name if a route rule matched
+        * @return string A Friendica\BaseModule-extending class name if a route rule matched
+        *
+        * @throws HTTPException\InternalServerErrorException
+        * @throws HTTPException\MethodNotAllowedException    If a rule matched but the method didn't
+        * @throws HTTPException\NotFoundException            If no rule matched
         */
        public function getModuleClass($cmd)
        {
@@ -164,15 +171,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])));
+               } else {
+                       throw new HTTPException\NotFoundException(L10n::t('Page not found.'));
                }
 
                return $moduleClass;
        }
+
+       /**
+        * Returns the module parameters.
+        *
+        * @return array parameters
+        */
+       public function getModuleParameters()
+       {
+               return $this->parameters;
+       }
 }