Skip to content

[master] Add route removal functionality to RouteCollection #56055

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/Illuminate/Routing/CompiledRouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ public function add(Route $route)
return $this->routes->add($route);
}

/**
* Remove the given route from the collection.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
public function remove(Route $route)
{
$this->routes->remove($route);
}

/**
* Refresh the name look-up table.
*
Expand Down
60 changes: 60 additions & 0 deletions src/Illuminate/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public function add(Route $route)
return $route;
}

/**
* Remove the given route from the collection.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
public function remove(Route $route)
{
$this->removeFromCollections($route);

$this->removeLookups($route);
}

/**
* Add the given route to the arrays of routes.
*
Expand Down Expand Up @@ -80,6 +93,24 @@ protected function addToCollections($route)
}
}

/**
* Remove the given route from the arrays of routes.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
protected function removeFromCollections(Route $route)
{
$methods = $route->methods();
$domainAndUri = $route->getDomain().$route->uri();

foreach ($methods as $method) {
unset($this->routes[$method][$domainAndUri]);
}

unset($this->allRoutes[implode('|', $methods).$domainAndUri]);
}

/**
* Add the route to any look-up tables if necessary.
*
Expand All @@ -105,6 +136,24 @@ protected function addLookups($route)
}
}

/**
* Remove the route from any look-up tables.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
protected function removeLookups(Route $route)
{
if ($name = $route->getName()) {
unset($this->nameList[$name]);
}

$action = $route->getAction();
if (isset($action['controller'])) {
$this->removeFromActionList($action);
}
}

/**
* Add a route to the controller action dictionary.
*
Expand All @@ -117,6 +166,17 @@ protected function addToActionList($action, $route)
$this->actionList[trim($action['controller'], '\\')] = $route;
}

/**
* Remove a route from the controller action dictionary.
*
* @param array $action
* @return void
*/
protected function removeFromActionList(array $action)
{
unset($this->actionList[trim($action['controller'], '\\')]);
}

/**
* Refresh the name look-up table.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Routing/RouteCollectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ interface RouteCollectionInterface
*/
public function add(Route $route);

/**
* Remove a Route instance from the collection.
*
* @param \Illuminate\Routing\Route $route
* @return void
*/
public function remove(Route $route);

/**
* Refresh the name look-up table.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Routing/CompiledRouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public function testRouteCollectionCanAddRoute()
$this->assertCount(1, $this->collection());
}

public function testRouteCollectionCanRemoveRoute()
{
$route = $this->newRoute('GET', 'foo', [
'uses' => 'FooController@index',
'as' => 'foo_index',
]);

$this->routeCollection->add($route);
$this->assertCount(1, $this->collection());

$this->routeCollection->remove($route);
$this->assertCount(0, $this->collection());
}

public function testRouteCollectionAddReturnsTheRoute()
{
$outputRoute = $this->collection()->add($inputRoute = $this->newRoute('GET', 'foo', [
Expand Down
22 changes: 22 additions & 0 deletions tests/Routing/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ public function testRouteCollectionCanAddRoute()
$this->assertCount(1, $this->routeCollection);
}

public function testRouteCanBeRemoved()
{
$route = new Route('GET', 'foo', [
'uses' => 'FooController@index',
'as' => 'foo_index',
'controller' => 'FooController@index',
]);

$this->routeCollection->add($route);
$this->assertCount(1, $this->routeCollection);
$this->assertSame($route, $this->routeCollection->getByName('foo_index'));
$this->assertSame($route, $this->routeCollection->getRoutes()[0]);
$this->assertSame($route, $this->routeCollection->getByAction('FooController@index'));

$this->routeCollection->remove($route);

$this->assertCount(0, $this->routeCollection);
$this->assertNull($this->routeCollection->getByName('foo_index'));
$this->assertEmpty($this->routeCollection->getRoutes());
$this->assertNull($this->routeCollection->getByAction('FooController@index'));
}

public function testRouteCollectionAddReturnsTheRoute()
{
$outputRoute = $this->routeCollection->add($inputRoute = new Route('GET', 'foo', [
Expand Down
Loading