Skip to content

Commit

Permalink
[Routing] Restore aliases removal in RouteCollection::remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Dec 1, 2023
1 parent 36a3fc4 commit 5b5b866
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
16 changes: 12 additions & 4 deletions 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 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

0 comments on commit 5b5b866

Please sign in to comment.