From 90e6f64c4056321ca2fb114fc08531c14b7285a1 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Sun, 24 Jan 2021 13:49:34 +0100 Subject: [PATCH] Added 'addParameter()' method to routerLoadViewAndControllerEvent, and to routerCallViewEvent. This allows the developer to add parameters retrieved outside of the matching system. Requested by CLIComponent. --- src/FuzeWorks/Event/RouterCallViewEvent.php | 14 ++++++++++++-- .../Event/RouterLoadViewAndControllerEvent.php | 14 ++++++++++++-- src/FuzeWorks/Router.php | 4 ++-- test/mcr/RouterTest.php | 7 +++++-- .../view.test.testdefaultcallablechangemethod.php | 4 ++-- 5 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/FuzeWorks/Event/RouterCallViewEvent.php b/src/FuzeWorks/Event/RouterCallViewEvent.php index 8cb6679..01f5ae4 100644 --- a/src/FuzeWorks/Event/RouterCallViewEvent.php +++ b/src/FuzeWorks/Event/RouterCallViewEvent.php @@ -66,7 +66,7 @@ class RouterCallViewEvent extends Event /** * The parameters that will be provided to the function in the view * - * @var string + * @var array */ public $viewParameters; @@ -91,7 +91,7 @@ class RouterCallViewEvent extends Event */ 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->controller = $controller; @@ -114,4 +114,14 @@ class RouterCallViewEvent extends Event if (!isset($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; + } } \ No newline at end of file diff --git a/src/FuzeWorks/Event/RouterLoadViewAndControllerEvent.php b/src/FuzeWorks/Event/RouterLoadViewAndControllerEvent.php index 7ec8e6d..0bd574d 100644 --- a/src/FuzeWorks/Event/RouterLoadViewAndControllerEvent.php +++ b/src/FuzeWorks/Event/RouterLoadViewAndControllerEvent.php @@ -75,7 +75,7 @@ class RouterLoadViewAndControllerEvent extends Event /** * The parameters that will be provided to the function in the view * - * @var string + * @var array */ public $viewParameters; @@ -100,7 +100,7 @@ class RouterLoadViewAndControllerEvent extends Event */ 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->viewType = $viewType; @@ -125,6 +125,16 @@ class RouterLoadViewAndControllerEvent extends Event $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. * diff --git a/src/FuzeWorks/Router.php b/src/FuzeWorks/Router.php index 6e17937..91c456e 100644 --- a/src/FuzeWorks/Router.php +++ b/src/FuzeWorks/Router.php @@ -344,7 +344,7 @@ class Router $viewType, // ViewMethod is provided as a Priority::NORMAL method [3 => [$viewMethod]], - $viewParameters, + [$viewParameters], $namespacePrefix, $route ); @@ -416,7 +416,7 @@ class Router { // Execute this method on the view 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(); return $output; } diff --git a/test/mcr/RouterTest.php b/test/mcr/RouterTest.php index 32d0a1f..552b681 100644 --- a/test/mcr/RouterTest.php +++ b/test/mcr/RouterTest.php @@ -437,7 +437,8 @@ class RouterTest extends TestCase { $matches = [ 'viewName' => 'TestDefaultCallableChangeMethod', - 'viewMethod' => 'index' + 'viewMethod' => 'index', + 'viewParameters' => 'parameter1' ]; $data = [ @@ -448,13 +449,15 @@ class RouterTest extends TestCase $this->assertNull($this->router->getCurrentView()); $mockController = $this->getMockBuilder('\FuzeWorks\Controller')->getMock(); + // Create listener Events::addListener(function($event, $mockController){ $event->overrideController($mockController); $event->addMethod('altered', Priority::HIGH); + $event->addParameter('parameter2'); }, 'routerLoadViewAndControllerEvent', Priority::NORMAL, $mockController); - $this->assertEquals('Altered!', $this->router->defaultCallable($matches, $data, '.*$')); + $this->assertEquals(['Altered', 'parameter1', 'parameter2'], $this->router->defaultCallable($matches, $data, '.*$')); } /* route() ------------------------------------------------------------ */ diff --git a/test/views/view.test.testdefaultcallablechangemethod.php b/test/views/view.test.testdefaultcallablechangemethod.php index 3533d0e..6943eac 100644 --- a/test/views/view.test.testdefaultcallablechangemethod.php +++ b/test/views/view.test.testdefaultcallablechangemethod.php @@ -45,8 +45,8 @@ class TestDefaultCallableChangeMethodTestView extends View return "Not altered!"; } - public function altered() + public function altered(string $param1, string $param2) { - return "Altered!"; + return ["Altered", $param1, $param2]; } } \ No newline at end of file