]> git.mxchange.org Git - friendica.git/commitdiff
Pass Router parameter to module content method
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 7 Nov 2019 03:34:38 +0000 (22:34 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Thu, 7 Nov 2019 03:35:28 +0000 (22:35 -0500)
src/App/Module.php
src/App/Page.php
src/App/Router.php
static/dependencies.config.php
tests/src/App/ModuleTest.php
tests/src/App/RouterTest.php

index eb5552285ea54a1cc4be9553afd779d8351d8191..868520c0257b23e5b77c6fa6c7ec7a24f52d1fae 100644 (file)
@@ -94,6 +94,14 @@ class Module
                return $this->module_class;
        }
 
+       /**
+        * @return array The module parameters extracted from the route
+        */
+       public function getParameters()
+       {
+               return $this->module_parameters;
+       }
+
        /**
         * @return bool True, if the current module is a backend module
         * @see Module::BACKEND_MODULES for a list
index de75db32fed95f081c94b2aec89b5e4f081e17dc..7af0bc8995473b452d7aaef051569c66a127b19b 100644 (file)
@@ -308,7 +308,7 @@ class Page implements ArrayAccess
                        $arr = ['content' => $content];
                        Hook::callAll($moduleClass . '_mod_content', $arr);
                        $content = $arr['content'];
-                       $arr     = ['content' => call_user_func([$moduleClass, 'content'], [])];
+                       $arr     = ['content' => call_user_func([$moduleClass, 'content'], $module->getParameters())];
                        Hook::callAll($moduleClass . '_mod_aftercontent', $arr);
                        $content .= $arr['content'];
                } catch (HTTPException $e) {
index a4a4a852a044748768fbe6aa7f5eb10c1b9aab3b..c9ba21bb3538e2df167dc74176652f2e4ab659c4 100644 (file)
@@ -65,12 +65,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);
@@ -80,10 +89,6 @@ class Router
                                throw new HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
                        }
                }
-
-               $this->routeCollector = $routeCollector;
-
-               return $this;
        }
 
        /**
@@ -96,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 HTTPException\InternalServerErrorException("Wrong route config for route '" . print_r($route, true) . "'");
-                               }
-                       }
+                       $this->addRoutes($routeCollector, $routes);
                });
        }
 
@@ -174,7 +171,7 @@ class Router
 
                $cmd = '/' . ltrim($cmd, '/');
 
-               $dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData());
+               $dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData());
 
                $moduleClass = null;
                $this->parameters = [];
index 938b13495b56cd6db6bd8ff9f2a062b600b74433..fbc085f4bc347dcc8dae17fecb7cb3eb79e29b61 100644 (file)
@@ -171,7 +171,7 @@ return [
                        $_SERVER, null
                ],
                'call' => [
-                       ['addRoutes', [include __DIR__ . '/routes.config.php'], Dice::CHAIN_CALL],
+                       ['loadRoutes', [include __DIR__ . '/routes.config.php'], Dice::CHAIN_CALL],
                ],
        ],
        L10n::class => [
index 8327bc706dd479e8d8c4159966f7150c0be67636..ce2a40b80640bca61ee2a4caa4fdf9b30527763b 100644 (file)
@@ -152,7 +152,7 @@ class ModuleTest extends DatabaseTest
                $config = \Mockery::mock(Configuration::class);
                $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
 
-               $router = (new App\Router([]))->addRoutes(include __DIR__ . '/../../../static/routes.config.php');
+               $router = (new App\Router([]))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php');
 
                $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
 
index b2dbaed20c8536440da9fa1626d37353bd8db8fa..102808f6ac22e677180df6545807c8600fb54667 100644 (file)
@@ -159,7 +159,7 @@ class RouterTest extends TestCase
        {
                $router = (new Router([
                        'REQUEST_METHOD' => Router::GET
-               ]))->addRoutes($routes);
+               ]))->loadRoutes($routes);
 
                $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
                $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
@@ -174,7 +174,7 @@ class RouterTest extends TestCase
        {
                $router = (new Router([
                        'REQUEST_METHOD' => Router::POST
-               ]))->addRoutes($routes);
+               ]))->loadRoutes($routes);
 
                // Don't find GET
                $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));