Skip to content

Commit

Permalink
Moved the resolveHandler method to a seperate class HandlerResolver. The
Browse files Browse the repository at this point in the history
Dispatch class depends on the HandlerResolverInterface. This allows to
people to implement their own resolvers. Basically this allows to use
Phroute together with a IoC Container
  • Loading branch information
frederikbosch committed Oct 7, 2014
1 parent 54fea04 commit d078b6d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/Phroute/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,28 @@ class Dispatcher {
private $staticRouteMap;
private $variableRouteData;
private $filters;
private $handlerResolver;
public $matchedRoute;

/**
* Create a new route dispatcher.
*
* @param RouteCollector $data
*/
public function __construct(RouteCollector $data)
public function __construct(RouteCollector $data, HandlerResolverInterface $resolver = null)
{
list($this->staticRouteMap, $this->variableRouteData) = $data->getData();

$this->filters = $data->getFilters();

if ($resolver === null)
{
$this->handlerResolver = new HandlerResolver();
}
else
{
$this->handlerResolver = $resolver;
}
}

/**
Expand All @@ -40,29 +50,13 @@ public function dispatch($httpMethod, $uri)
return $response;
}

$resolvedHandler = $this->resolveHandler($handler);
$resolvedHandler = $this->handlerResolver->resolve($handler);

$response = call_user_func_array($resolvedHandler, $vars);

return $this->dispatchFilters($afterFilter, $response);
}

/**
* Create an instance of the given filter handler.
*
* @param $handler
* @return array
*/
private function resolveHandler($handler)
{
if(is_array($handler) and is_string($handler[0]))
{
$handler[0] = new $handler[0];
}

return $handler;
}

/**
* Dispatch a route filter.
*
Expand All @@ -74,7 +68,9 @@ private function dispatchFilters($filters, $response = null)
{
while($filter = array_shift($filters))
{
if(($filteredResponse = call_user_func($this->resolveHandler($filter), $response)) !== null)
$handler = $this->handlerResolver->resolve($filter);

if(($filteredResponse = call_user_func($handler, $response)) !== null)
{
return $filteredResponse;
}
Expand Down
22 changes: 22 additions & 0 deletions src/Phroute/HandlerResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace Phroute;

class HandlerResolver implements HandlerResolverInterface {

/**
* Create an instance of the given handler.
*
* @param $handler
* @return array
*/

public function resolve ($handler)
{
if(is_array($handler) and is_string($handler[0]))
{
$handler[0] = new $handler[0];
}

return $handler;
}
}
14 changes: 14 additions & 0 deletions src/Phroute/HandlerResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Phroute;

interface HandlerResolverInterface {

/**
* Create an instance of the given handler.
*
* @param $handler
* @return array
*/

public function resolve($handler);
}

0 comments on commit d078b6d

Please sign in to comment.