8000 Ensure routes are not overwritten if have the same name · symfony/symfony@e895402 · GitHub
[go: up one dir, main page]

Skip to content

Commit e895402

Browse files
committed
Ensure routes are not overwritten if have the same name
Add the `addNamePrefix` method to the PHP trait
1 parent e0a8690 commit e895402

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,14 @@ final public function controller($controller)
124124

125125
return $this;
126126
}
127+
128+
/**
129+
* Adds a prefix to the name of all the routes within the collection.
130+
*/
131+
final public function addNamePrefix(string $prefix): self
132+
{
133+
$this->route->addNamePrefix($prefix);
134+
135+
return $this;
136+
}
127137
}

src/Symfony/Component/Routing/RouteCollection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,17 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement
154154
}
155155

156156
/**
157-
* Add a prefix to the name of all the routes within in the collection.
158-
*
159-
* @param string $prefix
157+
* Adds a prefix to the name of all the routes within in the collection.
160158
*/
161159
public function addNamePrefix(string $prefix)
162160
{
161+
$prefixedRoutes = array();
162+
163163
foreach ($this->routes as $name => $route) {
164-
$this->routes[$prefix.$name] = $route;
165-
unset($this->routes[$name]);
164+
$prefixedRoutes[$prefix.$name] = $route;
166165
}
166+
167+
$this->routes = $prefixedRoutes;
167168
}
168169

169170
/**

src/Symfony/Component/Routing/Tests/RouteCollectionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testAddDefaultsAndRequirementsAndOptions()
118118
$collection->add('foo', new Route('/{placeholder}'));
119119
$collection1 = new RouteCollection();
120120
$collection1->add('bar', new Route('/{placeholder}',
121-
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
121+
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
122122
);
123123
$collection->addCollection($collection1);
124124

@@ -308,10 +308,12 @@ public function testAddNamePrefix()
308308
$collection = new RouteCollection();
309309
$collection->add('foo', $foo = new Route('/foo'));
310310
$collection->add('bar', $bar = new Route('/bar'));
311+
$collection->add('api_foo', $apiFoo = new Route('/api/foo'));
311312
$collection->addNamePrefix('api_');
312313

313314
$this->assertEquals($foo, $collection->get('api_foo'));
314315
$this->assertEquals($bar, $collection->get('api_bar'));
316+
$this->assertEquals($apiFoo, $collection->get('api_api_foo'));
315317
$this->assertNull($collection->get('foo'));
316318
$this->assertNull($collection->get('bar'));
317319
}

0 commit comments

Comments
 (0)
0