[Routing] Changing RouteCollectionBuilder::import() behavior to add to the builder

This commit is contained in:
Ryan Weaver 2015-11-05 15:58:36 -05:00 committed by Fabien Potencier
parent 8c4e756c1b
commit 8feb9ef080
2 changed files with 13 additions and 6 deletions

View File

@ -49,16 +49,17 @@ class RouteCollectionBuilder
/**
* Import an external routing resource and returns the RouteCollectionBuilder.
*
* $routes->mount('/blog', $routes->import('blog.yml'));
* $routes->import('blog.yml', '/blog');
*
* @param mixed $resource
* @param string $type
* @param mixed $resource
* @param string|null $prefix
* @param string $type
*
* @return RouteCollectionBuilder
*
* @throws FileLoaderLoadException
*/
public function import($resource, $type = null)
public function import($resource, $prefix = '/', $type = null)
{
/** @var RouteCollection $collection */
$collection = $this->load($resource, $type);
@ -73,6 +74,9 @@ class RouteCollectionBuilder
$builder->addResource($resource);
}
// mount into this builder
$this->mount($prefix, $builder);
return $builder;
}

View File

@ -45,7 +45,7 @@ class RouteCollectionBuilderTest extends \PHPUnit_Framework_TestCase
// import the file!
$routes = new RouteCollectionBuilder($loader);
$importedRoutes = $routes->import('admin_routing.yml', 'yaml');
$importedRoutes = $routes->import('admin_routing.yml', '/', 'yaml');
// we should get back a RouteCollectionBuilder
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollectionBuilder', $importedRoutes);
@ -56,6 +56,9 @@ class RouteCollectionBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertSame($originalRoute, $route);
// should return file_resource.yml, which is in the original collection
$this->assertCount(1, $addedCollection->getResources());
// make sure the routes were imported into the top-level builder
$this->assertCount(1, $routes->build());
}
/**
@ -285,7 +288,7 @@ class RouteCollectionBuilderTest extends \PHPUnit_Framework_TestCase
->method('load')
->will($this->returnValue($importedCollection));
// import this from the /admin route builder
$adminRoutes->mount('/imported', $adminRoutes->import('admin.yml'));
$adminRoutes->import('admin.yml', '/imported');
$collection = $routes->build();
$this->assertEquals('/admin/dashboard', $collection->get('admin_dashboard')->getPath(), 'Routes before mounting have the prefix');