]> git.mxchange.org Git - friendica.git/commitdiff
Add App\Router dependency injection to App
authorHypolite Petovan <hypolite@mrpetovan.com>
Sat, 6 Apr 2019 03:16:12 +0000 (23:16 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sat, 6 Apr 2019 03:16:12 +0000 (23:16 -0400)
- Moved collectRoutes method from App to App\Router

src/App.php
src/App/Router.php
src/Factory/DependencyFactory.php
tests/include/ApiTest.php
tests/src/Database/DBATest.php
tests/src/Database/DBStructureTest.php

index da9239c685589da597885f9718a28bbff8b2b220..1649a8745479f55fd74a1123b2be4e3af9bd370a 100644 (file)
@@ -78,6 +78,11 @@ class App
         */
        private $mode;
 
+       /**
+        * @var App\Router
+        */
+       private $router;
+
        /**
         * @var string The App URL path
         */
@@ -173,6 +178,11 @@ class App
                return $this->mode;
        }
 
+       public function getRouter()
+       {
+               return $this->router;
+       }
+
        /**
         * Register a stylesheet file path to be included in the <head> tag of every page.
         * Inclusion is done in App->initHead().
@@ -218,20 +228,22 @@ class App
         *
         * @param Configuration    $config    The Configuration
         * @param App\Mode         $mode      The mode of this Friendica app
+        * @param App\Router       $router    The router of this Friendica app
         * @param LoggerInterface  $logger    The current app logger
         * @param Profiler         $profiler  The profiler of this application
         * @param bool             $isBackend Whether it is used for backend or frontend (Default true=backend)
         *
         * @throws Exception if the Basepath is not usable
         */
-       public function __construct(Configuration $config, App\Mode $mode, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
+       public function __construct(Configuration $config, App\Mode $mode, App\Router $router, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
        {
                BaseObject::setApp($this);
 
-               $this->logger   = $logger;
                $this->config   = $config;
-               $this->profiler = $profiler;
                $this->mode     = $mode;
+               $this->router   = $router;
+               $this->profiler = $profiler;
+               $this->logger   = $logger;
 
                $this->checkBackend($isBackend);
                $this->checkFriendicaApp();
@@ -1248,14 +1260,24 @@ class App
                                $this->module = "login";
                        }
 
-                       $router = new App\Router();
-                       $this->collectRoutes($router->getRouteCollector());
+                       /*
+                        * ROUTING
+                        *
+                        * From the request URL, routing consists of obtaining the name of a BaseModule-extending class of which the
+                        * post() and/or content() static methods can be respectively called to produce a data change or an output.
+                        */
+
+                       // First we try explicit routes defined in App\Router
+                       $this->router->collectRoutes();
+
+                       Hook::callAll('route_collection', $this->router->getRouteCollector());
 
-                       $this->module_class = $router->getModuleClass($this->cmd);
+                       $this->module_class = $this->router->getModuleClass($this->cmd);
 
-                       $privateapps = $this->config->get('config', 'private_addons', false);
+                       // Then we try addon-provided modules that we wrap in the LegacyModule class
                        if (!$this->module_class && Core\Addon::isEnabled($this->module) && file_exists("addon/{$this->module}/{$this->module}.php")) {
                                //Check if module is an app and if public access to apps is allowed or not
+                               $privateapps = $this->config->get('config', 'private_addons', false);
                                if ((!local_user()) && Core\Hook::isAddonApp($this->module) && $privateapps) {
                                        info(Core\L10n::t("You must be logged in to use addons. "));
                                } else {
@@ -1267,12 +1289,12 @@ class App
                                }
                        }
 
-                       // Controller class routing
+                       // Then we try name-matching a Friendica\Module class
                        if (!$this->module_class && class_exists('Friendica\\Module\\' . ucfirst($this->module))) {
                                $this->module_class = 'Friendica\\Module\\' . ucfirst($this->module);
                        }
 
-                       /* If not, next look for a 'standard' program module in the 'mod' directory
+                       /* Finally, we look for a 'standard' program module in the 'mod' directory
                         * We emulate a Module class through the LegacyModule class
                         */
                        if (!$this->module_class && file_exists("mod/{$this->module}.php")) {
@@ -1505,27 +1527,4 @@ class App
                        $this->internalRedirect($toUrl);
                }
        }
-
-       /**
-        * Static declaration of Friendica routes.
-        *
-        * Supports:
-        * - Route groups
-        * - Variable parts
-        * Disregards:
-        * - HTTP method other than GET
-        * - Named parameters
-        *
-        * Handler must be the name of a class extending Friendica\BaseModule.
-        *
-        * @brief Static declaration of Friendica routes.
-        * @param RouteCollector $routeCollector
-        * @throws InternalServerErrorException
-        */
-       private function collectRoutes(RouteCollector $routeCollector)
-       {
-               $routeCollector->addRoute(['GET', 'POST'], '/itemsource[/{guid}]', Module\Itemsource::class);
-
-               Hook::callAll('route_collection', $routeCollector);
-       }
 }
index 7753b6bbb9277be4a12229510b071ae1ac9384e0..be85fcd9dc33cf49a4c3c9bc1237ca44f3a6e3f1 100644 (file)
@@ -7,6 +7,7 @@ use FastRoute\DataGenerator\GroupCountBased;
 use FastRoute\Dispatcher;\r
 use FastRoute\RouteCollector;\r
 use FastRoute\RouteParser\Std;\r
+use Friendica\Module;\r
 \r
 /**\r
  * Wrapper for FastRoute\Router\r
@@ -23,6 +24,25 @@ class Router
        /** @var RouteCollector */\r
        protected $routeCollector;\r
 \r
+       /**\r
+        * Static declaration of Friendica routes.\r
+        *\r
+        * Supports:\r
+        * - Route groups\r
+        * - Variable parts\r
+        * Disregards:\r
+        * - HTTP method other than GET\r
+        * - Named parameters\r
+        *\r
+        * Handler must be the name of a class extending Friendica\BaseModule.\r
+        *\r
+        * @brief Static declaration of Friendica routes.\r
+        */\r
+       public function collectRoutes()\r
+       {\r
+               $this->routeCollector->addRoute(['GET', 'POST'], '/itemsource[/{guid}]', Module\Itemsource::class);\r
+       }\r
+\r
        public function __construct(RouteCollector $routeCollector = null)\r
        {\r
                if (!$routeCollector) {\r
@@ -37,6 +57,12 @@ class Router
                return $this->routeCollector;\r
        }\r
 \r
+       /**\r
+        * Returns the relevant module class name for the given page URI or NULL if no route rule matched.\r
+        *\r
+        * @param string $cmd The path component of the request URL without the query string\r
+        * @return string|null A Friendica\BaseModule-extending class name if a route rule matched\r
+        */\r
        public function getModuleClass($cmd)\r
        {\r
                $cmd = '/' . ltrim($cmd, '/');\r
index 63defd95f5f5847e28d8a8dcd1085ad66e4337ce..0f33e095bc7286ff23d54ca0d81f458d556a1ca6 100644 (file)
@@ -24,6 +24,7 @@ class DependencyFactory
        {
                $basePath = BasePath::create($directory, $_SERVER);
                $mode = new App\Mode($basePath);
+               $router = new App\Router();
                $configLoader = new Config\ConfigFileLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
@@ -34,6 +35,6 @@ class DependencyFactory
                $logger = Factory\LoggerFactory::create($channel, $config, $profiler);
                Factory\LoggerFactory::createDev($channel, $config, $profiler);
 
-               return new App($config, $mode, $logger, $profiler, $isBackend);
+               return new App($config, $mode, $router, $logger, $profiler, $isBackend);
        }
 }
index 80a25c20c11fe438f73988ff481e7fe7997d22be..ecfe3e962153756ec9c70c30e3a972e699ffe8b8 100644 (file)
@@ -50,6 +50,7 @@ class ApiTest extends DatabaseTest
        {
                $basePath = BasePath::create(dirname(__DIR__) . '/../');
                $mode = new App\Mode($basePath);
+               $router = new App\Router();
                $configLoader = new ConfigFileLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
@@ -57,7 +58,7 @@ class ApiTest extends DatabaseTest
                $config = Factory\ConfigFactory::createConfig($configCache);
                Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config, $profiler);
-               $this->app = new App($config, $mode, $logger, $profiler, false);
+               $this->app = new App($config, $mode, $router, $logger, $profiler, false);
 
                parent::setUp();
 
index c9413772195c9c279bc87f18e10e4cac10d9dfac..889ae6af0d8b4c7256209f58e309fb7c129e7450 100644 (file)
@@ -15,6 +15,7 @@ class DBATest extends DatabaseTest
        {
                $basePath = BasePath::create(dirname(__DIR__) . '/../../');
                $mode = new App\Mode($basePath);
+               $router = new App\Router();
                $configLoader = new ConfigFileLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
@@ -22,7 +23,7 @@ class DBATest extends DatabaseTest
                $config = Factory\ConfigFactory::createConfig($configCache);
                Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config, $profiler);
-               $this->app = new App($config, $mode, $logger, $profiler, false);
+               $this->app = new App($config, $mode, $router, $logger, $profiler, false);
 
                parent::setUp();
 
index 152014c114c9c236540bf631ebc511de492b7ad6..ec1531783e72595c6eb1b3532ee85adc86cc9c71 100644 (file)
@@ -15,6 +15,7 @@ class DBStructureTest extends DatabaseTest
        {
                $basePath = BasePath::create(dirname(__DIR__) . '/../../');
                $mode = new App\Mode($basePath);
+               $router = new App\Router();
                $configLoader = new ConfigFileLoader($basePath, $mode);
                $configCache = Factory\ConfigFactory::createCache($configLoader);
                $profiler = Factory\ProfilerFactory::create($configCache);
@@ -22,7 +23,7 @@ class DBStructureTest extends DatabaseTest
                $config = Factory\ConfigFactory::createConfig($configCache);
                Factory\ConfigFactory::createPConfig($configCache);
                $logger = Factory\LoggerFactory::create('test', $config, $profiler);
-               $this->app = new App($config, $mode, $logger, $profiler, false);
+               $this->app = new App($config, $mode, $router, $logger, $profiler, false);
 
                parent::setUp();
        }