Commit 4ca61a4d authored by Fabien Potencier's avatar Fabien Potencier

bug #1205 Use shared RouteCollection among nested routes and apply prefix to...

bug #1205 Use shared RouteCollection among nested routes and apply prefix to route... (srsbiz, fabpot)

This PR was merged into the 1.3 branch.

Discussion
----------

Use shared RouteCollection among nested routes and apply prefix to route...

Replaces #1107, fixes #1106

Commits
-------

b510e5a0 moved logic to an internal method
401674e9 simplified previous merge
091240ea Use shared RouteCollection among nested routes and apply prefix to route path before generating unique route name
parents e9bd89bb b510e5a0
...@@ -188,12 +188,20 @@ class ControllerCollection ...@@ -188,12 +188,20 @@ class ControllerCollection
*/ */
public function flush($prefix = '') public function flush($prefix = '')
{ {
$routes = new RouteCollection(); return $this->doFlush($prefix, new RouteCollection());
}
private function doFlush($prefix, RouteCollection $routes)
{
if ($prefix !== '') {
$prefix = '/'.trim(trim($prefix), '/');
}
foreach ($this->controllers as $controller) { foreach ($this->controllers as $controller) {
if ($controller instanceof Controller) { if ($controller instanceof Controller) {
$controller->getRoute()->setPath($prefix.$controller->getRoute()->getPath());
if (!$name = $controller->getRouteName()) { if (!$name = $controller->getRouteName()) {
$name = $controller->generateRouteName($prefix); $name = $controller->generateRouteName('');
while ($routes->get($name)) { while ($routes->get($name)) {
$name .= '_'; $name .= '_';
} }
...@@ -202,12 +210,10 @@ class ControllerCollection ...@@ -202,12 +210,10 @@ class ControllerCollection
$routes->add($name, $controller->getRoute()); $routes->add($name, $controller->getRoute());
$controller->freeze(); $controller->freeze();
} else { } else {
$routes->addCollection($controller->flush($controller->prefix)); $routes->addCollection($controller->doFlush($prefix.$controller->prefix, $routes));
} }
} }
$routes->addPrefix($prefix);
$this->controllers = array(); $this->controllers = array();
return $routes; return $routes;
......
...@@ -89,6 +89,41 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase ...@@ -89,6 +89,41 @@ class ControllerCollectionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('_a_a', '_a_a_'), array_keys($routes->all())); $this->assertEquals(array('_a_a', '_a_a_'), array_keys($routes->all()));
} }
public function testUniqueGeneratedRouteNamesAmongMounts()
{
$controllers = new ControllerCollection(new Route());
$controllers->mount('/root-a', $rootA = new ControllerCollection(new Route()));
$controllers->mount('/root_a', $rootB = new ControllerCollection(new Route()));
$rootA->match('/leaf', function () {});
$rootB->match('/leaf', function () {});
$routes = $controllers->flush();
$this->assertCount(2, $routes->all());
$this->assertEquals(array('_root_a_leaf', '_root_a_leaf_'), array_keys($routes->all()));
}
public function testUniqueGeneratedRouteNamesAmongNestedMounts()
{
$controllers = new ControllerCollection(new Route());
$controllers->mount('/root-a', $rootA = new ControllerCollection(new Route()));
$controllers->mount('/root_a', $rootB = new ControllerCollection(new Route()));
$rootA->mount('/tree', $treeA = new ControllerCollection(new Route()));
$rootB->mount('/tree', $treeB = new ControllerCollection(new Route()));
$treeA->match('/leaf', function () {});
$treeB->match('/leaf', function () {});
$routes = $controllers->flush();
$this->assertCount(2, $routes->all());
$this->assertEquals(array('_root_a_tree_leaf', '_root_a_tree_leaf_'), array_keys($routes->all()));
}
public function testAssert() public function testAssert()
{ {
$controllers = new ControllerCollection(new Route()); $controllers = new ControllerCollection(new Route());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment