diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php index 3613f25222..d3ced4d638 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php @@ -124,4 +124,14 @@ trait RouteTrait return $this; } + + /** + * Adds a prefix to the name of all the routes within the collection. + */ + final public function addNamePrefix(string $prefix): self + { + $this->route->addNamePrefix($prefix); + + return $this; + } } diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 3a77890703..31d69ca6d8 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -165,6 +165,10 @@ class XmlFileLoader extends FileLoader $subCollection->addRequirements($requirements); $subCollection->addOptions($options); + if ($namePrefix = $node->getAttribute('name-prefix')) { + $subCollection->addNamePrefix($namePrefix); + } + $collection->addCollection($subCollection); } diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index f3072c927b..a796dd1475 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -27,7 +27,7 @@ use Symfony\Component\Config\Loader\FileLoader; class YamlFileLoader extends FileLoader { private static $availableKeys = array( - 'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', + 'resource', 'type', 'prefix', 'path', 'host', 'schemes', 'methods', 'defaults', 'requirements', 'options', 'condition', 'controller', 'name_prefix', ); private $yamlParser; @@ -169,6 +169,10 @@ class YamlFileLoader extends FileLoader $subCollection->addRequirements($requirements); $subCollection->addOptions($options); + if (isset($config['name_prefix'])) { + $subCollection->addNamePrefix($config['name_prefix']); + } + $collection->addCollection($subCollection); } diff --git a/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd b/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd index a97111aaa5..fd461154df 100644 --- a/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd +++ b/src/Symfony/Component/Routing/Loader/schema/routing/routing-1.0.xsd @@ -50,6 +50,7 @@ + diff --git a/src/Symfony/Component/Routing/RouteCollection.php b/src/Symfony/Component/Routing/RouteCollection.php index feabf234bc..ebe92da714 100644 --- a/src/Symfony/Component/Routing/RouteCollection.php +++ b/src/Symfony/Component/Routing/RouteCollection.php @@ -153,6 +153,20 @@ class RouteCollection implements \IteratorAggregate, \Countable } } + /** + * Adds a prefix to the name of all the routes within in the collection. + */ + public function addNamePrefix(string $prefix) + { + $prefixedRoutes = array(); + + foreach ($this->routes as $name => $route) { + $prefixedRoutes[$prefix.$name] = $route; + } + + $this->routes = $prefixedRoutes; + } + /** * Sets the host pattern on all routes. * diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.xml b/src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.xml new file mode 100644 index 0000000000..b158dadb92 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.xml @@ -0,0 +1,10 @@ + + + + + + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.yml b/src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.yml new file mode 100644 index 0000000000..90dce0ea1b --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.yml @@ -0,0 +1,7 @@ +app: + resource: ../controller/routing.yml + +api: + resource: ../controller/routing.yml + name_prefix: api_ + prefix: /api diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidkeys.yml b/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidkeys.yml index 015e270fb1..b01d502738 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidkeys.yml +++ b/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidkeys.yml @@ -1,3 +1,3 @@ someroute: resource: path/to/some.yml - name_prefix: test_ + not_valid_key: test_ diff --git a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php index 221434b006..e5353d7eba 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php @@ -361,4 +361,15 @@ class XmlFileLoaderTest extends TestCase $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller'))); $loader->load('import_override_defaults.xml'); } + + public function testImportRouteWithNamePrefix() + { + $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix'))); + $routeCollection = $loader->load('routing.xml'); + + $this->assertNotNull($routeCollection->get('app_blog')); + $this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath()); + $this->assertNotNull($routeCollection->get('api_app_blog')); + $this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath()); + } } diff --git a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php index 1f7fd43897..5fa38f39d0 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php @@ -182,4 +182,15 @@ class YamlFileLoaderTest extends TestCase $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/controller'))); $loader->load('import_override_defaults.yml'); } + + public function testImportRouteWithNamePrefix() + { + $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures/import_with_name_prefix'))); + $routeCollection = $loader->load('routing.yml'); + + $this->assertNotNull($routeCollection->get('app_blog')); + $this->assertEquals('/blog', $routeCollection->get('app_blog')->getPath()); + $this->assertNotNull($routeCollection->get('api_app_blog')); + $this->assertEquals('/api/blog', $routeCollection->get('api_app_blog')->getPath()); + } } diff --git a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php index 83457ff14a..3527e12895 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php @@ -302,4 +302,19 @@ class RouteCollectionTest extends TestCase $this->assertEquals(array('PUT'), $routea->getMethods()); $this->assertEquals(array('PUT'), $routeb->getMethods()); } + + public function testAddNamePrefix() + { + $collection = new RouteCollection(); + $collection->add('foo', $foo = new Route('/foo')); + $collection->add('bar', $bar = new Route('/bar')); + $collection->add('api_foo', $apiFoo = new Route('/api/foo')); + $collection->addNamePrefix('api_'); + + $this->assertEquals($foo, $collection->get('api_foo')); + $this->assertEquals($bar, $collection->get('api_bar')); + $this->assertEquals($apiFoo, $collection->get('api_api_foo')); + $this->assertNull($collection->get('foo')); + $this->assertNull($collection->get('bar')); + } }