From acf16af2cbddffe5d7dc9a12d80a16c6e9d5fcf5 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Tue, 16 Aug 2022 21:16:52 +0100 Subject: [PATCH 1/2] Add supper for additional where* methods to route groups --- src/Illuminate/Routing/RouteRegistrar.php | 2 + src/Illuminate/Routing/Router.php | 4 + tests/Routing/RouteRegistrarTest.php | 136 ++++++++++++++++++++++ 3 files changed, 142 insertions(+) diff --git a/src/Illuminate/Routing/RouteRegistrar.php b/src/Illuminate/Routing/RouteRegistrar.php index 64c1359bd61d..b5c1a58af2e0 100644 --- a/src/Illuminate/Routing/RouteRegistrar.php +++ b/src/Illuminate/Routing/RouteRegistrar.php @@ -29,6 +29,8 @@ */ class RouteRegistrar { + use CreatesRegularExpressionRouteConstraints; + /** * The router instance. * diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index a711668f8854..ababf351ada0 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -1367,6 +1367,10 @@ public function __call($method, $parameters) return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters); } + if ($method !== 'where' && Str::startsWith($method, 'where')) { + return (new RouteRegistrar($this))->{$method}(...$parameters); + } + return (new RouteRegistrar($this))->attribute($method, array_key_exists(0, $parameters) ? $parameters[0] : true); } } diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index d94d3011b985..5b4f40294245 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -864,6 +864,142 @@ public function testWhereInRegistration() } } + public function testGroupWhereNumberRegistrationOnRouteRegistrar() + { + $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; + + $this->router->prefix('/{foo}/{bar}')->whereNumber(['foo', 'bar'])->group(function($router) { + $router->get('/'); + }); + + $this->router->prefix('/api/{bar}/{foo}')->whereNumber(['bar', 'foo'])->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereAlphaRegistrationOnRouteRegistrar() + { + $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; + + $this->router->prefix('/{foo}/{bar}')->whereAlpha(['foo', 'bar'])->group(function($router) { + $router->get('/'); + }); + + $this->router->prefix('/api/{bar}/{foo}')->whereAlpha(['bar', 'foo'])->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereAlphaNumericRegistrationOnRouteRegistrar() + { + $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; + + $this->router->prefix('/{foo}')->whereAlphaNumeric(['1a2b3c'])->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereInRegistrationOnRouteRegistrar() + { + $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; + + $this->router->prefix('/{foo}/{bar}')->whereIn(['foo', 'bar'], ['one', 'two'])->group(function($router) { + $router->get('/'); + }); + + $this->router->prefix('/api/{bar}/{foo}')->whereIn(['bar', 'foo'], ['one', 'two'])->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereNumberRegistrationOnRouter() + { + $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; + + $this->router->whereNumber(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function($router) { + $router->get('/'); + }); + + $this->router->whereNumber(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereAlphaRegistrationOnRouter() + { + $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; + + $this->router->whereAlpha(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function($router) { + $router->get('/'); + }); + + $this->router->whereAlpha(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereAlphaNumericRegistrationOnRouter() + { + $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; + + $this->router->whereAlphaNumeric(['1a2b3c'])->prefix('/{foo}')->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + + public function testGroupWhereInRegistrationOnRouter() + { + $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; + + $this->router->whereIn(['foo', 'bar'], ['one', 'two'])->prefix('/{foo}/{bar}')->group(function($router) { + $router->get('/'); + }); + + $this->router->whereIn(['bar', 'foo'], ['one', 'two'])->prefix('/api/{bar}/{foo}')->group(function($router) { + $router->get('/'); + }); + + /** @var \Illuminate\Routing\Route $route */ + foreach ($this->router->getRoutes() as $route) { + $this->assertEquals($wheres, $route->wheres); + } + } + public function testCanSetRouteName() { $this->router->as('users.index')->get('users', function () { From 279981045409e1549fcc6de9d14370b06c9dcc79 Mon Sep 17 00:00:00 2001 From: Ollie Read Date: Tue, 16 Aug 2022 21:23:19 +0100 Subject: [PATCH 2/2] Fix styling --- tests/Routing/RouteRegistrarTest.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 5b4f40294245..e585e77a1a0c 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -868,11 +868,11 @@ public function testGroupWhereNumberRegistrationOnRouteRegistrar() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; - $this->router->prefix('/{foo}/{bar}')->whereNumber(['foo', 'bar'])->group(function($router) { + $this->router->prefix('/{foo}/{bar}')->whereNumber(['foo', 'bar'])->group(function ($router) { $router->get('/'); }); - $this->router->prefix('/api/{bar}/{foo}')->whereNumber(['bar', 'foo'])->group(function($router) { + $this->router->prefix('/api/{bar}/{foo}')->whereNumber(['bar', 'foo'])->group(function ($router) { $router->get('/'); }); @@ -886,11 +886,11 @@ public function testGroupWhereAlphaRegistrationOnRouteRegistrar() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; - $this->router->prefix('/{foo}/{bar}')->whereAlpha(['foo', 'bar'])->group(function($router) { + $this->router->prefix('/{foo}/{bar}')->whereAlpha(['foo', 'bar'])->group(function ($router) { $router->get('/'); }); - $this->router->prefix('/api/{bar}/{foo}')->whereAlpha(['bar', 'foo'])->group(function($router) { + $this->router->prefix('/api/{bar}/{foo}')->whereAlpha(['bar', 'foo'])->group(function ($router) { $router->get('/'); }); @@ -904,7 +904,7 @@ public function testGroupWhereAlphaNumericRegistrationOnRouteRegistrar() { $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; - $this->router->prefix('/{foo}')->whereAlphaNumeric(['1a2b3c'])->group(function($router) { + $this->router->prefix('/{foo}')->whereAlphaNumeric(['1a2b3c'])->group(function ($router) { $router->get('/'); }); @@ -918,11 +918,11 @@ public function testGroupWhereInRegistrationOnRouteRegistrar() { $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; - $this->router->prefix('/{foo}/{bar}')->whereIn(['foo', 'bar'], ['one', 'two'])->group(function($router) { + $this->router->prefix('/{foo}/{bar}')->whereIn(['foo', 'bar'], ['one', 'two'])->group(function ($router) { $router->get('/'); }); - $this->router->prefix('/api/{bar}/{foo}')->whereIn(['bar', 'foo'], ['one', 'two'])->group(function($router) { + $this->router->prefix('/api/{bar}/{foo}')->whereIn(['bar', 'foo'], ['one', 'two'])->group(function ($router) { $router->get('/'); }); @@ -936,11 +936,11 @@ public function testGroupWhereNumberRegistrationOnRouter() { $wheres = ['foo' => '[0-9]+', 'bar' => '[0-9]+']; - $this->router->whereNumber(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function($router) { + $this->router->whereNumber(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function ($router) { $router->get('/'); }); - $this->router->whereNumber(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function($router) { + $this->router->whereNumber(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function ($router) { $router->get('/'); }); @@ -954,11 +954,11 @@ public function testGroupWhereAlphaRegistrationOnRouter() { $wheres = ['foo' => '[a-zA-Z]+', 'bar' => '[a-zA-Z]+']; - $this->router->whereAlpha(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function($router) { + $this->router->whereAlpha(['foo', 'bar'])->prefix('/{foo}/{bar}')->group(function ($router) { $router->get('/'); }); - $this->router->whereAlpha(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function($router) { + $this->router->whereAlpha(['bar', 'foo'])->prefix('/api/{bar}/{foo}')->group(function ($router) { $router->get('/'); }); @@ -972,7 +972,7 @@ public function testGroupWhereAlphaNumericRegistrationOnRouter() { $wheres = ['1a2b3c' => '[a-zA-Z0-9]+']; - $this->router->whereAlphaNumeric(['1a2b3c'])->prefix('/{foo}')->group(function($router) { + $this->router->whereAlphaNumeric(['1a2b3c'])->prefix('/{foo}')->group(function ($router) { $router->get('/'); }); @@ -986,11 +986,11 @@ public function testGroupWhereInRegistrationOnRouter() { $wheres = ['foo' => 'one|two', 'bar' => 'one|two']; - $this->router->whereIn(['foo', 'bar'], ['one', 'two'])->prefix('/{foo}/{bar}')->group(function($router) { + $this->router->whereIn(['foo', 'bar'], ['one', 'two'])->prefix('/{foo}/{bar}')->group(function ($router) { $router->get('/'); }); - $this->router->whereIn(['bar', 'foo'], ['one', 'two'])->prefix('/api/{bar}/{foo}')->group(function($router) { + $this->router->whereIn(['bar', 'foo'], ['one', 'two'])->prefix('/api/{bar}/{foo}')->group(function ($router) { $router->get('/'); });