Added 'addParameter()' method to routerLoadViewAndControllerEvent, and to routerCallViewEvent.

This allows the developer to add parameters retrieved outside of the matching system.
Requested by CLIComponent.
This commit is contained in:
Abel Hoogeveen 2021-01-24 13:49:34 +01:00
parent 9d86c03f02
commit 90e6f64c40
Signed by: abelhooge
GPG Key ID: 387E8DC1F73306FC
5 changed files with 33 additions and 10 deletions

View File

@ -66,7 +66,7 @@ class RouterCallViewEvent extends Event
/** /**
* The parameters that will be provided to the function in the view * The parameters that will be provided to the function in the view
* *
* @var string * @var array
*/ */
public $viewParameters; public $viewParameters;
@ -91,7 +91,7 @@ class RouterCallViewEvent extends Event
*/ */
public $controller; public $controller;
public function init(View $view, Controller $controller, array $viewMethods, string $viewParameters, string $route) public function init(View $view, Controller $controller, array $viewMethods, array $viewParameters, string $route)
{ {
$this->view = $view; $this->view = $view;
$this->controller = $controller; $this->controller = $controller;
@ -114,4 +114,14 @@ class RouterCallViewEvent extends Event
if (!isset($this->viewMethods[$priority][$method])) if (!isset($this->viewMethods[$priority][$method]))
$this->viewMethods[$priority][] = $method; $this->viewMethods[$priority][] = $method;
} }
/**
* Add a parameter to call the view with
*
* @param $parameter
*/
public function addParameter($parameter)
{
$this->viewParameters[] = $parameter;
}
} }

View File

@ -75,7 +75,7 @@ class RouterLoadViewAndControllerEvent extends Event
/** /**
* The parameters that will be provided to the function in the view * The parameters that will be provided to the function in the view
* *
* @var string * @var array
*/ */
public $viewParameters; public $viewParameters;
@ -100,7 +100,7 @@ class RouterLoadViewAndControllerEvent extends Event
*/ */
public $controller; public $controller;
public function init(string $viewName, string $viewType, array $viewMethods, string $viewParameters, string $namespacePrefix, string $route) public function init(string $viewName, string $viewType, array $viewMethods, array $viewParameters, string $namespacePrefix, string $route)
{ {
$this->viewName = $viewName; $this->viewName = $viewName;
$this->viewType = $viewType; $this->viewType = $viewType;
@ -125,6 +125,16 @@ class RouterLoadViewAndControllerEvent extends Event
$this->viewMethods[$priority][] = $method; $this->viewMethods[$priority][] = $method;
} }
/**
* Add a parameter to call the view with
*
* @param $parameter
*/
public function addParameter($parameter)
{
$this->viewParameters[] = $parameter;
}
/** /**
* Override the controller to be provided to the view. * Override the controller to be provided to the view.
* *

View File

@ -344,7 +344,7 @@ class Router
$viewType, $viewType,
// ViewMethod is provided as a Priority::NORMAL method // ViewMethod is provided as a Priority::NORMAL method
[3 => [$viewMethod]], [3 => [$viewMethod]],
$viewParameters, [$viewParameters],
$namespacePrefix, $namespacePrefix,
$route $route
); );
@ -416,7 +416,7 @@ class Router
{ {
// Execute this method on the view // Execute this method on the view
Logger::newLevel("Calling method '{$method}' on " . get_class($this->view) . ' with ' . get_class($this->controller)); Logger::newLevel("Calling method '{$method}' on " . get_class($this->view) . ' with ' . get_class($this->controller));
$output = $this->view->{$method}($event->viewParameters); $output = call_user_func_array([$this->view, $method], $event->viewParameters);
Logger::stopLevel(); Logger::stopLevel();
return $output; return $output;
} }

View File

@ -437,7 +437,8 @@ class RouterTest extends TestCase
{ {
$matches = [ $matches = [
'viewName' => 'TestDefaultCallableChangeMethod', 'viewName' => 'TestDefaultCallableChangeMethod',
'viewMethod' => 'index' 'viewMethod' => 'index',
'viewParameters' => 'parameter1'
]; ];
$data = [ $data = [
@ -448,13 +449,15 @@ class RouterTest extends TestCase
$this->assertNull($this->router->getCurrentView()); $this->assertNull($this->router->getCurrentView());
$mockController = $this->getMockBuilder('\FuzeWorks\Controller')->getMock(); $mockController = $this->getMockBuilder('\FuzeWorks\Controller')->getMock();
// Create listener // Create listener
Events::addListener(function($event, $mockController){ Events::addListener(function($event, $mockController){
$event->overrideController($mockController); $event->overrideController($mockController);
$event->addMethod('altered', Priority::HIGH); $event->addMethod('altered', Priority::HIGH);
$event->addParameter('parameter2');
}, 'routerLoadViewAndControllerEvent', Priority::NORMAL, $mockController); }, 'routerLoadViewAndControllerEvent', Priority::NORMAL, $mockController);
$this->assertEquals('Altered!', $this->router->defaultCallable($matches, $data, '.*$')); $this->assertEquals(['Altered', 'parameter1', 'parameter2'], $this->router->defaultCallable($matches, $data, '.*$'));
} }
/* route() ------------------------------------------------------------ */ /* route() ------------------------------------------------------------ */

View File

@ -45,8 +45,8 @@ class TestDefaultCallableChangeMethodTestView extends View
return "Not altered!"; return "Not altered!";
} }
public function altered() public function altered(string $param1, string $param2)
{ {
return "Altered!"; return ["Altered", $param1, $param2];
} }
} }