Skip to content

Commit

Permalink
Merge pull request #450 from mnlg/MoveDispatchToRoute
Browse files Browse the repository at this point in the history
move dispatch method to \Slim\Route
  • Loading branch information
Josh Lockhart committed Jun 4, 2013
2 parents 555d3af + 2639450 commit 72f0c78
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
16 changes: 11 additions & 5 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,21 @@ public function conditions(array $conditions)
}

/**
* Dispatch
* @return mixed The return value of the route callable, or FALSE on error
* Dispatch route
*
* This method invokes the route object's callable. If middleware is
* registered for the route, each callable middleware is invoked in
* the order specified.
*
* @return bool
*/
public function dispatch()
{
foreach ($this->middleware as $routeMiddleware) {
call_user_func_array($routeMiddleware, array($this));
foreach ($this->middleware as $mw) {
call_user_func_array($mw, array($this));
}

return call_user_func_array($this->callable, array_values($this->params));
$return = call_user_func_array($this->getCallable(), array_values($this->getParams()));
return ($return === false)? false : true;
}
}
12 changes: 0 additions & 12 deletions Slim/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,6 @@ public function urlFor($name, $params = array())
return preg_replace('#\(/?:.+\)|\(|\)#', '', $pattern);
}

/**
* Dispatch route
* @param \Slim\Route $route The route to dispatch
* @return bool True if route dispatched successfully, else false
*/
public function dispatch(\Slim\Route $route)
{
$this->currentRoute = $route;

return ($route->dispatch() !== false);
}

/**
* Add named route
* @param string $name The route name
Expand Down
2 changes: 1 addition & 1 deletion Slim/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ public function call()
foreach ($matchedRoutes as $route) {
try {
$this->applyHook('slim.before.dispatch');
$dispatched = $this->router->dispatch($route);
$dispatched = $route->dispatch();
$this->applyHook('slim.after.dispatch');
if ($dispatched) {
break;
Expand Down
38 changes: 38 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,46 @@ public function testSupportsHttpMethod()
$this->assertFalse($route->supportsHttpMethod('PUT'));
}

/**
* Test dispatch with params
*/
public function testDispatch()
{
$this->expectOutputString('Hello josh');
$route = new \Slim\Route('/hello/:name', function ($name) { echo "Hello $name"; });
$route->matches('/hello/josh'); //<-- Extracts params from resource URI
$route->dispatch();
}

/**
* Test dispatch with middleware
*/
public function testDispatchWithMiddlware()
{
$this->expectOutputString('First! Second! Hello josh');
$route = new \Slim\Route('/hello/:name', function ($name) { echo "Hello $name"; });
$route->setMiddleware(function () {
echo "First! ";
});
$route->setMiddleware(function () {
echo "Second! ";
});
$route->matches('/hello/josh'); //<-- Extracts params from resource URI
$route->dispatch();
}

/**
* Test middleware with arguments
*/
public function testRouteMiddlwareArguments()
{
$this->expectOutputString('foobar');
$route = new \Slim\Route('/foo', function () { echo "bar"; });
$route->setName('foo');
$route->setMiddleware(function ($route) {
echo $route->getName();
});
$route->matches('/foo'); //<-- Extracts params from resource URI
$route->dispatch();
}
}
12 changes: 0 additions & 12 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,6 @@ public function testHasNamedRoute()
$this->assertFalse($router->hasNamedRoute('bar'));
}

/**
* Router should set current route and dispatch returns non-false value
*/
public function testDispatch()
{
$router = new \Slim\Router();
$route = new \Slim\Route('/foo', function () {});

$this->assertTrue($router->dispatch($route) !== false);
$this->assertAttributeSame($route, 'currentRoute', $router);
}

/**
* Router should return current route if set during iteration
*/
Expand Down

0 comments on commit 72f0c78

Please sign in to comment.