From f614584395f80bd432f5b67abec4ee4cda7296bd Mon Sep 17 00:00:00 2001 From: Gerhard Seidel Date: Wed, 27 May 2020 14:21:00 +0800 Subject: [PATCH 1/3] fix passing route object parameter to generator --- src/ProviderBasedGenerator.php | 5 +++++ .../Routing/ProviderBasedGeneratorTest.php | 22 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/ProviderBasedGenerator.php b/src/ProviderBasedGenerator.php index 05a56a9d..9fed72bf 100644 --- a/src/ProviderBasedGenerator.php +++ b/src/ProviderBasedGenerator.php @@ -69,6 +69,11 @@ public function generate($name, $parameters = [], $referenceType = self::ABSOLUT throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name)); } + // the parameter must be unset to avoid unexpected generation behaviour + if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)) { + unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); + } + // the Route has a cache of its own and is not recompiled as long as it does not get modified $compiledRoute = $route->compile(); $hostTokens = $compiledRoute->getHostTokens(); diff --git a/tests/Unit/Routing/ProviderBasedGeneratorTest.php b/tests/Unit/Routing/ProviderBasedGeneratorTest.php index 529ffea7..c7a8e73a 100644 --- a/tests/Unit/Routing/ProviderBasedGeneratorTest.php +++ b/tests/Unit/Routing/ProviderBasedGeneratorTest.php @@ -21,6 +21,7 @@ use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\Route as SymfonyRoute; class ProviderBasedGeneratorTest extends TestCase { @@ -106,6 +107,15 @@ public function testGenerateFromRoute(): void $this->assertEquals('result_url', $url); } + public function testRemoveRouteObject(): void + { + $url = $this->generator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [ + RouteObjectInterface::ROUTE_OBJECT => new Proxy('/path'), + ]); + + $this->assertEquals('result_url', $url); + } + /** * @group legacy * @@ -172,7 +182,12 @@ class TestableProviderBasedGenerator extends ProviderBasedGenerator { protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = []) { - return 'result_url'; + $url = 'result_url'; + if ($parameters && $query = http_build_query($parameters, '', '&', PHP_QUERY_RFC3986)) { + $url .= '?'.$query; + } + + return $url; } } @@ -188,3 +203,8 @@ public function getContent(): ?object return null; } } + +class Proxy extends SymfonyRoute +{ + public $__isInitialized__ = true; +} From fd728fe96c90ffc361cf1f04925cefdfcbeb883b Mon Sep 17 00:00:00 2001 From: Gerhard Seidel Date: Wed, 27 May 2020 14:28:16 +0800 Subject: [PATCH 2/3] fix code style --- tests/Unit/Routing/ProviderBasedGeneratorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Routing/ProviderBasedGeneratorTest.php b/tests/Unit/Routing/ProviderBasedGeneratorTest.php index c7a8e73a..fd861238 100644 --- a/tests/Unit/Routing/ProviderBasedGeneratorTest.php +++ b/tests/Unit/Routing/ProviderBasedGeneratorTest.php @@ -115,7 +115,7 @@ public function testRemoveRouteObject(): void $this->assertEquals('result_url', $url); } - + /** * @group legacy * From 61d2d4d2519f8d5a50439750ba2024f0dc72c4d3 Mon Sep 17 00:00:00 2001 From: Gerhard Seidel Date: Wed, 27 May 2020 15:44:51 +0800 Subject: [PATCH 3/3] only unset route object parameter if it is used before --- src/ProviderBasedGenerator.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/ProviderBasedGenerator.php b/src/ProviderBasedGenerator.php index 9fed72bf..a57b3608 100644 --- a/src/ProviderBasedGenerator.php +++ b/src/ProviderBasedGenerator.php @@ -65,15 +65,11 @@ public function generate($name, $parameters = [], $referenceType = self::ABSOLUT && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute ) { $route = $parameters[RouteObjectInterface::ROUTE_OBJECT]; + unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); } elseif (null === $route = $this->provider->getRouteByName($name)) { throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name)); } - // the parameter must be unset to avoid unexpected generation behaviour - if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)) { - unset($parameters[RouteObjectInterface::ROUTE_OBJECT]); - } - // the Route has a cache of its own and is not recompiled as long as it does not get modified $compiledRoute = $route->compile(); $hostTokens = $compiledRoute->getHostTokens();