Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handler method resolver #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions src/Phroute/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Dispatcher {
private $variableRouteData;
private $filters;
private $handlerResolver;
private $handlerMethodResolver;
public $matchedRoute;

/**
Expand All @@ -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();
Expand All @@ -33,6 +34,15 @@ public function __construct(RouteDataInterface $data, HandlerResolverInterface $
{
$this->handlerResolver = $resolver;
}

if(null === $methodResolver)
{
$this->handlerMethodResolver = new HandlerMethodResolver();
}
else
{
$this->handlerMethodResolver = $methodResolver;
}
}

/**
Expand All @@ -52,10 +62,10 @@ public function dispatch($httpMethod, $uri)
{
return $response;
}

$resolvedHandler = $this->handlerResolver->resolve($handler);
$response = call_user_func_array($resolvedHandler, $vars);

$response = $this->handlerMethodResolver->resolve($resolvedHandler, $vars);

return $this->dispatchFilters($afterFilter, $response);
}
Expand All @@ -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;
}

Expand All @@ -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]));
Expand All @@ -102,7 +112,7 @@ private function parseFilters($filters)
{
$afterFilter = array_intersect_key($this->filters, array_flip((array) $filters[Route::AFTER]));
}

return array($beforeFilter, $afterFilter);
}

Expand All @@ -119,7 +129,7 @@ private function dispatchRoute($httpMethod, $uri)
{
return $this->dispatchStaticRoute($httpMethod, $uri);
}

return $this->dispatchVariableRoute($httpMethod, $uri);
}

Expand All @@ -139,7 +149,7 @@ private function dispatchStaticRoute($httpMethod, $uri)
{
$httpMethod = $this->checkFallbacks($routes, $httpMethod);
}

return $routes[$httpMethod];
}

Expand All @@ -153,22 +163,22 @@ 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]))
{
return $method;
}
}

$this->matchedRoute = $routes;

throw new HttpMethodNotAllowedException('Allow: ' . implode(', ', array_keys($routes)));
}

Expand All @@ -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))
{
Expand All @@ -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)
{
Expand Down
25 changes: 25 additions & 0 deletions src/Phroute/HandlerMethodResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Created by phroute.
* Date: 8/25/2017
* Time: 11:13 PM
*/

namespace Phroute\Phroute;


class HandlerMethodResolver implements HandlerMethodResolverInterface
{

/**
* Call method from instance of the given handler.
*
* @param callable $handler
* @param array $arguments
* @return mixed
*/
public function resolve($handler, $arguments)
{
return call_user_func_array($handler, $arguments);
}
}
23 changes: 23 additions & 0 deletions src/Phroute/HandlerMethodResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Created by phroute.
* Date: 8/25/2017
* Time: 11:10 PM
*/

namespace Phroute\Phroute;


interface HandlerMethodResolverInterface
{

/**
* Call handler function.
*
* @param callable $handler
* @param array $arguments
* @return mixed
*/

public function resolve($handler, $arguments);
}