From 877cac9ec54f190284074020612c679776d9cdca Mon Sep 17 00:00:00 2001 From: mnlg Date: Thu, 18 Oct 2012 20:54:22 +0200 Subject: [PATCH 1/3] move dispatch method to \Slim\Route --- Slim/Route.php | 16 +++++++++++----- Slim/Router.php | 12 ------------ Slim/Slim.php | 2 +- tests/RouteTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Slim/Route.php b/Slim/Route.php index 7f527a156..13259a7d2 100644 --- a/Slim/Route.php +++ b/Slim/Route.php @@ -415,15 +415,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->getMiddleware() 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; } } diff --git a/Slim/Router.php b/Slim/Router.php index aa0f48e76..2e0a4e5c9 100644 --- a/Slim/Router.php +++ b/Slim/Router.php @@ -149,18 +149,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 diff --git a/Slim/Slim.php b/Slim/Slim.php index 498ce38bf..6861907eb 100644 --- a/Slim/Slim.php +++ b/Slim/Slim.php @@ -1222,7 +1222,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; diff --git a/tests/RouteTest.php b/tests/RouteTest.php index 26ff2facf..3f7fb8dbe 100644 --- a/tests/RouteTest.php +++ b/tests/RouteTest.php @@ -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(); } } From f71dbdd4d0c1408a2805bb0e6aebbc001db41db1 Mon Sep 17 00:00:00 2001 From: mnlg Date: Wed, 3 Apr 2013 20:35:11 +0200 Subject: [PATCH 2/3] remove Router dispatch test --- tests/RouterTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/RouterTest.php b/tests/RouterTest.php index c645d5f3f..a8969ba28 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -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 */ From 2639450b960f21d1960e76d392a08b43c669299c Mon Sep 17 00:00:00 2001 From: mnlg Date: Wed, 3 Apr 2013 20:42:07 +0200 Subject: [PATCH 3/3] get the middleware from the class property instead of using the getMiddleware method --- Slim/Route.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Slim/Route.php b/Slim/Route.php index 13259a7d2..032080049 100644 --- a/Slim/Route.php +++ b/Slim/Route.php @@ -425,7 +425,7 @@ public function conditions(array $conditions) */ public function dispatch() { - foreach ($this->getMiddleware() as $mw) { + foreach ($this->middleware as $mw) { call_user_func_array($mw, array($this)); }