From 222d0e6f974255c8f13d6478b8616ed573f7e8e8 Mon Sep 17 00:00:00 2001 From: ntdinh1987 Date: Fri, 25 Aug 2017 23:18:11 +0700 Subject: [PATCH 1/2] Handler method resolver --- src/Phroute/Dispatcher.php | 48 +++++++++++-------- src/Phroute/HandlerMethodResolver.php | 25 ++++++++++ .../HandlerMethodResolverInterface.php | 23 +++++++++ 3 files changed, 77 insertions(+), 19 deletions(-) create mode 100644 src/Phroute/HandlerMethodResolver.php create mode 100644 src/Phroute/HandlerMethodResolverInterface.php diff --git a/src/Phroute/Dispatcher.php b/src/Phroute/Dispatcher.php index 6e637e5..744f617 100644 --- a/src/Phroute/Dispatcher.php +++ b/src/Phroute/Dispatcher.php @@ -9,6 +9,7 @@ class Dispatcher { private $variableRouteData; private $filters; private $handlerResolver; + private $handlerMethodResolver; public $matchedRoute; /** @@ -17,14 +18,14 @@ class Dispatcher { * @param RouteDataInterface $data * @param HandlerResolverInterface $resolver */ - public function __construct(RouteDataInterface $data, HandlerResolverInterface $resolver = null) + public function __construct(RouteDataInterface $data, HandlerResolverInterface $resolver = null, HandlerMethodResolverInterface $methodResolver = null) { $this->staticRouteMap = $data->getStaticRoutes(); $this->variableRouteData = $data->getVariableRoutes(); - + $this->filters = $data->getFilters(); - + if ($resolver === null) { $this->handlerResolver = new HandlerResolver(); @@ -33,6 +34,15 @@ public function __construct(RouteDataInterface $data, HandlerResolverInterface $ { $this->handlerResolver = $resolver; } + + if(null === $methodResolver) + { + $this->handlerMethodResolver = new HandlerMethodResolver(); + } + else + { + $this->handlerMethodResolver = $methodResolver; + } } /** @@ -52,9 +62,9 @@ public function dispatch($httpMethod, $uri) { return $response; } - + $resolvedHandler = $this->handlerResolver->resolve($handler); - + $response = call_user_func_array($resolvedHandler, $vars); return $this->dispatchFilters($afterFilter, $response); @@ -72,13 +82,13 @@ private function dispatchFilters($filters, $response = null) while($filter = array_shift($filters)) { $handler = $this->handlerResolver->resolve($filter); - + if(($filteredResponse = call_user_func($handler, $response)) !== null) { return $filteredResponse; } } - + return $response; } @@ -89,10 +99,10 @@ private function dispatchFilters($filters, $response = null) * @return array */ private function parseFilters($filters) - { + { $beforeFilter = array(); $afterFilter = array(); - + if(isset($filters[Route::BEFORE])) { $beforeFilter = array_intersect_key($this->filters, array_flip((array) $filters[Route::BEFORE])); @@ -102,7 +112,7 @@ private function parseFilters($filters) { $afterFilter = array_intersect_key($this->filters, array_flip((array) $filters[Route::AFTER])); } - + return array($beforeFilter, $afterFilter); } @@ -119,7 +129,7 @@ private function dispatchRoute($httpMethod, $uri) { return $this->dispatchStaticRoute($httpMethod, $uri); } - + return $this->dispatchVariableRoute($httpMethod, $uri); } @@ -139,7 +149,7 @@ private function dispatchStaticRoute($httpMethod, $uri) { $httpMethod = $this->checkFallbacks($routes, $httpMethod); } - + return $routes[$httpMethod]; } @@ -153,12 +163,12 @@ private function dispatchStaticRoute($httpMethod, $uri) private function checkFallbacks($routes, $httpMethod) { $additional = array(Route::ANY); - + if($httpMethod === Route::HEAD) { $additional[] = Route::GET; } - + foreach($additional as $method) { if(isset($routes[$method])) @@ -166,9 +176,9 @@ private function checkFallbacks($routes, $httpMethod) return $method; } } - + $this->matchedRoute = $routes; - + throw new HttpMethodNotAllowedException('Allow: ' . implode(', ', array_keys($routes))); } @@ -182,7 +192,7 @@ private function checkFallbacks($routes, $httpMethod) */ private function dispatchVariableRoute($httpMethod, $uri) { - foreach ($this->variableRouteData as $data) + foreach ($this->variableRouteData as $data) { if (!preg_match($data['regex'], $uri, $matches)) { @@ -192,13 +202,13 @@ private function dispatchVariableRoute($httpMethod, $uri) $count = count($matches); while(!isset($data['routeMap'][$count++])); - + $routes = $data['routeMap'][$count - 1]; if (!isset($routes[$httpMethod])) { $httpMethod = $this->checkFallbacks($routes, $httpMethod); - } + } foreach (array_values($routes[$httpMethod][2]) as $i => $varName) { diff --git a/src/Phroute/HandlerMethodResolver.php b/src/Phroute/HandlerMethodResolver.php new file mode 100644 index 0000000..db78baa --- /dev/null +++ b/src/Phroute/HandlerMethodResolver.php @@ -0,0 +1,25 @@ + Date: Fri, 25 Aug 2017 23:33:51 +0700 Subject: [PATCH 2/2] Method resolver with HandlerMethodReolver --- src/Phroute/Dispatcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phroute/Dispatcher.php b/src/Phroute/Dispatcher.php index 744f617..79ec1ea 100644 --- a/src/Phroute/Dispatcher.php +++ b/src/Phroute/Dispatcher.php @@ -65,7 +65,7 @@ public function dispatch($httpMethod, $uri) $resolvedHandler = $this->handlerResolver->resolve($handler); - $response = call_user_func_array($resolvedHandler, $vars); + $response = $this->handlerMethodResolver->resolve($resolvedHandler, $vars); return $this->dispatchFilters($afterFilter, $response); }