From 7c3867fa6703ed49cfae623d8b1702f5af684ccd Mon Sep 17 00:00:00 2001 From: Stephan van Diepen Date: Thu, 23 Jan 2020 13:02:54 +0800 Subject: [PATCH 1/3] Add shallow option for resource routes --- .../Routing/PendingResourceRegistration.php | 13 +++++++ src/Illuminate/Routing/ResourceRegistrar.php | 25 +++++++++++++ tests/Routing/RouteRegistrarTest.php | 11 ++++++ tests/Routing/RoutingRouteTest.php | 35 +++++++++++++++++++ 4 files changed, 84 insertions(+) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index f4ec5bc09d4f..8ffa4cce79ff 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -153,6 +153,19 @@ public function middleware($middleware) return $this; } + /** + * Set the shallow option for a resource. + * + * @param boolean $shallow + * @return \Illuminate\Routing\PendingResourceRegistration + */ + public function shallow($shallow = true) + { + $this->options['shallow'] = $shallow; + + return $this; + } + /** * Register the resource route. * diff --git a/src/Illuminate/Routing/ResourceRegistrar.php b/src/Illuminate/Routing/ResourceRegistrar.php index 58c9ec831813..cb4ab9df3db1 100644 --- a/src/Illuminate/Routing/ResourceRegistrar.php +++ b/src/Illuminate/Routing/ResourceRegistrar.php @@ -230,6 +230,9 @@ protected function addResourceStore($name, $base, $controller, $options) */ protected function addResourceShow($name, $base, $controller, $options) { + + $name = $this->getNameWithShallowness($name, $options); + $uri = $this->getResourceUri($name).'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'show', $options); @@ -248,6 +251,8 @@ protected function addResourceShow($name, $base, $controller, $options) */ protected function addResourceEdit($name, $base, $controller, $options) { + $name = $this->getNameWithShallowness($name, $options); + $uri = $this->getResourceUri($name).'/{'.$base.'}/'.static::$verbs['edit']; $action = $this->getResourceAction($name, $controller, 'edit', $options); @@ -266,6 +271,8 @@ protected function addResourceEdit($name, $base, $controller, $options) */ protected function addResourceUpdate($name, $base, $controller, $options) { + $name = $this->getNameWithShallowness($name, $options); + $uri = $this->getResourceUri($name).'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'update', $options); @@ -284,6 +291,8 @@ protected function addResourceUpdate($name, $base, $controller, $options) */ protected function addResourceDestroy($name, $base, $controller, $options) { + $name = $this->getNameWithShallowness($name, $options); + $uri = $this->getResourceUri($name).'/{'.$base.'}'; $action = $this->getResourceAction($name, $controller, 'destroy', $options); @@ -401,6 +410,22 @@ protected function getResourceRouteName($resource, $method, $options) return trim(sprintf('%s%s.%s', $prefix, $name, $method), '.'); } + /** + * Get the name for a given resource with shallowness applied when needed. + * + * @param string $name + * @param array $options + * @return string + */ + protected function getNameWithShallowness($name, $options) + { + if (isset($options['shallow']) && $options['shallow']) { + return last(explode(".", $name)); + } else { + return $name; + } + } + /** * Set or unset the unmapped global parameters to singular. * diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 848c7154a3dc..eeb153a18724 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -335,6 +335,17 @@ public function testCanExcludeMethodsOnRegisteredResource() $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy')); } + public function testCanSetShallowOptionOnRegisteredResource() + { + $this->router->resource('users.tasks', RouteRegistrarControllerStub::class)->shallow(); + + $this->assertCount(7, $this->router->getRoutes()); + + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.tasks.index')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('tasks.show')); + $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.tasks.show')); + } + public function testCanExcludeMethodsOnRegisteredApiResource() { $this->router->apiResource('users', RouteRegistrarControllerStub::class) diff --git a/tests/Routing/RoutingRouteTest.php b/tests/Routing/RoutingRouteTest.php index 8bf68384f26f..8064df2792e1 100644 --- a/tests/Routing/RoutingRouteTest.php +++ b/tests/Routing/RoutingRouteTest.php @@ -1182,6 +1182,41 @@ public function testInvalidActionException() $router->dispatch(Request::create('/')); } + public function testShallowResourceRouting() + { + $router = $this->getRouter(); + $router->resource('foo.bar', 'FooController', ['shallow' => true]); + $routes = $router->getRoutes(); + $routes = $routes->getRoutes(); + + $this->assertSame('foo/{foo}/bar', $routes[0]->uri()); + $this->assertSame('foo/{foo}/bar/create', $routes[1]->uri()); + $this->assertSame('foo/{foo}/bar', $routes[2]->uri()); + + $this->assertSame('bar/{bar}', $routes[3]->uri()); + $this->assertSame('bar/{bar}/edit', $routes[4]->uri()); + $this->assertSame('bar/{bar}', $routes[5]->uri()); + $this->assertSame('bar/{bar}', $routes[6]->uri()); + + $router = $this->getRouter(); + $router->resource('foo', 'FooController'); + $router->resource('foo.bar.baz', 'FooController', ['shallow' => true]); + $routes = $router->getRoutes(); + $routes = $routes->getRoutes(); + + $this->assertSame('foo', $routes[0]->uri()); + $this->assertSame('foo/create', $routes[1]->uri()); + $this->assertSame('foo', $routes[2]->uri()); + $this->assertSame('foo/{foo}', $routes[3]->uri()); + $this->assertSame('foo/{foo}/edit', $routes[4]->uri()); + $this->assertSame('foo/{foo}', $routes[5]->uri()); + $this->assertSame('foo/{foo}', $routes[6]->uri()); + + $this->assertSame('foo/{foo}/bar/{bar}/baz', $routes[7]->uri()); + $this->assertSame('foo/{foo}/bar/{bar}/baz/create', $routes[8]->uri()); + $this->assertSame('foo/{foo}/bar/{bar}/baz', $routes[9]->uri()); + } + public function testResourceRouting() { $router = $this->getRouter(); From 5cf32e87189f521caabdababc85f76852d7561c0 Mon Sep 17 00:00:00 2001 From: Stephan van Diepen Date: Thu, 23 Jan 2020 13:30:34 +0800 Subject: [PATCH 2/3] Fix styling issues --- src/Illuminate/Routing/PendingResourceRegistration.php | 2 +- src/Illuminate/Routing/ResourceRegistrar.php | 2 +- tests/Routing/RouteRegistrarTest.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 8ffa4cce79ff..79ce47317968 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -156,7 +156,7 @@ public function middleware($middleware) /** * Set the shallow option for a resource. * - * @param boolean $shallow + * @param bool $shallow * @return \Illuminate\Routing\PendingResourceRegistration */ public function shallow($shallow = true) diff --git a/src/Illuminate/Routing/ResourceRegistrar.php b/src/Illuminate/Routing/ResourceRegistrar.php index cb4ab9df3db1..46e514375cf5 100644 --- a/src/Illuminate/Routing/ResourceRegistrar.php +++ b/src/Illuminate/Routing/ResourceRegistrar.php @@ -420,7 +420,7 @@ protected function getResourceRouteName($resource, $method, $options) protected function getNameWithShallowness($name, $options) { if (isset($options['shallow']) && $options['shallow']) { - return last(explode(".", $name)); + return last(explode('.', $name)); } else { return $name; } diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index eeb153a18724..e48d4a4985e7 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -345,7 +345,7 @@ public function testCanSetShallowOptionOnRegisteredResource() $this->assertTrue($this->router->getRoutes()->hasNamedRoute('tasks.show')); $this->assertFalse($this->router->getRoutes()->hasNamedRoute('users.tasks.show')); } - + public function testCanExcludeMethodsOnRegisteredApiResource() { $this->router->apiResource('users', RouteRegistrarControllerStub::class) From c6b201281df66dca8905b2bf662070fc62fe52e1 Mon Sep 17 00:00:00 2001 From: Stephan van Diepen Date: Thu, 23 Jan 2020 13:31:43 +0800 Subject: [PATCH 3/3] Clean up whitespace --- src/Illuminate/Routing/ResourceRegistrar.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Illuminate/Routing/ResourceRegistrar.php b/src/Illuminate/Routing/ResourceRegistrar.php index 46e514375cf5..498aa5ee7370 100644 --- a/src/Illuminate/Routing/ResourceRegistrar.php +++ b/src/Illuminate/Routing/ResourceRegistrar.php @@ -230,7 +230,6 @@ protected function addResourceStore($name, $base, $controller, $options) */ protected function addResourceShow($name, $base, $controller, $options) { - $name = $this->getNameWithShallowness($name, $options); $uri = $this->getResourceUri($name).'/{'.$base.'}';