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; } /**