Skip to content

Commit b587fa3

Browse files
committed
bug #16361 Re-adding the ability to add a resource to the RouteCollectionBuilder (weaverryan)
This PR was merged into the 2.8 branch. Discussion ---------- Re-adding the ability to add a resource to the RouteCollectionBuilder | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a At the end of #15778, I removed the ability to add a resource to the RouteCollectionBuilder. But, this is needed (at least internally): if you import, the returned RouteCollection may have resources, which need to be brought forward into the new RouteCollectionBuilder (the code in `import()` to do this already exists, but is calling an undefined `addResource()` method). This adds the test with minimal code to fix. P.S. Fabien told me to remove `addResource` originally... so isn't this *kind of* his fault? Commits ------- 3b67daa Re-adding the ability to add a resource to the RouteCollectionBuilder
2 parents c812b39 + 3b67daa commit b587fa3

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Symfony/Component/Routing/RouteCollectionBuilder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Config\Exception\FileLoaderLoadException;
1515
use Symfony\Component\Config\Loader\LoaderInterface;
16+
use Symfony\Component\Config\Resource\ResourceInterface;
1617

1718
/**
1819
* Helps add and import routes into a RouteCollection.
@@ -35,6 +36,7 @@ class RouteCollectionBuilder
3536
private $options = array();
3637
private $schemes;
3738
private $methods;
39+
private $resources = array();
3840

3941
/**
4042
* @param LoaderInterface $loader
@@ -237,6 +239,20 @@ public function setMethods($methods)
237239
return $this;
238240
}
239241

242+
/**
243+
* Adds a resource for this collection.
244+
*
245+
* @param ResourceInterface $resource
246+
*
247+
* @return $this
248+
*/
249+
private function addResource(ResourceInterface $resource)
250+
{
251+
$this->resources[] = $resource;
252+
253+
return $this;
254+
}
255+
240256
/**
241257
* Creates the final RouteCollection and returns it.
242258
*
@@ -291,6 +307,10 @@ public function build()
291307

292308
$routeCollection->addCollection($subCollection);
293309
}
310+
311+
foreach ($this->resources as $resource) {
312+
$routeCollection->addResource($resource);
313+
}
294314
}
295315

296316
return $routeCollection;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Routing\Tests;
1313

14+
use Symfony\Component\Config\Resource\FileResource;
1415
use Symfony\Component\Routing\Route;
1516
use Symfony\Component\Routing\RouteCollection;
1617
use Symfony\Component\Routing\RouteCollectionBuilder;
@@ -29,6 +30,7 @@ public function testImport()
2930
$originalRoute = new Route('/foo/path');
3031
$expectedCollection = new RouteCollection();
3132
$expectedCollection->add('one_test_route', $originalRoute);
33+
$expectedCollection->addResource(new FileResource('file_resource.yml'));
3234

3335
$resolvedLoader
3436
->expects($this->once())
@@ -52,6 +54,8 @@ public function testImport()
5254
$addedCollection = $importedRoutes->build();
5355
$route = $addedCollection->get('one_test_route');
5456
$this->assertSame($originalRoute, $route);
57+
// should return file_resource.yml, which is in the original collection
58+
$this->assertCount(1, $addedCollection->getResources());
5559
}
5660

5761
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy