Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #25791 [Routing] Make sure we only build routes once (sroze)
This PR was merged into the 3.3 branch.

Discussion
----------

[Routing] Make sure we only build routes once

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #25746
| License       | MIT
| Doc PR        | ø

We need to build the collection(s) only once, else the prefix would be duplicated.

Commits
-------

927a75a Make sure we only build once and have one time the prefix when importing routes
  • Loading branch information
fabpot committed Jan 16, 2018
2 parents ff40995 + 927a75a commit 663f3f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Routing/RouteCollectionBuilder.php
Expand Up @@ -76,11 +76,11 @@ public function import($resource, $prefix = '/', $type = null)
foreach ($collection->getResources() as $resource) {
$builder->addResource($resource);
}

// mount into this builder
$this->mount($prefix, $builder);
}

// mount into this builder
$this->mount($prefix, $builder);

return $builder;
}

Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Routing/Tests/RouteCollectionBuilderTest.php
Expand Up @@ -335,4 +335,30 @@ public function testAutomaticRouteNamesDoNotConflict()
// there are 2 routes (i.e. with non-conflicting names)
$this->assertCount(3, $collection->all());
}

public function testAddsThePrefixOnlyOnceWhenLoadingMultipleCollections()
{
$firstCollection = new RouteCollection();
$firstCollection->add('a', new Route('/a'));

$secondCollection = new RouteCollection();
$secondCollection->add('b', new Route('/b'));

$loader = $this->getMockBuilder('Symfony\Component\Config\Loader\LoaderInterface')->getMock();
$loader->expects($this->any())
->method('supports')
->will($this->returnValue(true));
$loader
->expects($this->any())
->method('load')
->will($this->returnValue(array($firstCollection, $secondCollection)));

$routeCollectionBuilder = new RouteCollectionBuilder($loader);
$routeCollectionBuilder->import('/directory/recurse/*', '/other/', 'glob');
$routes = $routeCollectionBuilder->build()->all();

$this->assertEquals(2, count($routes));
$this->assertEquals('/other/a', $routes['a']->getPath());
$this->assertEquals('/other/b', $routes['b']->getPath());
}
}

0 comments on commit 663f3f0

Please sign in to comment.