From e0a8690dd697f1e618a346f8ff88c28cbd17090c Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Mon, 27 Nov 2017 17:18:01 +0000 Subject: [PATCH 1/2] Allow to set name prefixes from the configuration --- .../Component/Routing/Loader/XmlFileLoader.php | 4 ++++ .../Component/Routing/Loader/YamlFileLoader.php | 6 +++++- .../Routing/Loader/schema/routing/routing-1.0.xsd | 1 + src/Symfony/Component/Routing/RouteCollection.php | 13 +++++++++++++ .../Fixtures/import_with_name_prefix/routing.xml | 10 ++++++++++ .../Fixtures/import_with_name_prefix/routing.yml | 7 +++++++ .../Routing/Tests/Fixtures/nonvalidkeys.yml | 2 +- .../Routing/Tests/Loader/XmlFileLoaderTest.php | 11 +++++++++++ .../Routing/Tests/Loader/YamlFileLoaderTest.php | 11 +++++++++++ .../Routing/Tests/RouteCollectionTest.php | 15 ++++++++++++++- 10 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.xml create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/import_with_name_prefix/routing.yml diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 3a77890703ce2..31d69ca6d8e8c 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -165,6 +165,10 @@ protected function parseImport(RouteCollection $collection, \DOMElement $node, $ $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 f3072c927b73e..a796dd14758f2 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -27,7 +27,7 @@ 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 @@ protected function parseImport(RouteCollection $collection, array $config, $path $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 a97111aaa55e3..fd461154dfe44 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 feabf234bc6d4..c19cfb1c0cf26 100644 --- a/src/Symfony/Component/Routing/RouteCollection.php +++ b/src/Symfony/Component/Routing/RouteCollection.php @@ -153,6 +153,19 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement } } + /** + * Add a prefix to the name of all the routes within in the collection. + * + * @param string $prefix + */ + public function addNamePrefix(string $prefix) + { + foreach ($this->routes as $name => $route) { + $this->routes[$prefix.$name] = $route; + unset($this->routes[$name]); + } + } + /** * 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 0000000000000..b158dadb92734 --- /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 0000000000000..90dce0ea1bfc4 --- /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 015e270fb187b..b01d502738284 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 221434b0068aa..e5353d7eba292 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php @@ -361,4 +361,15 @@ public function testImportWithOverriddenController() $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 1f7fd43897ae3..5fa38f39d05a6 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php @@ -182,4 +182,15 @@ public function testImportWithOverriddenController() $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 83457ff14a7bf..4f054c9093813 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php @@ -118,7 +118,7 @@ public function testAddDefaultsAndRequirementsAndOptions() $collection->add('foo', new Route('/{placeholder}')); $collection1 = new RouteCollection(); $collection1->add('bar', new Route('/{placeholder}', - array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value')) + array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value')) ); $collection->addCollection($collection1); @@ -302,4 +302,17 @@ public function testSetMethods() $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->addNamePrefix('api_'); + + $this->assertEquals($foo, $collection->get('api_foo')); + $this->assertEquals($bar, $collection->get('api_bar')); + $this->assertNull($collection->get('foo')); + $this->assertNull($collection->get('bar')); + } } From e89540232325b8a380cb9ac0b91053dab249ed08 Mon Sep 17 00:00:00 2001 From: Samuel ROZE Date: Tue, 28 Nov 2017 05:08:19 +0000 Subject: [PATCH 2/2] Ensure routes are not overwritten if have the same name Add the `addNamePrefix` method to the PHP trait --- .../Routing/Loader/Configurator/Traits/RouteTrait.php | 10 ++++++++++ src/Symfony/Component/Routing/RouteCollection.php | 11 ++++++----- .../Component/Routing/Tests/RouteCollectionTest.php | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php index 3613f2522285f..d3ced4d63807b 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 @@ final public function controller($controller) 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/RouteCollection.php b/src/Symfony/Component/Routing/RouteCollection.php index c19cfb1c0cf26..ebe92da714c39 100644 --- a/src/Symfony/Component/Routing/RouteCollection.php +++ b/src/Symfony/Component/Routing/RouteCollection.php @@ -154,16 +154,17 @@ public function addPrefix($prefix, array $defaults = array(), array $requirement } /** - * Add a prefix to the name of all the routes within in the collection. - * - * @param string $prefix + * 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) { - $this->routes[$prefix.$name] = $route; - unset($this->routes[$name]); + $prefixedRoutes[$prefix.$name] = $route; } + + $this->routes = $prefixedRoutes; } /** diff --git a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php index 4f054c9093813..3527e12895683 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php @@ -118,7 +118,7 @@ public function testAddDefaultsAndRequirementsAndOptions() $collection->add('foo', new Route('/{placeholder}')); $collection1 = new RouteCollection(); $collection1->add('bar', new Route('/{placeholder}', - array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value')) + array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value')) ); $collection->addCollection($collection1); @@ -308,10 +308,12 @@ 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')); }