From 368fd2576b8c5d902ad85da22e158ebe953fd741 Mon Sep 17 00:00:00 2001 From: Daniel Coulbourne Date: Mon, 16 Apr 2018 05:00:45 -0400 Subject: [PATCH] Allow PendingResourceRegistration to be fluently registered, returning a RouteCollection. --- .../Routing/PendingResourceRegistration.php | 22 ++++++++++++++++++- tests/Routing/RouteRegistrarTest.php | 6 ++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index fbb54e8c821a..d0d52e55b7bc 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,17 @@ 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 +167,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());