1
0
Fork 0

Merge pull request #7818 from annando/module-parameters

Pass the parameters from the router to the modules
This commit is contained in:
Philipp 2019-11-05 09:05:06 +01:00 committed by GitHub
commit d6fe071339
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 13 deletions

View file

@ -63,6 +63,11 @@ class Module
*/ */
private $module_class; private $module_class;
/**
* @var array The module parameters
*/
private $module_parameters;
/** /**
* @var bool true, if the module is a backend module * @var bool true, if the module is a backend module
*/ */
@ -98,10 +103,11 @@ class Module
return $this->isBackend; return $this->isBackend;
} }
public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, bool $isBackend = false, bool $printNotAllowedAddon = false) public function __construct(string $module = self::DEFAULT, string $moduleClass = self::DEFAULT_CLASS, array $moduleParameters = [], bool $isBackend = false, bool $printNotAllowedAddon = false)
{ {
$this->module = $module; $this->module = $module;
$this->module_class = $moduleClass; $this->module_class = $moduleClass;
$this->module_parameters = $moduleParameters;
$this->isBackend = $isBackend; $this->isBackend = $isBackend;
$this->printNotAllowedAddon = $printNotAllowedAddon; $this->printNotAllowedAddon = $printNotAllowedAddon;
} }
@ -129,7 +135,7 @@ class Module
$isBackend = in_array($module, Module::BACKEND_MODULES);; $isBackend = in_array($module, Module::BACKEND_MODULES);;
return new Module($module, $this->module_class, $isBackend, $this->printNotAllowedAddon); return new Module($module, $this->module_class, [], $isBackend, $this->printNotAllowedAddon);
} }
/** /**
@ -148,6 +154,7 @@ class Module
$printNotAllowedAddon = false; $printNotAllowedAddon = false;
$module_class = null; $module_class = null;
$module_parameters = [];
/** /**
* ROUTING * ROUTING
* *
@ -156,6 +163,7 @@ class Module
**/ **/
try { try {
$module_class = $router->getModuleClass($args->getCommand()); $module_class = $router->getModuleClass($args->getCommand());
$module_parameters = $router->getModuleParameters();
} catch (MethodNotAllowedException $e) { } catch (MethodNotAllowedException $e) {
$module_class = MethodNotAllowed::class; $module_class = MethodNotAllowed::class;
} catch (NotFoundException $e) { } catch (NotFoundException $e) {
@ -185,7 +193,7 @@ class Module
$module_class = $module_class ?: PageNotFound::class; $module_class = $module_class ?: PageNotFound::class;
} }
return new Module($this->module, $module_class, $this->isBackend, $printNotAllowedAddon); return new Module($this->module, $module_class, $module_parameters, $this->isBackend, $printNotAllowedAddon);
} }
/** /**
@ -233,18 +241,18 @@ class Module
Core\Hook::callAll($this->module . '_mod_init', $placeholder); Core\Hook::callAll($this->module . '_mod_init', $placeholder);
call_user_func([$this->module_class, 'init']); call_user_func([$this->module_class, 'init'], $this->module_parameters);
// "rawContent" is especially meant for technical endpoints. // "rawContent" is especially meant for technical endpoints.
// This endpoint doesn't need any theme initialization or other comparable stuff. // This endpoint doesn't need any theme initialization or other comparable stuff.
call_user_func([$this->module_class, 'rawContent']); call_user_func([$this->module_class, 'rawContent'], $this->module_parameters);
if ($server['REQUEST_METHOD'] === 'POST') { if ($server['REQUEST_METHOD'] === 'POST') {
Core\Hook::callAll($this->module . '_mod_post', $post); Core\Hook::callAll($this->module . '_mod_post', $post);
call_user_func([$this->module_class, 'post']); call_user_func([$this->module_class, 'post'], $this->module_parameters);
} }
Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder); Core\Hook::callAll($this->module . '_mod_afterpost', $placeholder);
call_user_func([$this->module_class, 'afterpost']); call_user_func([$this->module_class, 'afterpost'], $this->module_parameters);
} }
} }

View file

@ -39,6 +39,11 @@ class Router
*/ */
private $httpMethod; private $httpMethod;
/**
* @var array Module parameters
*/
private $parameters = [];
/** /**
* @param array $server The $_SERVER variable * @param array $server The $_SERVER variable
* @param RouteCollector|null $routeCollector Optional the loaded Route collector * @param RouteCollector|null $routeCollector Optional the loaded Route collector
@ -172,10 +177,12 @@ class Router
$dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData()); $dispatcher = new \FastRoute\Dispatcher\GroupCountBased($this->routeCollector->getData());
$moduleClass = null; $moduleClass = null;
$this->parameters = [];
$routeInfo = $dispatcher->dispatch($this->httpMethod, $cmd); $routeInfo = $dispatcher->dispatch($this->httpMethod, $cmd);
if ($routeInfo[0] === Dispatcher::FOUND) { if ($routeInfo[0] === Dispatcher::FOUND) {
$moduleClass = $routeInfo[1]; $moduleClass = $routeInfo[1];
$this->parameters = $routeInfo[2];
} elseif ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) { } 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(L10n::t('Method not allowed for this module. Allowed method(s): %s', implode(', ', $routeInfo[1])));
} else { } else {
@ -184,4 +191,14 @@ class Router
return $moduleClass; return $moduleClass;
} }
/**
* Returns the module parameters.
*
* @return array parameters
*/
public function getModuleParameters()
{
return $this->parameters;
}
} }

View file

@ -200,7 +200,7 @@ class ModeTest extends MockedTest
public function testIsBackendButIndex() public function testIsBackendButIndex()
{ {
$server = []; $server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, true); $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], true);
$mobileDetect = new MobileDetect(); $mobileDetect = new MobileDetect();
$mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect); $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
@ -214,7 +214,7 @@ class ModeTest extends MockedTest
public function testIsNotBackend() public function testIsNotBackend()
{ {
$server = []; $server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false); $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = new MobileDetect(); $mobileDetect = new MobileDetect();
$mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect); $mode = (new Mode())->determineRunMode(false, $module, $server, $mobileDetect);
@ -232,7 +232,7 @@ class ModeTest extends MockedTest
'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest', 'HTTP_X_REQUESTED_WITH' => 'xmlhttprequest',
]; ];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false); $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = new MobileDetect(); $mobileDetect = new MobileDetect();
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect); $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
@ -246,7 +246,7 @@ class ModeTest extends MockedTest
public function testIsNotAjax() public function testIsNotAjax()
{ {
$server = []; $server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false); $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = new MobileDetect(); $mobileDetect = new MobileDetect();
$mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect); $mode = (new Mode())->determineRunMode(true, $module, $server, $mobileDetect);
@ -260,7 +260,7 @@ class ModeTest extends MockedTest
public function testIsMobileIsTablet() public function testIsMobileIsTablet()
{ {
$server = []; $server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false); $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = \Mockery::mock(MobileDetect::class); $mobileDetect = \Mockery::mock(MobileDetect::class);
$mobileDetect->shouldReceive('isMobile')->andReturn(true); $mobileDetect->shouldReceive('isMobile')->andReturn(true);
$mobileDetect->shouldReceive('isTablet')->andReturn(true); $mobileDetect->shouldReceive('isTablet')->andReturn(true);
@ -278,7 +278,7 @@ class ModeTest extends MockedTest
public function testIsNotMobileIsNotTablet() public function testIsNotMobileIsNotTablet()
{ {
$server = []; $server = [];
$module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, false); $module = new Module(Module::DEFAULT, Module::DEFAULT_CLASS, [], false);
$mobileDetect = \Mockery::mock(MobileDetect::class); $mobileDetect = \Mockery::mock(MobileDetect::class);
$mobileDetect->shouldReceive('isMobile')->andReturn(false); $mobileDetect->shouldReceive('isMobile')->andReturn(false);
$mobileDetect->shouldReceive('isTablet')->andReturn(false); $mobileDetect->shouldReceive('isTablet')->andReturn(false);