Skip to content

[Routing] Restore aliases removal in RouteCollection::remove() #52845

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

Merged
merged 1 commit into from
Dec 1, 2023
Merged
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
16 changes: 12 additions & 4 deletions src/Symfony/Component/Routing/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,21 @@ public function get(string $name)
*/
public function remove($name)
{
$names = (array) $name;
foreach ($names as $n) {
unset($this->routes[$n], $this->priorities[$n]);
$routes = [];
foreach ((array) $name as $n) {
if (isset($this->routes[$n])) {
$routes[] = $n;
}

unset($this->routes[$n], $this->priorities[$n], $this->aliases[$n]);
}

if (!$routes) {
return;
}

foreach ($this->aliases as $k => $alias) {
if (\in_array($alias->getId(), $names, true)) {
if (\in_array($alias->getId(), $routes, true)) {
unset($this->aliases[$k]);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/Routing/Tests/RouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,22 @@ public function testGet()
public function testRemove()
{
$collection = new RouteCollection();
$collection->add('foo', $foo = new Route('/foo'));
$collection->add('foo', new Route('/foo'));

$collection1 = new RouteCollection();
$collection1->add('bar', $bar = new Route('/bar'));
$collection->addCollection($collection1);
$collection->add('last', $last = new Route('/last'));
$collection->addAlias('ccc_my_custom_alias', 'foo');
$collection->addAlias('alias_removed_when_removing_route_foo', 'foo');
$collection->addAlias('alias_directly_removed', 'bar');

$collection->remove('foo');
$this->assertSame(['bar' => $bar, 'last' => $last], $collection->all(), '->remove() can remove a single route');
$collection->remove('alias_directly_removed');
$this->assertNull($collection->getAlias('alias_directly_removed'));
$collection->remove(['bar', 'last']);
$this->assertSame([], $collection->all(), '->remove() accepts an array and can remove multiple routes at once');
$this->assertNull($collection->getAlias('ccc_my_custom_alias'));
$this->assertNull($collection->getAlias('alias_removed_when_removing_route_foo'));
}

public function testSetHost()
Expand Down