From eabe5f04aee66d004cd153e5c1578a347d69f567 Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Sat, 29 Aug 2015 16:04:47 -0500 Subject: [PATCH 1/5] Added the any() method. This method is useful for defining a single middleware that will respond to any of the $httpRouteMethods. For example, a Restful controller that has logic to route to the appropriate method internally: $app->any('/foo', Api\RestfulController::class); Remove any check --- src/Application.php | 11 +++++++++++ test/ApplicationTest.php | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Application.php b/src/Application.php index 07fb8bc5..22c99015 100644 --- a/src/Application.php +++ b/src/Application.php @@ -155,6 +155,17 @@ public function __call($method, $args) return call_user_func_array([$this, 'route'], $args); } + /** + * @param string|Router\Route $path + * @param callable|string $middleware Middleware (or middleware service name) to associate with route. + * @param null|string $name the name of the route + * @return Router\Route + */ + public function any($path, $middleware, $name = null) + { + return $this->route($path, $middleware, $this->httpRouteMethods, $name); + } + /** * Overload pipe() operation. * diff --git a/test/ApplicationTest.php b/test/ApplicationTest.php index 2f9c4ebc..67ad48c4 100644 --- a/test/ApplicationTest.php +++ b/test/ApplicationTest.php @@ -68,6 +68,26 @@ public function testRouteMethodReturnsRouteInstance() $this->assertSame($this->noopMiddleware, $route->getMiddleware()); } + public function testAnyRouteMethod() + { + $this->router->addRoute(Argument::type(Route::class))->shouldBeCalled(); + $route = $this->getApp()->any('/foo', $this->noopMiddleware); + $this->assertInstanceOf(Route::class, $route); + $this->assertEquals('/foo', $route->getPath()); + $this->assertSame($this->noopMiddleware, $route->getMiddleware()); + + $reflClass = new \ReflectionClass($this->getApp()); + $routeMethods = $reflClass->getProperty('httpRouteMethods'); + $routeMethods->setAccessible(true); + $routeMethods = $routeMethods->getValue($this->getApp()); + + foreach ($routeMethods as $method) { + $this->assertTrue($route->allowsMethod($method)); + } + + $this->assertSame($routeMethods, $route->getAllowedMethods()); + } + /** * @dataProvider commonHttpMethods */ From 64c7b5fb0b2c44dc223c36198892b8982499c8dd Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Wed, 2 Sep 2015 09:14:32 -0500 Subject: [PATCH 2/5] Update application.md --- doc/book/application.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/book/application.md b/doc/book/application.md index f6341c1f..53e2d291 100644 --- a/doc/book/application.md +++ b/doc/book/application.md @@ -116,9 +116,9 @@ where: This method is typically only used if you want a single middleware to handle multiple HTTP request methods. -### get(), post(), put(), patch(), delete() +### get(), post(), put(), patch(), delete(), any() -Each of the methods `get()`, `post()`, `put()`, `patch()`, and `delete()` +Each of the methods `get()`, `post()`, `put()`, `patch()`, `delete()`, `any()` proxies to `route()` and has the signature: ```php From 390a86f5a4ad7d6b691b1f1280b9d610f5a506fe Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Wed, 2 Sep 2015 09:16:30 -0500 Subject: [PATCH 3/5] Update application.md --- doc/book/application.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/book/application.md b/doc/book/application.md index 53e2d291..c2696834 100644 --- a/doc/book/application.md +++ b/doc/book/application.md @@ -118,7 +118,7 @@ multiple HTTP request methods. ### get(), post(), put(), patch(), delete(), any() -Each of the methods `get()`, `post()`, `put()`, `patch()`, `delete()`, `any()` +Each of the methods `get()`, `post()`, `put()`, `patch()`, `delete()`, and `any()` proxies to `route()` and has the signature: ```php From 5f4a8659013c3821431b7eb64d3184d90f4233d8 Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Wed, 2 Sep 2015 09:28:10 -0500 Subject: [PATCH 4/5] Update Application.php --- src/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.php b/src/Application.php index 22c99015..902d3978 100644 --- a/src/Application.php +++ b/src/Application.php @@ -163,7 +163,7 @@ public function __call($method, $args) */ public function any($path, $middleware, $name = null) { - return $this->route($path, $middleware, $this->httpRouteMethods, $name); + return $this->route($path, $middleware, Router\Route::HTTP_METHOD_ANY, $name); } /** From cab19e5f5b1ce97ea5d99309340c70d9d34dbfbd Mon Sep 17 00:00:00 2001 From: Kyle Spraggs Date: Wed, 2 Sep 2015 09:54:41 -0500 Subject: [PATCH 5/5] Update Application.php --- src/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application.php b/src/Application.php index 902d3978..eaebb027 100644 --- a/src/Application.php +++ b/src/Application.php @@ -163,7 +163,7 @@ public function __call($method, $args) */ public function any($path, $middleware, $name = null) { - return $this->route($path, $middleware, Router\Route::HTTP_METHOD_ANY, $name); + return $this->route($path, $middleware, null, $name); } /**