From 4b033fedd86feb5b3af2bc43aa813f2e2a7e5104 Mon Sep 17 00:00:00 2001 From: Daniel Coulbourne Date: Mon, 16 Apr 2018 04:36:06 -0400 Subject: [PATCH 1/2] Cause ResourceRegistrar::register() to return a RouteCollection. --- src/Illuminate/Routing/ResourceRegistrar.php | 8 ++++++-- tests/Routing/RouteRegistrarTest.php | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Routing/ResourceRegistrar.php b/src/Illuminate/Routing/ResourceRegistrar.php index 5209e9e9279a..d1ec79fc1ae9 100644 --- a/src/Illuminate/Routing/ResourceRegistrar.php +++ b/src/Illuminate/Routing/ResourceRegistrar.php @@ -68,7 +68,7 @@ public function __construct(Router $router) * @param string $name * @param string $controller * @param array $options - * @return void + * @return \Illuminate\Routing\RouteCollection */ public function register($name, $controller, array $options = []) { @@ -92,9 +92,13 @@ public function register($name, $controller, array $options = []) $defaults = $this->resourceDefaults; + $collection = new RouteCollection; + foreach ($this->getResourceMethods($defaults, $options) as $m) { - $this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options); + $collection->add($this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options)); } + + return $collection; } /** diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 3a239ddcb047..feafa47c9e61 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -7,6 +7,7 @@ use Illuminate\Routing\Router; use PHPUnit\Framework\TestCase; use Illuminate\Container\Container; +use Illuminate\Routing\ResourceRegistrar; use Illuminate\Contracts\Events\Dispatcher; class RouteRegistrarTest extends TestCase @@ -219,6 +220,22 @@ public function testCanRegisterResource() $this->seeMiddleware('resource-middleware'); } + public function testCanAccessRegisteredResourceRoutesAsRouteCollection() + { + $registrar = new ResourceRegistrar($this->router); + $resource = $registrar->register('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub'); + + $this->assertCount(7, $resource->getRoutes()); + + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.create')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.store')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.edit')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.update')); + $this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy')); + } + public function testCanLimitMethodsOnRegisteredResource() { $this->router->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub') From 4ba828bdc3ace37920bf6d9f761931750b3b73b7 Mon Sep 17 00:00:00 2001 From: Daniel Coulbourne Date: Mon, 16 Apr 2018 05:00:45 -0400 Subject: [PATCH 2/2] Allow PendingResourceRegistration to be fluently registered, returning a RouteCollection. --- .../Routing/PendingResourceRegistration.php | 23 ++++++++++++++++++- tests/Routing/RouteRegistrarTest.php | 6 ++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index fbb54e8c821a..796b752c1837 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -11,6 +11,13 @@ class PendingResourceRegistration */ protected $registrar; + /** + * The resource's registration status. + * + * @var bool + */ + protected $registered = false; + /** * The resource name. * @@ -142,6 +149,18 @@ public function middleware($middleware) return $this; } + /** + * Register the Resource. + * + * @return \Illuminate\Routing\RouteCollection + */ + public function register() + { + $this->registered = true; + + return $this->registrar->register($this->name, $this->controller, $this->options); + } + /** * Handle the object's destruction. * @@ -149,6 +168,8 @@ public function middleware($middleware) */ public function __destruct() { - $this->registrar->register($this->name, $this->controller, $this->options); + if (! $this->registered) { + $this->register(); + } } } diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index feafa47c9e61..c2a335f7bb8c 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -7,7 +7,6 @@ use Illuminate\Routing\Router; use PHPUnit\Framework\TestCase; use Illuminate\Container\Container; -use Illuminate\Routing\ResourceRegistrar; use Illuminate\Contracts\Events\Dispatcher; class RouteRegistrarTest extends TestCase @@ -222,8 +221,9 @@ public function testCanRegisterResource() public function testCanAccessRegisteredResourceRoutesAsRouteCollection() { - $registrar = new ResourceRegistrar($this->router); - $resource = $registrar->register('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub'); + $resource = $this->router->middleware('resource-middleware') + ->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub') + ->register(); $this->assertCount(7, $resource->getRoutes());