From 0053eeefcf921fb84f749f2f1b6a66dbe9253f39 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 19 Mar 2018 12:40:52 +0100 Subject: [PATCH] [Routing] Fix name-prefixing when using PHP DSL --- .../Routing/Loader/Configurator/ImportConfigurator.php | 8 +++++++- .../Routing/Loader/Configurator/Traits/RouteTrait.php | 10 ---------- .../Component/Routing/Tests/Fixtures/php_dsl.php | 3 +++ .../Routing/Tests/Loader/PhpFileLoaderTest.php | 2 ++ 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php b/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php index 9057c2a5d09dc..baacbe9592dcb 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/ImportConfigurator.php @@ -40,8 +40,14 @@ public function __destruct() * * @return $this */ - final public function prefix($prefix) + final public function prefix($prefix, string $namePrefix = '') { + if ('' !== $namePrefix) { + $this->route->addNamePrefix($namePrefix); + } + if (!$prefix) { + return $this; + } if (!\is_array($prefix)) { $this->route->addPrefix($prefix); } else { diff --git a/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php b/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php index d3ced4d63807b..3613f2522285f 100644 --- a/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php +++ b/src/Symfony/Component/Routing/Loader/Configurator/Traits/RouteTrait.php @@ -124,14 +124,4 @@ 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/Tests/Fixtures/php_dsl.php b/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl.php index 0780c9fa80f1d..0b8fa0a9eb339 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/php_dsl.php @@ -15,6 +15,9 @@ ->prefix('/sub') ->requirements(array('id' => '\d+')); + $routes->import('php_dsl_sub.php') + ->prefix('/zub', 'z_'); + $routes->add('ouf', '/ouf') ->schemes(array('https')) ->methods(array('GET')) diff --git a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php index 378254282fe76..c0a72b6f81cbe 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php @@ -106,6 +106,8 @@ public function testRoutingConfigurator() ->setHost('host') ->setRequirements(array('id' => '\d+')) ); + $expectedCollection->add('z_c_bar', new Route('/zub/pub/bar')); + $expectedCollection->add('z_c_pub_buz', (new Route('/zub/pub/buz'))->setHost('host')); $expectedCollection->add('ouf', (new Route('/ouf')) ->setSchemes(array('https')) ->setMethods(array('GET'))