Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57e0468

Browse files
committed
bug #16477 [Routing] Changing RouteCollectionBuilder::import() behavior to add to the builder (weaverryan)
This PR was squashed before being merged into the 2.8 branch (closes #16477). Discussion ---------- [Routing] Changing RouteCollectionBuilder::import() behavior to add to the builder | Q | A | ------------- | --- | Bug fix? | behavior change | New feature? | behavior change | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Based on conversation starting here: #15990 (comment). ```php // Before: $routes->mount('/admin', $routes->import(__DIR__.'/config/admin.yml'); // After: $routes->import(__DIR__.'/config/admin.yml', '/admin'); ``` This makes `import()` actually add the `RouteCollectionBuilder` into itself. We didn't do this before at Fabien's request, and actually the current implementation (before this PR) is quite "clean". However, I agree with @wouterj that `import()` really sounds/looks like it will actually *import* those routes *into* this `RouteCollectionBuilder`. This change is subjective - we just need to pick which way we like better and run full steam with it. Commits ------- 8feb9ef [Routing] Changing RouteCollectionBuilder::import() behavior to add to the builder
2 parents fe6e9e4 + 8feb9ef commit 57e0468

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/Symfony/Component/Routing/RouteCollectionBuilder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,17 @@ public function __construct(LoaderInterface $loader = null)
4949
/**
5050
* Import an external routing resource and returns the RouteCollectionBuilder.
5151
*
52-
* $routes->mount('/blog', $routes->import('blog.yml'));
52+
* $routes->import('blog.yml', '/blog');
5353
*
54-
* @param mixed $resource
55-
* @param string $type
54+
* @param mixed $resource
55+
* @param string|null $prefix
56+
* @param string $type
5657
*
5758
* @return RouteCollectionBuilder
5859
*
5960
* @throws FileLoaderLoadException
6061
*/
61-
public function import($resource, $type = null)
62+
public function import($resource, $prefix = '/', $type = null)
6263
{
6364
/** @var RouteCollection $collection */
6465
$collection = $this->load($resource, $type);
@@ -73,6 +74,9 @@ public function import($resource, $type = null)
7374
$builder->addResource($resource);
7475
}
7576

77+
// mount into this builder
78+
$this->mount($prefix, $builder);
79+
7680
return $builder;
7781
}
7882

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testImport()
4545

4646
// import the file!
4747
$routes = new RouteCollectionBuilder($loader);
48-
$importedRoutes = $routes->import('admin_routing.yml', 'yaml');
48+
$importedRoutes = $routes->import('admin_routing.yml', '/', 'yaml');
4949

5050
// we should get back a RouteCollectionBuilder
5151
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollectionBuilder', $importedRoutes);
@@ -56,6 +56,9 @@ public function testImport()
5656
$this->assertSame($originalRoute, $route);
5757
// should return file_resource.yml, which is in the original collection
5858
$this->assertCount(1, $addedCollection->getResources());
59+
60+
// make sure the routes were imported into the top-level builder
61+
$this->assertCount(1, $routes->build());
5962
}
6063

6164
/**
@@ -285,7 +288,7 @@ public function testFlushSetsPrefixedWithMultipleLevels()
285288
->method('load')
286289
->will($this->returnValue($importedCollection));
287290
// import this from the /admin route builder
288-
$adminRoutes->mount('/imported', $adminRoutes->import('admin.yml'));
291+
$adminRoutes->import('admin.yml', '/imported');
289292

290293
$collection = $routes->build();
291294
$this->assertEquals('/admin/dashboard', $collection->get('admin_dashboard')->getPath(), 'Routes before mounting have the prefix');

0 commit comments

Comments
 (0)