Merge pull request #7822 from MrPetovan/task/router-parameter-content

Pass Router parameters to module content method
This commit is contained in:
Philipp 2019-11-07 07:14:30 +01:00 committed by GitHub
commit 0258fd17c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 20 deletions

View file

@ -94,6 +94,14 @@ class Module
return $this->module_class; 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 * @return bool True, if the current module is a backend module
* @see Module::BACKEND_MODULES for a list * @see Module::BACKEND_MODULES for a list

View file

@ -308,7 +308,7 @@ class Page implements ArrayAccess
$arr = ['content' => $content]; $arr = ['content' => $content];
Hook::callAll($moduleClass . '_mod_content', $arr); Hook::callAll($moduleClass . '_mod_content', $arr);
$content = $arr['content']; $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); Hook::callAll($moduleClass . '_mod_aftercontent', $arr);
$content .= $arr['content']; $content .= $arr['content'];
} catch (HTTPException $e) { } catch (HTTPException $e) {

View file

@ -65,12 +65,21 @@ class Router
* *
* @throws HTTPException\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) ? $routeCollector = (isset($this->routeCollector) ?
$this->routeCollector : $this->routeCollector :
new RouteCollector(new Std(), new GroupCountBased())); 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) { foreach ($routes as $route => $config) {
if ($this->isGroup($config)) { if ($this->isGroup($config)) {
$this->addGroup($route, $config, $routeCollector); $this->addGroup($route, $config, $routeCollector);
@ -80,10 +89,6 @@ class Router
throw new HTTPException\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;
} }
/** /**
@ -96,15 +101,7 @@ class Router
private function addGroup(string $groupRoute, array $routes, RouteCollector $routeCollector) private function addGroup(string $groupRoute, array $routes, RouteCollector $routeCollector)
{ {
$routeCollector->addGroup($groupRoute, function (RouteCollector $routeCollector) use ($routes) { $routeCollector->addGroup($groupRoute, function (RouteCollector $routeCollector) use ($routes) {
foreach ($routes as $route => $config) { $this->addRoutes($routeCollector, $routes);
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) . "'");
}
}
}); });
} }
@ -174,7 +171,7 @@ class Router
$cmd = '/' . ltrim($cmd, '/'); $cmd = '/' . ltrim($cmd, '/');
$dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData()); $dispatcher = new Dispatcher\GroupCountBased($this->routeCollector->getData());
$moduleClass = null; $moduleClass = null;
$this->parameters = []; $this->parameters = [];

View file

@ -171,7 +171,7 @@ return [
$_SERVER, null $_SERVER, null
], ],
'call' => [ 'call' => [
['addRoutes', [include __DIR__ . '/routes.config.php'], Dice::CHAIN_CALL], ['loadRoutes', [include __DIR__ . '/routes.config.php'], Dice::CHAIN_CALL],
], ],
], ],
L10n::class => [ L10n::class => [

View file

@ -152,7 +152,7 @@ class ModuleTest extends DatabaseTest
$config = \Mockery::mock(Configuration::class); $config = \Mockery::mock(Configuration::class);
$config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once(); $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); $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);

View file

@ -159,7 +159,7 @@ class RouterTest extends TestCase
{ {
$router = (new Router([ $router = (new Router([
'REQUEST_METHOD' => Router::GET 'REQUEST_METHOD' => Router::GET
]))->addRoutes($routes); ]))->loadRoutes($routes);
$this->assertEquals(Module\Home::class, $router->getModuleClass('/')); $this->assertEquals(Module\Home::class, $router->getModuleClass('/'));
$this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route')); $this->assertEquals(Module\Friendica::class, $router->getModuleClass('/group/route'));
@ -174,7 +174,7 @@ class RouterTest extends TestCase
{ {
$router = (new Router([ $router = (new Router([
'REQUEST_METHOD' => Router::POST 'REQUEST_METHOD' => Router::POST
]))->addRoutes($routes); ]))->loadRoutes($routes);
// Don't find GET // Don't find GET
$this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it')); $this->assertEquals(Module\NodeInfo::class, $router->getModuleClass('/post/it'));