From d8acd801d243f1cc2e653b73bbcaedf669d332b9 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Mon, 11 Feb 2019 19:31:49 +0100 Subject: [PATCH] Implemented routerLoadCallableEvent Fixes #3 --- .../Event/RouterLoadCallableEvent.php | 82 +++++++++++++++++++ src/FuzeWorks/Router.php | 19 ++++- 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/FuzeWorks/Event/RouterLoadCallableEvent.php diff --git a/src/FuzeWorks/Event/RouterLoadCallableEvent.php b/src/FuzeWorks/Event/RouterLoadCallableEvent.php new file mode 100644 index 0000000..f403b72 --- /dev/null +++ b/src/FuzeWorks/Event/RouterLoadCallableEvent.php @@ -0,0 +1,82 @@ + + * @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net) + */ +class RouterLoadCallableEvent extends Event +{ + + /** + * The callable to be loaded + * + * @var callable + */ + public $callable; + + /** + * The matches with which the callable is loaded + * + * @var array + */ + public $matches; + + /** + * The route which resulted in this callable being loaded + * + * @var string + */ + public $route; + + public function init(callable $callable, array $matches, string $route) + { + $this->callable = $callable; + $this->matches = $matches; + $this->route = $route; + } + +} \ No newline at end of file diff --git a/src/FuzeWorks/Router.php b/src/FuzeWorks/Router.php index e697ea3..37fbc01 100644 --- a/src/FuzeWorks/Router.php +++ b/src/FuzeWorks/Router.php @@ -36,6 +36,7 @@ namespace FuzeWorks; +use FuzeWorks\Event\RouterLoadCallableEvent; use FuzeWorks\Event\RouterLoadViewAndControllerEvent; use FuzeWorks\Exception\ConfigException; use FuzeWorks\Exception\ControllerException; @@ -187,6 +188,7 @@ class Router * @param string $path * @return mixed * @throws NotFoundException + * @throws RouterException */ public function route(string $path) { @@ -248,6 +250,7 @@ class Router * @param array $matches * @param string $route * @return mixed + * @throws RouterException */ protected function loadCallable(callable $callable, array $matches, string $route) { @@ -257,10 +260,22 @@ class Router if (!is_int($key)) Logger::log($key.': '.var_export($value, true).''); } - Logger::stopLevel(); + + try { + /** @var RouterLoadCallableEvent $event */ + $event = Events::fireEvent('routerLoadCallableEvent', + $callable, + $matches, + $route + ); + } catch (EventException $e) { + throw new RouterException("Could not load callable. routerLoadCallableEvent threw exception: '".$e->getMessage()."'"); + } // Invoke callable - return call_user_func_array($callable, [$matches, $route]); + $output = call_user_func_array($event->callable, [$event->matches, $event->route]); + Logger::stopLevel(); + return $output; } /**