From ee51b0b64ca2597e1ed4d11b2dd811c473c61e57 Mon Sep 17 00:00:00 2001 From: Arturs Vonda Date: Fri, 11 Jul 2014 21:32:20 +0300 Subject: [PATCH 1/2] [Routing] Add matched and default parameters to redirect responses --- .../Matcher/Dumper/PhpMatcherDumper.php | 48 ++++--- .../Component/Routing/Matcher/UrlMatcher.php | 4 +- .../Tests/Fixtures/dumper/url_matcher1.php | 120 ++++++++++------ .../Tests/Fixtures/dumper/url_matcher2.php | 136 ++++++++++++------ .../Tests/Fixtures/dumper/url_matcher3.php | 9 +- .../Matcher/RedirectableUrlMatcherTest.php | 16 ++- 6 files changed, 218 insertions(+), 115 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index aa7df8a78b5ba..c1c447135dc46 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -286,10 +286,32 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren } } + // optimize parameters array + if ($matches || $hostMatches) { + $vars = array(); + if ($hostMatches) { + $vars[] = '$hostMatches'; + } + if ($matches) { + $vars[] = '$matches'; + } + $vars[] = "array('_route' => '$name')"; + + $code .= sprintf( + " \$ret = \$this->mergeDefaults(array_replace(%s), %s);\n", + implode(', ', $vars), + str_replace("\n", '', var_export($route->getDefaults(), true)) + ); + } elseif ($route->getDefaults()) { + $code .= sprintf(" \$ret = %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true))); + } else { + $code .= sprintf(" \$ret = array('_route' => '%s');\n", $name); + } + if ($hasTrailingSlash) { $code .= <<redirect(\$pathinfo.'/', '$name'); + return array_replace(\$ret, \$this->redirect(\$pathinfo.'/', '$name')); } @@ -304,34 +326,14 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $code .= <<context->getScheme()])) { - return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes)); + return array_replace(\$ret, \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes))); } EOF; } - // optimize parameters array - if ($matches || $hostMatches) { - $vars = array(); - if ($hostMatches) { - $vars[] = '$hostMatches'; - } - if ($matches) { - $vars[] = '$matches'; - } - $vars[] = "array('_route' => '$name')"; - - $code .= sprintf( - " return \$this->mergeDefaults(array_replace(%s), %s);\n", - implode(', ', $vars), - str_replace("\n", '', var_export($route->getDefaults(), true)) - ); - } elseif ($route->getDefaults()) { - $code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true))); - } else { - $code .= sprintf(" return array('_route' => '%s');\n", $name); - } + $code .= " return \$ret;\n"; $code .= " }\n"; if ($methods) { diff --git a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php index 9786a9b42cc60..c5eb836609f72 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -164,7 +164,9 @@ protected function matchCollection($pathinfo, RouteCollection $routes) $status = $this->handleRouteRequirements($pathinfo, $name, $route); if (self::ROUTE_MATCH === $status[0]) { - return $status[1]; + $attributes = array_replace($matches, $hostMatches, (array) $status[1]); + + return $this->mergeDefaults($attributes, $route->getDefaults()); } if (self::REQUIREMENT_MISMATCH === $status[0]) { diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php index 4ea0b8a1a3e7c..80efee5b42d7d 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -29,7 +29,8 @@ public function match($pathinfo) // foo if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?Pbaz|symfony)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',)); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',)); + return $ret; } if (0 === strpos($pathinfo, '/bar')) { @@ -40,7 +41,8 @@ public function match($pathinfo) goto not_bar; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ()); + return $ret; } not_bar: @@ -51,7 +53,8 @@ public function match($pathinfo) goto not_barhead; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ()); + return $ret; } not_barhead: @@ -61,24 +64,28 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/test/baz')) { // baz if ($pathinfo === '/test/baz') { - return array('_route' => 'baz'); + $ret = array('_route' => 'baz'); + return $ret; } // baz2 if ($pathinfo === '/test/baz.html') { - return array('_route' => 'baz2'); + $ret = array('_route' => 'baz2'); + return $ret; } // baz3 if ($pathinfo === '/test/baz3/') { - return array('_route' => 'baz3'); + $ret = array('_route' => 'baz3'); + return $ret; } } // baz4 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ()); + return $ret; } // baz5 @@ -88,7 +95,8 @@ public function match($pathinfo) goto not_baz5; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ()); + return $ret; } not_baz5: @@ -99,7 +107,8 @@ public function match($pathinfo) goto not_bazbaz6; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ()); + return $ret; } not_bazbaz6: @@ -107,47 +116,55 @@ public function match($pathinfo) // foofoo if ($pathinfo === '/foofoo') { - return array ( 'def' => 'test', '_route' => 'foofoo',); + $ret = array ( 'def' => 'test', '_route' => 'foofoo',); + return $ret; } // quoter if (preg_match('#^/(?P[\']+)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ()); + return $ret; } // space if ($pathinfo === '/spa ce') { - return array('_route' => 'space'); + $ret = array('_route' => 'space'); + return $ret; } if (0 === strpos($pathinfo, '/a')) { if (0 === strpos($pathinfo, '/a/b\'b')) { // foo1 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ()); + return $ret; } // bar1 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ()); + return $ret; } } // overridden if (preg_match('#^/a/(?P.*)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ()); + return $ret; } if (0 === strpos($pathinfo, '/a/b\'b')) { // foo2 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ()); + return $ret; } // bar2 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ()); + return $ret; } } @@ -157,40 +174,47 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/multi')) { // helloWorld if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?P[^/]++))?$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',)); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',)); + return $ret; } // overridden2 if ($pathinfo === '/multi/new') { - return array('_route' => 'overridden2'); + $ret = array('_route' => 'overridden2'); + return $ret; } // hey if ($pathinfo === '/multi/hey/') { - return array('_route' => 'hey'); + $ret = array('_route' => 'hey'); + return $ret; } } // foo3 if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ()); + return $ret; } // bar3 if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ()); + return $ret; } if (0 === strpos($pathinfo, '/aba')) { // ababa if ($pathinfo === '/ababa') { - return array('_route' => 'ababa'); + $ret = array('_route' => 'ababa'); + return $ret; } // foo4 if (preg_match('#^/aba/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ()); + return $ret; } } @@ -200,12 +224,14 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route1 if ($pathinfo === '/route1') { - return array('_route' => 'route1'); + $ret = array('_route' => 'route1'); + return $ret; } // route2 if ($pathinfo === '/c2/route2') { - return array('_route' => 'route2'); + $ret = array('_route' => 'route2'); + return $ret; } } @@ -213,7 +239,8 @@ public function match($pathinfo) if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) { // route3 if ($pathinfo === '/c2/route3') { - return array('_route' => 'route3'); + $ret = array('_route' => 'route3'); + return $ret; } } @@ -221,7 +248,8 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route4 if ($pathinfo === '/route4') { - return array('_route' => 'route4'); + $ret = array('_route' => 'route4'); + return $ret; } } @@ -229,36 +257,42 @@ public function match($pathinfo) if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) { // route5 if ($pathinfo === '/route5') { - return array('_route' => 'route5'); + $ret = array('_route' => 'route5'); + return $ret; } } // route6 if ($pathinfo === '/route6') { - return array('_route' => 'route6'); + $ret = array('_route' => 'route6'); + return $ret; } if (preg_match('#^(?P[^\\.]++)\\.example\\.com$#si', $host, $hostMatches)) { if (0 === strpos($pathinfo, '/route1')) { // route11 if ($pathinfo === '/route11') { - return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ()); + $ret = $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ()); + return $ret; } // route12 if ($pathinfo === '/route12') { - return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',)); + $ret = $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',)); + return $ret; } // route13 if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ()); + $ret = $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ()); + return $ret; } // route14 if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',)); + $ret = $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',)); + return $ret; } } @@ -268,7 +302,8 @@ public function match($pathinfo) if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) { // route15 if (0 === strpos($pathinfo, '/route15') && preg_match('#^/route15/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ()); + return $ret; } } @@ -276,12 +311,14 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/route1')) { // route16 if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',)); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',)); + return $ret; } // route17 if ($pathinfo === '/route17') { - return array('_route' => 'route17'); + $ret = array('_route' => 'route17'); + return $ret; } } @@ -289,18 +326,21 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/a')) { // a if ($pathinfo === '/a/a...') { - return array('_route' => 'a'); + $ret = array('_route' => 'a'); + return $ret; } if (0 === strpos($pathinfo, '/a/b')) { // b if (preg_match('#^/a/b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ()); + return $ret; } // c if (0 === strpos($pathinfo, '/a/b/c') && preg_match('#^/a/b/c/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ()); + return $ret; } } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php index f9d3fa2d8257b..aa908b18ca672 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -29,7 +29,8 @@ public function match($pathinfo) // foo if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?Pbaz|symfony)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',)); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',)); + return $ret; } if (0 === strpos($pathinfo, '/bar')) { @@ -40,7 +41,8 @@ public function match($pathinfo) goto not_bar; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ()); + return $ret; } not_bar: @@ -51,7 +53,8 @@ public function match($pathinfo) goto not_barhead; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ()); + return $ret; } not_barhead: @@ -61,32 +64,36 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/test/baz')) { // baz if ($pathinfo === '/test/baz') { - return array('_route' => 'baz'); + $ret = array('_route' => 'baz'); + return $ret; } // baz2 if ($pathinfo === '/test/baz.html') { - return array('_route' => 'baz2'); + $ret = array('_route' => 'baz2'); + return $ret; } // baz3 if (rtrim($pathinfo, '/') === '/test/baz3') { + $ret = array('_route' => 'baz3'); if (substr($pathinfo, -1) !== '/') { - return $this->redirect($pathinfo.'/', 'baz3'); + return array_replace($ret, $this->redirect($pathinfo.'/', 'baz3')); } - return array('_route' => 'baz3'); + return $ret; } } // baz4 if (preg_match('#^/test/(?P[^/]++)/?$#s', $pathinfo, $matches)) { + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ()); if (substr($pathinfo, -1) !== '/') { - return $this->redirect($pathinfo.'/', 'baz4'); + return array_replace($ret, $this->redirect($pathinfo.'/', 'baz4')); } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ()); + return $ret; } // baz5 @@ -96,7 +103,8 @@ public function match($pathinfo) goto not_baz5; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ()); + return $ret; } not_baz5: @@ -107,7 +115,8 @@ public function match($pathinfo) goto not_bazbaz6; } - return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ()); + return $ret; } not_bazbaz6: @@ -115,47 +124,55 @@ public function match($pathinfo) // foofoo if ($pathinfo === '/foofoo') { - return array ( 'def' => 'test', '_route' => 'foofoo',); + $ret = array ( 'def' => 'test', '_route' => 'foofoo',); + return $ret; } // quoter if (preg_match('#^/(?P[\']+)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ()); + return $ret; } // space if ($pathinfo === '/spa ce') { - return array('_route' => 'space'); + $ret = array('_route' => 'space'); + return $ret; } if (0 === strpos($pathinfo, '/a')) { if (0 === strpos($pathinfo, '/a/b\'b')) { // foo1 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ()); + return $ret; } // bar1 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ()); + return $ret; } } // overridden if (preg_match('#^/a/(?P.*)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ()); + return $ret; } if (0 === strpos($pathinfo, '/a/b\'b')) { // foo2 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ()); + return $ret; } // bar2 if (preg_match('#^/a/b\'b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ()); + return $ret; } } @@ -165,44 +182,51 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/multi')) { // helloWorld if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?P[^/]++))?$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',)); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',)); + return $ret; } // overridden2 if ($pathinfo === '/multi/new') { - return array('_route' => 'overridden2'); + $ret = array('_route' => 'overridden2'); + return $ret; } // hey if (rtrim($pathinfo, '/') === '/multi/hey') { + $ret = array('_route' => 'hey'); if (substr($pathinfo, -1) !== '/') { - return $this->redirect($pathinfo.'/', 'hey'); + return array_replace($ret, $this->redirect($pathinfo.'/', 'hey')); } - return array('_route' => 'hey'); + return $ret; } } // foo3 if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ()); + return $ret; } // bar3 if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ()); + return $ret; } if (0 === strpos($pathinfo, '/aba')) { // ababa if ($pathinfo === '/ababa') { - return array('_route' => 'ababa'); + $ret = array('_route' => 'ababa'); + return $ret; } // foo4 if (preg_match('#^/aba/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ()); + return $ret; } } @@ -212,12 +236,14 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route1 if ($pathinfo === '/route1') { - return array('_route' => 'route1'); + $ret = array('_route' => 'route1'); + return $ret; } // route2 if ($pathinfo === '/c2/route2') { - return array('_route' => 'route2'); + $ret = array('_route' => 'route2'); + return $ret; } } @@ -225,7 +251,8 @@ public function match($pathinfo) if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) { // route3 if ($pathinfo === '/c2/route3') { - return array('_route' => 'route3'); + $ret = array('_route' => 'route3'); + return $ret; } } @@ -233,7 +260,8 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route4 if ($pathinfo === '/route4') { - return array('_route' => 'route4'); + $ret = array('_route' => 'route4'); + return $ret; } } @@ -241,36 +269,42 @@ public function match($pathinfo) if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) { // route5 if ($pathinfo === '/route5') { - return array('_route' => 'route5'); + $ret = array('_route' => 'route5'); + return $ret; } } // route6 if ($pathinfo === '/route6') { - return array('_route' => 'route6'); + $ret = array('_route' => 'route6'); + return $ret; } if (preg_match('#^(?P[^\\.]++)\\.example\\.com$#si', $host, $hostMatches)) { if (0 === strpos($pathinfo, '/route1')) { // route11 if ($pathinfo === '/route11') { - return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ()); + $ret = $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ()); + return $ret; } // route12 if ($pathinfo === '/route12') { - return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',)); + $ret = $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',)); + return $ret; } // route13 if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ()); + $ret = $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ()); + return $ret; } // route14 if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',)); + $ret = $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',)); + return $ret; } } @@ -280,7 +314,8 @@ public function match($pathinfo) if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) { // route15 if (0 === strpos($pathinfo, '/route15') && preg_match('#^/route15/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ()); + return $ret; } } @@ -288,12 +323,14 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/route1')) { // route16 if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',)); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',)); + return $ret; } // route17 if ($pathinfo === '/route17') { - return array('_route' => 'route17'); + $ret = array('_route' => 'route17'); + return $ret; } } @@ -301,18 +338,21 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/a')) { // a if ($pathinfo === '/a/a...') { - return array('_route' => 'a'); + $ret = array('_route' => 'a'); + return $ret; } if (0 === strpos($pathinfo, '/a/b')) { // b if (preg_match('#^/a/b/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ()); + return $ret; } // c if (0 === strpos($pathinfo, '/a/b/c') && preg_match('#^/a/b/c/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ()); + return $ret; } } @@ -321,22 +361,24 @@ public function match($pathinfo) // secure if ($pathinfo === '/secure') { + $ret = array('_route' => 'secure'); $requiredSchemes = array ( 'https' => 0,); if (!isset($requiredSchemes[$this->context->getScheme()])) { - return $this->redirect($pathinfo, 'secure', key($requiredSchemes)); + return array_replace($ret, $this->redirect($pathinfo, 'secure', key($requiredSchemes))); } - return array('_route' => 'secure'); + return $ret; } // nonsecure if ($pathinfo === '/nonsecure') { + $ret = array('_route' => 'nonsecure'); $requiredSchemes = array ( 'http' => 0,); if (!isset($requiredSchemes[$this->context->getScheme()])) { - return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)); + return array_replace($ret, $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes))); } - return array('_route' => 'nonsecure'); + return $ret; } throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException(); diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php index d9da7b02d4b43..1b972feef12a6 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -30,19 +30,22 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/rootprefix')) { // static if ($pathinfo === '/rootprefix/test') { - return array('_route' => 'static'); + $ret = array('_route' => 'static'); + return $ret; } // dynamic if (preg_match('#^/rootprefix/(?P[^/]++)$#s', $pathinfo, $matches)) { - return $this->mergeDefaults(array_replace($matches, array('_route' => 'dynamic')), array ()); + $ret = $this->mergeDefaults(array_replace($matches, array('_route' => 'dynamic')), array ()); + return $ret; } } // with-condition if ($pathinfo === '/with-condition' && ($context->getMethod() == "GET")) { - return array('_route' => 'with-condition'); + $ret = array('_route' => 'with-condition'); + return $ret; } throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException(); diff --git a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php index b6c5a3e62218f..a668a3865d8aa 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php @@ -64,8 +64,22 @@ public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches() $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); $matcher ->expects($this->never()) + ->method('redirect'); + $matcher->match('/foo'); + } + + public function testRedirectWithParams() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https'))); + + $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); + $matcher + ->expects($this->once()) ->method('redirect') + ->with('/foo/baz', 'foo', 'https') + ->will($this->returnValue(array('_route' => 'foo'))) ; - $matcher->match('/foo'); + $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz')); } } From 76a03c4a884faff86193bf6483056e1208869778 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Sun, 12 Jun 2016 18:04:21 +0200 Subject: [PATCH 2/2] [Routing] also add matched params for redirect due to trailing slash --- .../Routing/RedirectableUrlMatcherTest.php | 2 +- .../Matcher/RedirectableUrlMatcher.php | 4 ++-- .../Component/Routing/Matcher/UrlMatcher.php | 8 +------ .../Matcher/RedirectableUrlMatcherTest.php | 23 +++++++++++++++---- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php index 057aa6c8091a4..4d1d1376f9224 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php @@ -32,7 +32,7 @@ public function testRedirectWhenNoSlash() 'scheme' => null, 'httpPort' => $context->getHttpPort(), 'httpsPort' => $context->getHttpsPort(), - '_route' => null, + '_route' => 'foo', ), $matcher->match('/foo') ); diff --git a/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php b/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php index 463bc0d056809..22dc4a335d98c 100644 --- a/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php @@ -32,9 +32,9 @@ public function match($pathinfo) } try { - parent::match($pathinfo.'/'); + $parameters = parent::match($pathinfo.'/'); - return $this->redirect($pathinfo.'/', null); + return array_replace($parameters, $this->redirect($pathinfo.'/', isset($parameters['_route']) ? $parameters['_route'] : null)); } catch (ResourceNotFoundException $e2) { throw $e; } diff --git a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php index c5eb836609f72..a990608918a02 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -163,17 +163,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes) $status = $this->handleRouteRequirements($pathinfo, $name, $route); - if (self::ROUTE_MATCH === $status[0]) { - $attributes = array_replace($matches, $hostMatches, (array) $status[1]); - - return $this->mergeDefaults($attributes, $route->getDefaults()); - } - if (self::REQUIREMENT_MISMATCH === $status[0]) { continue; } - return $this->getAttributes($route, $name, array_replace($matches, $hostMatches)); + return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : array())); } } diff --git a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php index a668a3865d8aa..9f1a50e2ec999 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php @@ -23,7 +23,7 @@ public function testRedirectWhenNoSlash() $coll->add('foo', new Route('/foo/')); $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); - $matcher->expects($this->once())->method('redirect'); + $matcher->expects($this->once())->method('redirect')->will($this->returnValue(array())); $matcher->match('/foo'); } @@ -68,7 +68,7 @@ public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches() $matcher->match('/foo'); } - public function testRedirectWithParams() + public function testSchemeRedirectWithParams() { $coll = new RouteCollection(); $coll->add('foo', new Route('/foo/{bar}', array(), array(), array(), '', array('https'))); @@ -78,8 +78,23 @@ public function testRedirectWithParams() ->expects($this->once()) ->method('redirect') ->with('/foo/baz', 'foo', 'https') - ->will($this->returnValue(array('_route' => 'foo'))) + ->will($this->returnValue(array('redirect' => 'value'))) + ; + $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz')); + } + + public function testSlashRedirectWithParams() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/foo/{bar}/')); + + $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); + $matcher + ->expects($this->once()) + ->method('redirect') + ->with('/foo/baz/', 'foo', null) + ->will($this->returnValue(array('redirect' => 'value'))) ; - $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz')); + $this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'redirect' => 'value'), $matcher->match('/foo/baz')); } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy