Skip to content

Commit

Permalink
Allow PendingResourceRegistration to be fluently registered, returnin…
Browse files Browse the repository at this point in the history
…g a RouteCollection.
  • Loading branch information
Daniel Coulbourne committed Apr 16, 2018
1 parent 4b033fe commit 368fd25
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
22 changes: 21 additions & 1 deletion src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class PendingResourceRegistration
*/
protected $registrar;

/**
* The resource's registration status.
*
* @var bool
*/
protected $registered = false;

/**
* The resource name.
*
Expand Down Expand Up @@ -142,13 +149,26 @@ 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.
*
* @return void
*/
public function __destruct()
{
$this->registrar->register($this->name, $this->controller, $this->options);
if (! $this->registered) {
$this->register();
}
}
}
6 changes: 3 additions & 3 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 368fd25

Please sign in to comment.