From c6b0a02c3b87dd40ba6253e751bf2d6c1d9f818e Mon Sep 17 00:00:00 2001 From: "l.potherat" Date: Fri, 9 Jun 2017 13:26:14 +0200 Subject: [PATCH 1/2] Added shouldDispatchCallback --- src/Router.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Router.php b/src/Router.php index ed3d660..2c9dbd7 100755 --- a/src/Router.php +++ b/src/Router.php @@ -44,6 +44,13 @@ class Router */ private $basePath = ''; + /** + * Callback called before dispatch. Should return the route to dispatch. + * The choosen route for dispatch is the only parameter. + * @var callable + */ + private static $shouldDispatchCallback = null; + /** * @param RouteCollection $collection */ @@ -140,6 +147,17 @@ public function match($requestUrl, $requestMethod = RequestMethodInterface::METH } $routes->setParameters($params); + + $shouldDispatchCallback = static::$shouldDispatchCallback; + //If shouldDispatch callback set, call it before dispatch + if($shouldDispatchCallback !== null){ + $routes = $shouldDispatchCallback($routes); + //if false, ignore the route + if(!$routes){ + continue; + } + } + $routes->dispatch(); return $routes; @@ -209,4 +227,15 @@ public static function parseConfig(array $config) return $router; } + + /** + * Sets a callback before dispatch to allow routing or not. + * The callable takes the dispatching route as first parameter. + * It should return false to skip the route, or a Route in replacement of the passed Route. + * @param callable $shouldDispatchCallback + * @return Router + */ + public static function setShouldDispatchCallback(callable $shouldDispatchCallback){ + static::$shouldDispatchCallback = $shouldDispatchCallback; + } } From 8b1b2e484a8247c6f614b2db92e9dbc3c2dee1bd Mon Sep 17 00:00:00 2001 From: "l.potherat" Date: Fri, 9 Jun 2017 13:35:55 +0200 Subject: [PATCH 2/2] Removed @return since it returns nothing --- src/Router.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Router.php b/src/Router.php index 2c9dbd7..129bf36 100755 --- a/src/Router.php +++ b/src/Router.php @@ -233,7 +233,6 @@ public static function parseConfig(array $config) * The callable takes the dispatching route as first parameter. * It should return false to skip the route, or a Route in replacement of the passed Route. * @param callable $shouldDispatchCallback - * @return Router */ public static function setShouldDispatchCallback(callable $shouldDispatchCallback){ static::$shouldDispatchCallback = $shouldDispatchCallback;