Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Fluent partial resource routes #18393

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Illuminate/Contracts/Routing/Registrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function match($methods, $uri, $action);
* @param string $name
* @param string $controller
* @param array $options
* @return void
* @return \Illuminate\Routing\PendingResourceRegistration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR should target 5.5, because this is a breaking change to the interface, requiring implementations to return this object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A newbie question: does this require any actions by me? My first PR 🥇

*/
public function resource($name, $controller, array $options = []);

Expand Down
67 changes: 67 additions & 0 deletions src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Illuminate\Routing;

class PendingResourceRegistration
{
/**
* The resource registrar.
*
* @var \Illuminate\Routing\ResourceRegistrar
*/
protected $registrar;

/**
* The resource information.
*
* @var array
*/
protected $resource;

/**
* Create a new pending resource registration instance.
*
* @param \Illuminate\Routing\ResourceRegistrar $registrar
* @param array $resource
* @return void
*/
public function __construct(ResourceRegistrar $registrar, array $resource)
{
$this->registrar = $registrar;
$this->resource = $resource;
}

/**
* Route a resource to a controller.
*
* @return void
*/
public function register()
{
$this->registrar->register(
$this->resource['name'],
$this->resource['controller'],
$this->resource['options']
);
}

/**
* Set the methods the controller should apply to.
*
* @param array|string|dynamic $methods
*/
public function only($methods)
{
$this->resource['options']['only'] = is_array($methods) ? $methods : func_get_args();
}

/**
* Set the methods the controller should exclude.
*
* @param array|string|dynamic $methods
*/
public function except($methods)
{
$this->resource['options']['except'] = is_array($methods) ? $methods : func_get_args();
}
}
17 changes: 16 additions & 1 deletion src/Illuminate/Routing/ResourceRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ public function __construct(Router $router)
$this->router = $router;
}

/**
* Create a pending resource registration.
*
* @param string $name
* @param string $controller
* @param array $options
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function registration($name, $controller, array $options = [])
{
return new PendingResourceRegistration(
$this, compact('name', 'controller', 'options')
);
}

/**
* Route a resource to a controller.
*
Expand Down Expand Up @@ -113,7 +128,7 @@ protected function prefixedResource($name, $controller, array $options)
// are supported in the framework, but we need to know what name to use for a
// place-holder on the route parameters, which should be the base resources.
$callback = function ($me) use ($name, $controller, $options) {
$me->resource($name, $controller, $options);
$me->resource($name, $controller, $options)->register();
};

return $this->router->group(compact('prefix'), $callback);
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ public function attribute($key, $value)
* @param string $name
* @param string $controller
* @param array $options
* @return void
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function resource($name, $controller, array $options = [])
{
$this->router->resource($name, $controller, $this->attributes + $options);
return $this->router->resource($name, $controller, $this->attributes + $options);
}

/**
Expand Down
31 changes: 27 additions & 4 deletions src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ class Router implements RegistrarContract, BindingRegistrar
*/
protected $patterns = [];

/**
* The resources not yet registered.
*
* @var array
*/
protected $pendingResources = [];

/**
* The route group attribute stack.
*
Expand Down Expand Up @@ -240,7 +247,7 @@ public function resources(array $resources)
* @param string $name
* @param string $controller
* @param array $options
* @return void
* @return \Illuminate\Routing\PendingResourceRegistration
*/
public function resource($name, $controller, array $options = [])
{
Expand All @@ -250,7 +257,7 @@ public function resource($name, $controller, array $options = [])
$registrar = new ResourceRegistrar($this);
}

$registrar->register($name, $controller, $options);
return $this->pendingResources[] = $registrar->registration($name, $controller, $options);
}

/**
Expand Down Expand Up @@ -530,7 +537,7 @@ public function dispatchToRoute(Request $request)
*/
protected function findRoute($request)
{
$this->current = $route = $this->routes->match($request);
$this->current = $route = $this->getRoutes()->match($request);

$this->container->instance(Route::class, $route);

Expand Down Expand Up @@ -903,7 +910,7 @@ public function current()
*/
public function has($name)
{
return $this->routes->hasNamedRoute($name);
return $this->getRoutes()->hasNamedRoute($name);
}

/**
Expand Down Expand Up @@ -1049,9 +1056,25 @@ public function resourceVerbs(array $verbs = [])
*/
public function getRoutes()
{
$this->registerPendingResources();

return $this->routes;
}

/**
* Register all pending resources.
*
* @return void
*/
public function registerPendingResources()
{
foreach ($this->pendingResources as $key => $resource) {
$resource->register();
}

$this->pendingResources = [];
}

/**
* Set the route collection instance.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,44 @@ public function testCanRegisterResource()
$this->seeMiddleware('resource-middleware');
}

public function testCanLimitMethodsOnRegisteredResource()
{
$this->router->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub')
->only('index', 'show', 'destroy');

$this->assertCount(3, $this->router->getRoutes());

$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.index'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.show'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy'));
}

public function testCanExcludeMethodsOnRegisteredResource()
{
$this->router->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub')
->except(['index', 'create', 'store', 'show', 'edit']);

$this->assertCount(2, $this->router->getRoutes());

$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.update'));
$this->assertTrue($this->router->getRoutes()->hasNamedRoute('users.destroy'));
}

public function testCanNotLimitMethodsAfterRegistered()
{
$resource = $this->router->resource('users', 'Illuminate\Tests\Routing\RouteRegistrarControllerStub');

$this->router->getRoutes();

// pending resources have now been registered and
// can not be limited to specific methods anymore

$resource->only('index', 'show');
$resource->except('index', 'show');

$this->assertCount(7, $this->router->getRoutes());
}

public function testCanSetRouteName()
{
$this->router->as('users.index')->get('users', function () {
Expand Down