From 60202afc19da6a29eaac42cd201aaa488e90d21a Mon Sep 17 00:00:00 2001 From: Jonas Delrue Date: Sun, 4 Sep 2016 10:41:54 +0200 Subject: [PATCH] adding selectors to PhRoute --- src/Phroute/Dispatcher.php | 32 ++++++++++++++++++++++++++- src/Phroute/RouteCollector.php | 1 + src/Phroute/RouteParser.php | 10 +++++++-- test/Dispatcher/DispatcherTest.php | 5 +++++ test/Dispatcher/SelectorTest.php | 35 ++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 test/Dispatcher/SelectorTest.php diff --git a/src/Phroute/Dispatcher.php b/src/Phroute/Dispatcher.php index 6e637e5..3d5cd29 100644 --- a/src/Phroute/Dispatcher.php +++ b/src/Phroute/Dispatcher.php @@ -44,6 +44,8 @@ public function __construct(RouteDataInterface $data, HandlerResolverInterface $ */ public function dispatch($httpMethod, $uri) { + list($uri, $selectors) = $this->explodeSelectors($uri); + list($handler, $filters, $vars) = $this->dispatchRoute($httpMethod, trim($uri, '/')); list($beforeFilter, $afterFilter) = $this->parseFilters($filters); @@ -54,12 +56,38 @@ public function dispatch($httpMethod, $uri) } $resolvedHandler = $this->handlerResolver->resolve($handler); - + array_push ($vars, $selectors); $response = call_user_func_array($resolvedHandler, $vars); return $this->dispatchFilters($afterFilter, $response); } + /** + * Add selectors (eg. ?deleted=true to called function as argument) + * + * @param $uri + * @return array|url, array of selectors + */ + private function explodeSelectors($uri){ + if(strpos($uri,'?') !== false){ + list($url, $selectors) = explode("?", $_SERVER['REQUEST_URI']); + + $arrayOfFilters = explode("&", $selectors); + $asArrayOfFilters = Array(); + for( $i=0; $i $varName) { + if(!isset($matches[$i + 1]) || $matches[$i + 1] === '') { unset($routes[$httpMethod][2][$varName]); diff --git a/src/Phroute/RouteCollector.php b/src/Phroute/RouteCollector.php index 25aba08..7b8e336 100644 --- a/src/Phroute/RouteCollector.php +++ b/src/Phroute/RouteCollector.php @@ -169,6 +169,7 @@ private function addStaticRoute($httpMethod, $routeData, $handler, $filters) */ private function addVariableRoute($httpMethod, $routeData, $handler, $filters) { + list($regex, $variables) = $routeData; if (isset($this->regexToRoutesMap[$regex][$httpMethod])) diff --git a/src/Phroute/RouteParser.php b/src/Phroute/RouteParser.php index 0f25274..ed30bc7 100644 --- a/src/Phroute/RouteParser.php +++ b/src/Phroute/RouteParser.php @@ -59,10 +59,17 @@ class RouteParser { * Parse a route returning the correct data format to pass to the dispatch engine. * * @param $route - * @return array + * @return ? */ public function parse($route) { + if (strpos($route, '{selectors}') == false) { + $route .= '/{selectors}?'; + }else if (strpos($route, '{selectors}?') == false) { + $route = str_replace('{selectors}', '{selectors}?', $route); + } + + $this->reset(); $route = strtr($route, $this->regexShortcuts); @@ -106,7 +113,6 @@ public function parse($route) } $this->staticParts($route, strlen($route)); - return [[implode('', $this->parts), $this->variables], array_values($this->reverseParts)]; } diff --git a/test/Dispatcher/DispatcherTest.php b/test/Dispatcher/DispatcherTest.php index 720c2c1..f53e08d 100644 --- a/test/Dispatcher/DispatcherTest.php +++ b/test/Dispatcher/DispatcherTest.php @@ -1,5 +1,7 @@ controller('/selectors', 'Selectors'); + + + + $dispatcher = new Dispatcher($router->getData()); + + + // Todo code should be part of phroute... + $url = $_SERVER['REQUEST_URI']; + + + $response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $url); + + echo $response; +?>