From ea8c55ae61cd24bfcd5ab769e068b8f4acc44495 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Fri, 24 Feb 2017 23:35:18 +0100 Subject: [PATCH 01/12] Optimized dumped router matcher, prevent unneeded function calls. --- .../Matcher/Dumper/PhpMatcherDumper.php | 12 ++++++----- .../Tests/Fixtures/dumper/url_matcher1.php | 12 ++++++----- .../Tests/Fixtures/dumper/url_matcher2.php | 20 ++++++++++--------- .../Tests/Fixtures/dumper/url_matcher3.php | 2 ++ 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index c04605c3f7fef..7031a19c70f06 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -105,8 +105,10 @@ public function match(\$pathinfo) { \$allow = array(); \$pathinfo = rawurldecode(\$pathinfo); + \$trimmedPathinfo = rtrim(\$pathinfo, '/'); \$context = \$this->context; \$request = \$this->request; + \$requestMethod = \$context->getMethod(); $code @@ -133,7 +135,7 @@ private function compileRoutes(RouteCollection $routes, $supportsRedirections) foreach ($groups as $collection) { if (null !== $regex = $collection->getAttribute('host_regex')) { if (!$fetchedHost) { - $code .= " \$host = \$this->context->getHost();\n\n"; + $code .= " \$host = \$context->getHost();\n\n"; $fetchedHost = true; } @@ -227,7 +229,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P.*?)\$\1#'.(substr($regex, -1) === 'u' ? 'u' : ''), $regex, $m)) { if ($supportsTrailingSlash && substr($m['url'], -1) === '/') { - $conditions[] = sprintf("rtrim(\$pathinfo, '/') === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); + $conditions[] = sprintf("\$trimmedPathinfo === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); $hasTrailingSlash = true; } else { $conditions[] = sprintf('$pathinfo === %s', var_export(str_replace('\\', '', $m['url']), true)); @@ -266,7 +268,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren if ($methods) { if (1 === count($methods)) { $code .= <<context->getMethod() != '$methods[0]') { + if (\$requestMethod != '$methods[0]') { \$allow[] = '$methods[0]'; goto $gotoname; } @@ -276,7 +278,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren } else { $methods = implode("', '", $methods); $code .= <<context->getMethod(), array('$methods'))) { + if (!in_array(\$requestMethod, array('$methods'))) { \$allow = array_merge(\$allow, array('$methods')); goto $gotoname; } @@ -303,7 +305,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $schemes = str_replace("\n", '', var_export(array_flip($schemes), true)); $code .= <<context->getScheme()])) { + if (!isset(\$requiredSchemes[\$context->getScheme()])) { return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes)); } 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..b6e6c95454653 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -24,8 +24,10 @@ public function match($pathinfo) { $allow = array(); $pathinfo = rawurldecode($pathinfo); + $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; + $requestMethod = $context->getMethod(); // foo if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?Pbaz|symfony)$#s', $pathinfo, $matches)) { @@ -35,7 +37,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) { + if (!in_array($requestMethod, array('GET', 'HEAD'))) { $allow = array_merge($allow, array('GET', 'HEAD')); goto not_bar; } @@ -46,7 +48,7 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) { + if (!in_array($requestMethod, array('GET', 'HEAD'))) { $allow = array_merge($allow, array('GET', 'HEAD')); goto not_barhead; } @@ -83,7 +85,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($this->context->getMethod() != 'POST') { + if ($requestMethod != 'POST') { $allow[] = 'POST'; goto not_baz5; } @@ -94,7 +96,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($this->context->getMethod() != 'PUT') { + if ($requestMethod != 'PUT') { $allow[] = 'PUT'; goto not_bazbaz6; } @@ -195,7 +197,7 @@ public function match($pathinfo) } - $host = $this->context->getHost(); + $host = $context->getHost(); if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route1 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..a7e6520a91844 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -24,8 +24,10 @@ public function match($pathinfo) { $allow = array(); $pathinfo = rawurldecode($pathinfo); + $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; + $requestMethod = $context->getMethod(); // foo if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?Pbaz|symfony)$#s', $pathinfo, $matches)) { @@ -35,7 +37,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) { + if (!in_array($requestMethod, array('GET', 'HEAD'))) { $allow = array_merge($allow, array('GET', 'HEAD')); goto not_bar; } @@ -46,7 +48,7 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) { + if (!in_array($requestMethod, array('GET', 'HEAD'))) { $allow = array_merge($allow, array('GET', 'HEAD')); goto not_barhead; } @@ -70,7 +72,7 @@ public function match($pathinfo) } // baz3 - if (rtrim($pathinfo, '/') === '/test/baz3') { + if ($trimmedPathinfo === '/test/baz3') { if (substr($pathinfo, -1) !== '/') { return $this->redirect($pathinfo.'/', 'baz3'); } @@ -91,7 +93,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($this->context->getMethod() != 'POST') { + if ($requestMethod != 'POST') { $allow[] = 'POST'; goto not_baz5; } @@ -102,7 +104,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($this->context->getMethod() != 'PUT') { + if ($requestMethod != 'PUT') { $allow[] = 'PUT'; goto not_bazbaz6; } @@ -174,7 +176,7 @@ public function match($pathinfo) } // hey - if (rtrim($pathinfo, '/') === '/multi/hey') { + if ($trimmedPathinfo === '/multi/hey') { if (substr($pathinfo, -1) !== '/') { return $this->redirect($pathinfo.'/', 'hey'); } @@ -207,7 +209,7 @@ public function match($pathinfo) } - $host = $this->context->getHost(); + $host = $context->getHost(); if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route1 @@ -322,7 +324,7 @@ public function match($pathinfo) // secure if ($pathinfo === '/secure') { $requiredSchemes = array ( 'https' => 0,); - if (!isset($requiredSchemes[$this->context->getScheme()])) { + if (!isset($requiredSchemes[$context->getScheme()])) { return $this->redirect($pathinfo, 'secure', key($requiredSchemes)); } @@ -332,7 +334,7 @@ public function match($pathinfo) // nonsecure if ($pathinfo === '/nonsecure') { $requiredSchemes = array ( 'http' => 0,); - if (!isset($requiredSchemes[$this->context->getScheme()])) { + if (!isset($requiredSchemes[$context->getScheme()])) { return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)); } 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..d2cb1b1bcaf54 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -24,8 +24,10 @@ public function match($pathinfo) { $allow = array(); $pathinfo = rawurldecode($pathinfo); + $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; + $requestMethod = $context->getMethod(); if (0 === strpos($pathinfo, '/rootprefix')) { // static From 0d69cfec36e87a5b3d38b580b77129466e3f7c4d Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Fri, 24 Feb 2017 23:45:51 +0100 Subject: [PATCH 02/12] Fixed CS --- .../Component/Routing/Matcher/Dumper/PhpMatcherDumper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 7031a19c70f06..c68bed289a215 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -229,7 +229,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P.*?)\$\1#'.(substr($regex, -1) === 'u' ? 'u' : ''), $regex, $m)) { if ($supportsTrailingSlash && substr($m['url'], -1) === '/') { - $conditions[] = sprintf("\$trimmedPathinfo === %s", var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); + $conditions[] = sprintf('$trimmedPathinfo === %s', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); $hasTrailingSlash = true; } else { $conditions[] = sprintf('$pathinfo === %s', var_export(str_replace('\\', '', $m['url']), true)); From 0425f33a57f55ead17e34dd1edcec14076c05d2c Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Sat, 25 Feb 2017 02:33:46 +0100 Subject: [PATCH 03/12] Flipped around special GET HEAD handling for less expensive method comparison. --- .../Matcher/Dumper/PhpMatcherDumper.php | 38 ++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index c68bed289a215..2c1d7340ac7e3 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -108,7 +108,12 @@ public function match(\$pathinfo) \$trimmedPathinfo = rtrim(\$pathinfo, '/'); \$context = \$this->context; \$request = \$this->request; - \$requestMethod = \$context->getMethod(); + \$requestMethod = \$isLikeGetMethod = \$context->getMethod(); + + if (\$requestMethod === 'HEAD') { + \$isLikeGetMethod = 'GET'; + } + $code @@ -219,11 +224,6 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $hostMatches = false; $methods = $route->getMethods(); - // GET and HEAD are equivalent - if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { - $methods[] = 'HEAD'; - } - $supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('HEAD', $methods)); $regex = $compiledRoute->getRegex(); @@ -265,20 +265,40 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren EOF; $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name); + if ($methods) { if (1 === count($methods)) { - $code .= << Date: Sat, 25 Feb 2017 03:02:10 +0100 Subject: [PATCH 04/12] Corrected tests. --- .../Matcher/Dumper/PhpMatcherDumper.php | 2 +- .../Tests/Fixtures/dumper/url_matcher1.php | 19 ++++++++++++------- .../Tests/Fixtures/dumper/url_matcher2.php | 19 ++++++++++++------- .../Tests/Fixtures/dumper/url_matcher3.php | 7 ++++++- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 2c1d7340ac7e3..037250540221b 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -109,7 +109,7 @@ public function match(\$pathinfo) \$context = \$this->context; \$request = \$this->request; \$requestMethod = \$isLikeGetMethod = \$context->getMethod(); - + if (\$requestMethod === 'HEAD') { \$isLikeGetMethod = 'GET'; } 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 b6e6c95454653..29d3021ad830a 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -27,7 +27,12 @@ public function match($pathinfo) $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; - $requestMethod = $context->getMethod(); + $requestMethod = $isLikeGetMethod = $context->getMethod(); + + if ($requestMethod === 'HEAD') { + $isLikeGetMethod = 'GET'; + } + // foo if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?Pbaz|symfony)$#s', $pathinfo, $matches)) { @@ -37,8 +42,8 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($requestMethod, array('GET', 'HEAD'))) { - $allow = array_merge($allow, array('GET', 'HEAD')); + if (!in_array($isLikeGetMethod, array('GET'))) { + $allow = array_merge($allow, array('GET')); goto not_bar; } @@ -48,8 +53,8 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($requestMethod, array('GET', 'HEAD'))) { - $allow = array_merge($allow, array('GET', 'HEAD')); + if ($isLikeGetMethod != 'GET') { + $allow[] = 'GET'; goto not_barhead; } @@ -85,7 +90,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($requestMethod != 'POST') { + if ($isLikeGetMethod != 'POST') { $allow[] = 'POST'; goto not_baz5; } @@ -96,7 +101,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($requestMethod != 'PUT') { + if ($isLikeGetMethod != 'PUT') { $allow[] = 'PUT'; goto not_bazbaz6; } 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 a7e6520a91844..4628986484f06 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -27,7 +27,12 @@ public function match($pathinfo) $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; - $requestMethod = $context->getMethod(); + $requestMethod = $isLikeGetMethod = $context->getMethod(); + + if ($requestMethod === 'HEAD') { + $isLikeGetMethod = 'GET'; + } + // foo if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?Pbaz|symfony)$#s', $pathinfo, $matches)) { @@ -37,8 +42,8 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($requestMethod, array('GET', 'HEAD'))) { - $allow = array_merge($allow, array('GET', 'HEAD')); + if (!in_array($isLikeGetMethod, array('GET'))) { + $allow = array_merge($allow, array('GET')); goto not_bar; } @@ -48,8 +53,8 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($requestMethod, array('GET', 'HEAD'))) { - $allow = array_merge($allow, array('GET', 'HEAD')); + if ($isLikeGetMethod != 'GET') { + $allow[] = 'GET'; goto not_barhead; } @@ -93,7 +98,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($requestMethod != 'POST') { + if ($isLikeGetMethod != 'POST') { $allow[] = 'POST'; goto not_baz5; } @@ -104,7 +109,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($requestMethod != 'PUT') { + if ($isLikeGetMethod != 'PUT') { $allow[] = 'PUT'; goto not_bazbaz6; } 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 d2cb1b1bcaf54..919fcaf26f2c5 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -27,7 +27,12 @@ public function match($pathinfo) $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; - $requestMethod = $context->getMethod(); + $requestMethod = $isLikeGetMethod = $context->getMethod(); + + if ($requestMethod === 'HEAD') { + $isLikeGetMethod = 'GET'; + } + if (0 === strpos($pathinfo, '/rootprefix')) { // static From ef6ad3a7c45584e50375a49cf9ada544a951c90c Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Mon, 27 Feb 2017 17:46:42 +0100 Subject: [PATCH 05/12] Use yoda statements. --- .../Matcher/Dumper/PhpMatcherDumper.php | 10 ++-- .../Tests/Fixtures/dumper/url_matcher1.php | 42 ++++++++--------- .../Tests/Fixtures/dumper/url_matcher2.php | 46 +++++++++---------- .../Tests/Fixtures/dumper/url_matcher3.php | 4 +- 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 037250540221b..0a7e6860e46a7 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -229,10 +229,10 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren if (!count($compiledRoute->getPathVariables()) && false !== preg_match('#^(.)\^(?P.*?)\$\1#'.(substr($regex, -1) === 'u' ? 'u' : ''), $regex, $m)) { if ($supportsTrailingSlash && substr($m['url'], -1) === '/') { - $conditions[] = sprintf('$trimmedPathinfo === %s', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); + $conditions[] = sprintf('%s === $trimmedPathinfo', var_export(rtrim(str_replace('\\', '', $m['url']), '/'), true)); $hasTrailingSlash = true; } else { - $conditions[] = sprintf('$pathinfo === %s', var_export(str_replace('\\', '', $m['url']), true)); + $conditions[] = sprintf('%s === $pathinfo', var_export(str_replace('\\', '', $m['url']), true)); } } else { if ($compiledRoute->getStaticPrefix() && $compiledRoute->getStaticPrefix() !== $parentPrefix) { @@ -270,7 +270,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren if (1 === count($methods)) { if ($methods[0] === 'HEAD') { $code .= <<[^/]++)$#s', $pathinfo, $matches)) { - if ($isLikeGetMethod != 'GET') { + if ('GET' !== $isLikeGetMethod) { $allow[] = 'GET'; goto not_barhead; } @@ -67,17 +67,17 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/test')) { if (0 === strpos($pathinfo, '/test/baz')) { // baz - if ($pathinfo === '/test/baz') { + if ('/test/baz' === $pathinfo) { return array('_route' => 'baz'); } // baz2 - if ($pathinfo === '/test/baz.html') { + if ('/test/baz.html' === $pathinfo) { return array('_route' => 'baz2'); } // baz3 - if ($pathinfo === '/test/baz3/') { + if ('/test/baz3/' === $pathinfo) { return array('_route' => 'baz3'); } @@ -90,7 +90,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($isLikeGetMethod != 'POST') { + if ('POST' !== $isLikeGetMethod) { $allow[] = 'POST'; goto not_baz5; } @@ -101,7 +101,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($isLikeGetMethod != 'PUT') { + if ('PUT' !== $isLikeGetMethod) { $allow[] = 'PUT'; goto not_bazbaz6; } @@ -113,7 +113,7 @@ public function match($pathinfo) } // foofoo - if ($pathinfo === '/foofoo') { + if ('/foofoo' === $pathinfo) { return array ( 'def' => 'test', '_route' => 'foofoo',); } @@ -123,7 +123,7 @@ public function match($pathinfo) } // space - if ($pathinfo === '/spa ce') { + if ('/spa ce' === $pathinfo) { return array('_route' => 'space'); } @@ -168,12 +168,12 @@ public function match($pathinfo) } // overridden2 - if ($pathinfo === '/multi/new') { + if ('/multi/new' === $pathinfo) { return array('_route' => 'overridden2'); } // hey - if ($pathinfo === '/multi/hey/') { + if ('/multi/hey/' === $pathinfo) { return array('_route' => 'hey'); } @@ -191,7 +191,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/aba')) { // ababa - if ($pathinfo === '/ababa') { + if ('/ababa' === $pathinfo) { return array('_route' => 'ababa'); } @@ -206,12 +206,12 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route1 - if ($pathinfo === '/route1') { + if ('/route1' === $pathinfo) { return array('_route' => 'route1'); } // route2 - if ($pathinfo === '/c2/route2') { + if ('/c2/route2' === $pathinfo) { return array('_route' => 'route2'); } @@ -219,7 +219,7 @@ public function match($pathinfo) if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) { // route3 - if ($pathinfo === '/c2/route3') { + if ('/c2/route3' === $pathinfo) { return array('_route' => 'route3'); } @@ -227,7 +227,7 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route4 - if ($pathinfo === '/route4') { + if ('/route4' === $pathinfo) { return array('_route' => 'route4'); } @@ -235,26 +235,26 @@ public function match($pathinfo) if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) { // route5 - if ($pathinfo === '/route5') { + if ('/route5' === $pathinfo) { return array('_route' => 'route5'); } } // route6 - if ($pathinfo === '/route6') { + if ('/route6' === $pathinfo) { return array('_route' => 'route6'); } if (preg_match('#^(?P[^\\.]++)\\.example\\.com$#si', $host, $hostMatches)) { if (0 === strpos($pathinfo, '/route1')) { // route11 - if ($pathinfo === '/route11') { + if ('/route11' === $pathinfo) { return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ()); } // route12 - if ($pathinfo === '/route12') { + if ('/route12' === $pathinfo) { return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',)); } @@ -287,7 +287,7 @@ public function match($pathinfo) } // route17 - if ($pathinfo === '/route17') { + if ('/route17' === $pathinfo) { return array('_route' => 'route17'); } @@ -295,7 +295,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/a')) { // a - if ($pathinfo === '/a/a...') { + if ('/a/a...' === $pathinfo) { return array('_route' => 'a'); } 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 4628986484f06..adac78925f8a9 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -53,7 +53,7 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if ($isLikeGetMethod != 'GET') { + if ('GET' !== $isLikeGetMethod) { $allow[] = 'GET'; goto not_barhead; } @@ -67,17 +67,17 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/test')) { if (0 === strpos($pathinfo, '/test/baz')) { // baz - if ($pathinfo === '/test/baz') { + if ('/test/baz' === $pathinfo) { return array('_route' => 'baz'); } // baz2 - if ($pathinfo === '/test/baz.html') { + if ('/test/baz.html' === $pathinfo) { return array('_route' => 'baz2'); } // baz3 - if ($trimmedPathinfo === '/test/baz3') { + if ('/test/baz3' === $trimmedPathinfo) { if (substr($pathinfo, -1) !== '/') { return $this->redirect($pathinfo.'/', 'baz3'); } @@ -98,7 +98,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($isLikeGetMethod != 'POST') { + if ('POST' !== $isLikeGetMethod) { $allow[] = 'POST'; goto not_baz5; } @@ -109,7 +109,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ($isLikeGetMethod != 'PUT') { + if ('PUT' !== $isLikeGetMethod) { $allow[] = 'PUT'; goto not_bazbaz6; } @@ -121,7 +121,7 @@ public function match($pathinfo) } // foofoo - if ($pathinfo === '/foofoo') { + if ('/foofoo' === $pathinfo) { return array ( 'def' => 'test', '_route' => 'foofoo',); } @@ -131,7 +131,7 @@ public function match($pathinfo) } // space - if ($pathinfo === '/spa ce') { + if ('/spa ce' === $pathinfo) { return array('_route' => 'space'); } @@ -176,12 +176,12 @@ public function match($pathinfo) } // overridden2 - if ($pathinfo === '/multi/new') { + if ('/multi/new' === $pathinfo) { return array('_route' => 'overridden2'); } // hey - if ($trimmedPathinfo === '/multi/hey') { + if ('/multi/hey' === $trimmedPathinfo) { if (substr($pathinfo, -1) !== '/') { return $this->redirect($pathinfo.'/', 'hey'); } @@ -203,7 +203,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/aba')) { // ababa - if ($pathinfo === '/ababa') { + if ('/ababa' === $pathinfo) { return array('_route' => 'ababa'); } @@ -218,12 +218,12 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route1 - if ($pathinfo === '/route1') { + if ('/route1' === $pathinfo) { return array('_route' => 'route1'); } // route2 - if ($pathinfo === '/c2/route2') { + if ('/c2/route2' === $pathinfo) { return array('_route' => 'route2'); } @@ -231,7 +231,7 @@ public function match($pathinfo) if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) { // route3 - if ($pathinfo === '/c2/route3') { + if ('/c2/route3' === $pathinfo) { return array('_route' => 'route3'); } @@ -239,7 +239,7 @@ public function match($pathinfo) if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) { // route4 - if ($pathinfo === '/route4') { + if ('/route4' === $pathinfo) { return array('_route' => 'route4'); } @@ -247,26 +247,26 @@ public function match($pathinfo) if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) { // route5 - if ($pathinfo === '/route5') { + if ('/route5' === $pathinfo) { return array('_route' => 'route5'); } } // route6 - if ($pathinfo === '/route6') { + if ('/route6' === $pathinfo) { return array('_route' => 'route6'); } if (preg_match('#^(?P[^\\.]++)\\.example\\.com$#si', $host, $hostMatches)) { if (0 === strpos($pathinfo, '/route1')) { // route11 - if ($pathinfo === '/route11') { + if ('/route11' === $pathinfo) { return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ()); } // route12 - if ($pathinfo === '/route12') { + if ('/route12' === $pathinfo) { return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',)); } @@ -299,7 +299,7 @@ public function match($pathinfo) } // route17 - if ($pathinfo === '/route17') { + if ('/route17' === $pathinfo) { return array('_route' => 'route17'); } @@ -307,7 +307,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/a')) { // a - if ($pathinfo === '/a/a...') { + if ('/a/a...' === $pathinfo) { return array('_route' => 'a'); } @@ -327,7 +327,7 @@ public function match($pathinfo) } // secure - if ($pathinfo === '/secure') { + if ('/secure' === $pathinfo) { $requiredSchemes = array ( 'https' => 0,); if (!isset($requiredSchemes[$context->getScheme()])) { return $this->redirect($pathinfo, 'secure', key($requiredSchemes)); @@ -337,7 +337,7 @@ public function match($pathinfo) } // nonsecure - if ($pathinfo === '/nonsecure') { + if ('/nonsecure' === $pathinfo) { $requiredSchemes = array ( 'http' => 0,); if (!isset($requiredSchemes[$context->getScheme()])) { return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)); 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 919fcaf26f2c5..3c091137908fc 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -36,7 +36,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/rootprefix')) { // static - if ($pathinfo === '/rootprefix/test') { + if ('/rootprefix/test' === $pathinfo) { return array('_route' => 'static'); } @@ -48,7 +48,7 @@ public function match($pathinfo) } // with-condition - if ($pathinfo === '/with-condition' && ($context->getMethod() == "GET")) { + if ('/with-condition' === $pathinfo && ($context->getMethod() == "GET")) { return array('_route' => 'with-condition'); } From 44271e07e1e22650335abd15ceb434c627330ce3 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Mon, 27 Feb 2017 17:57:33 +0100 Subject: [PATCH 06/12] After filtering there's also a case where only one HTTP method is present. --- .../Routing/Matcher/Dumper/PhpMatcherDumper.php | 15 +++++++++++++-- .../Tests/Fixtures/dumper/url_matcher1.php | 4 ++-- .../Tests/Fixtures/dumper/url_matcher2.php | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 0a7e6860e46a7..49bfbb53d9a46 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -296,8 +296,18 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $methods = array_filter($methods, function ($method) { return 'HEAD' !== $method; }); } - $methods = implode("', '", $methods); - $code .= <<[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($isLikeGetMethod, array('GET'))) { - $allow = array_merge($allow, array('GET')); + if ('GET' !== $isLikeGetMethod) { + $allow[] = 'GET'; goto not_bar; } 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 adac78925f8a9..5aac52a6619cb 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -42,8 +42,8 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if (!in_array($isLikeGetMethod, array('GET'))) { - $allow = array_merge($allow, array('GET')); + if ('GET' !== $isLikeGetMethod) { + $allow[] = 'GET'; goto not_bar; } From e4a9e1ede7449dd63c86edf0b4fb06f0c58c6dae Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Mon, 27 Feb 2017 17:58:38 +0100 Subject: [PATCH 07/12] CS --- .../Component/Routing/Matcher/Dumper/PhpMatcherDumper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 49bfbb53d9a46..031e7a7d574f3 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -297,7 +297,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren } if (1 === count($methods)) { - $code .= << Date: Mon, 27 Feb 2017 18:11:03 +0100 Subject: [PATCH 08/12] Optimise schema check as well. --- .../Component/Routing/Matcher/Dumper/PhpMatcherDumper.php | 3 ++- .../Component/Routing/Tests/Fixtures/dumper/url_matcher1.php | 1 + .../Component/Routing/Tests/Fixtures/dumper/url_matcher2.php | 5 +++-- .../Component/Routing/Tests/Fixtures/dumper/url_matcher3.php | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 031e7a7d574f3..d65c63004a7f7 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -109,6 +109,7 @@ public function match(\$pathinfo) \$context = \$this->context; \$request = \$this->request; \$requestMethod = \$isLikeGetMethod = \$context->getMethod(); + \$schema = \$context->getScheme(); if (\$requestMethod === 'HEAD') { \$isLikeGetMethod = 'GET'; @@ -336,7 +337,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $schemes = str_replace("\n", '', var_export(array_flip($schemes), true)); $code .= <<getScheme()])) { + if (!isset(\$requiredSchemes[\$schema])) { return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes)); } 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 a45cc2530e854..c4e03183972a8 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -28,6 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $isLikeGetMethod = $context->getMethod(); + $schema = $context->getScheme(); if ($requestMethod === 'HEAD') { $isLikeGetMethod = 'GET'; 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 5aac52a6619cb..224336acded1f 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -28,6 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $isLikeGetMethod = $context->getMethod(); + $schema = $context->getScheme(); if ($requestMethod === 'HEAD') { $isLikeGetMethod = 'GET'; @@ -329,7 +330,7 @@ public function match($pathinfo) // secure if ('/secure' === $pathinfo) { $requiredSchemes = array ( 'https' => 0,); - if (!isset($requiredSchemes[$context->getScheme()])) { + if (!isset($requiredSchemes[$schema])) { return $this->redirect($pathinfo, 'secure', key($requiredSchemes)); } @@ -339,7 +340,7 @@ public function match($pathinfo) // nonsecure if ('/nonsecure' === $pathinfo) { $requiredSchemes = array ( 'http' => 0,); - if (!isset($requiredSchemes[$context->getScheme()])) { + if (!isset($requiredSchemes[$schema])) { return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)); } 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 3c091137908fc..2e0196b66045f 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -28,6 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $isLikeGetMethod = $context->getMethod(); + $schema = $context->getScheme(); if ($requestMethod === 'HEAD') { $isLikeGetMethod = 'GET'; From 9d92c57942ece43e7ec48f7dcd8bdf967e5179d6 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Mon, 27 Feb 2017 19:30:04 +0100 Subject: [PATCH 09/12] Added a test case which more clearly demonstrates the effect of the http method optimization, especially the HEAD case. --- .../Tests/Fixtures/dumper/url_matcher4.php | 101 ++++++++++++++++++ .../Matcher/Dumper/PhpMatcherDumperTest.php | 49 +++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php new file mode 100644 index 0000000000000..37517bf5be249 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php @@ -0,0 +1,101 @@ +context = $context; + } + + public function match($pathinfo) + { + $allow = array(); + $pathinfo = rawurldecode($pathinfo); + $trimmedPathinfo = rtrim($pathinfo, '/'); + $context = $this->context; + $request = $this->request; + $requestMethod = $isLikeGetMethod = $context->getMethod(); + $schema = $context->getScheme(); + + if ($requestMethod === 'HEAD') { + $isLikeGetMethod = 'GET'; + } + + + // just_head + if ('/just_head' === $pathinfo) { + if ('HEAD' !== $requestMethod) { + $allow[] = 'HEAD'; + goto not_just_head; + } + + return array('_route' => 'just_head'); + } + not_just_head: + + // head_and_get + if ('/head_and_get' === $pathinfo) { + if ('GET' !== $isLikeGetMethod) { + $allow[] = 'GET'; + goto not_head_and_get; + } + + return array('_route' => 'head_and_get'); + } + not_head_and_get: + + if (0 === strpos($pathinfo, '/p')) { + // post_and_head + if ('/post_and_get' === $pathinfo) { + if (!in_array($requestMethod, array('POST', 'HEAD'))) { + $allow = array_merge($allow, array('POST', 'HEAD')); + goto not_post_and_head; + } + + return array('_route' => 'post_and_head'); + } + not_post_and_head: + + if (0 === strpos($pathinfo, '/put_and_post')) { + // put_and_post + if ('/put_and_post' === $pathinfo) { + if (!in_array($requestMethod, array('PUT', 'POST'))) { + $allow = array_merge($allow, array('PUT', 'POST')); + goto not_put_and_post; + } + + return array('_route' => 'put_and_post'); + } + not_put_and_post: + + // put_and_get_and_head + if ('/put_and_post' === $pathinfo) { + if (!in_array($isLikeGetMethod, array('PUT', 'GET'))) { + $allow = array_merge($allow, array('PUT', 'GET')); + goto not_put_and_get_and_head; + } + + return array('_route' => 'put_and_get_and_head'); + } + not_put_and_get_and_head: + + } + + } + + throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException(); + } +} diff --git a/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php b/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php index cb3a1441656e4..3b1d723eb9198 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php @@ -279,10 +279,59 @@ public function getRouteCollections() $route->setCondition('context.getMethod() == "GET"'); $rootprefixCollection->add('with-condition', $route); + /* test case 4 */ + $headMatchCasesCollection = new RouteCollection(); + $headMatchCasesCollection->add('just_head', new Route( + '/just_head', + array(), + array(), + array(), + '', + array(), + array('HEAD') + )); + $headMatchCasesCollection->add('head_and_get', new Route( + '/head_and_get', + array(), + array(), + array(), + '', + array(), + array('GET', 'HEAD') + )); + $headMatchCasesCollection->add('post_and_head', new Route( + '/post_and_get', + array(), + array(), + array(), + '', + array(), + array('POST', 'HEAD') + )); + $headMatchCasesCollection->add('put_and_post', new Route( + '/put_and_post', + array(), + array(), + array(), + '', + array(), + array('PUT', 'POST') + )); + $headMatchCasesCollection->add('put_and_get_and_head', new Route( + '/put_and_post', + array(), + array(), + array(), + '', + array(), + array('PUT', 'GET', 'HEAD') + )); + return array( array($collection, 'url_matcher1.php', array()), array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')), array($rootprefixCollection, 'url_matcher3.php', array()), + array($headMatchCasesCollection, 'url_matcher4.php', array()), ); } } From f56cf9abc8c604cca372dd690503dfc793f0e43b Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Tue, 28 Feb 2017 12:30:37 +0100 Subject: [PATCH 10/12] Yoda comparison. --- .../Component/Routing/Matcher/Dumper/PhpMatcherDumper.php | 2 +- .../Component/Routing/Tests/Fixtures/dumper/url_matcher1.php | 2 +- .../Component/Routing/Tests/Fixtures/dumper/url_matcher2.php | 2 +- .../Component/Routing/Tests/Fixtures/dumper/url_matcher3.php | 2 +- .../Component/Routing/Tests/Fixtures/dumper/url_matcher4.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index d65c63004a7f7..6fd18588f7b9d 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -111,7 +111,7 @@ public function match(\$pathinfo) \$requestMethod = \$isLikeGetMethod = \$context->getMethod(); \$schema = \$context->getScheme(); - if (\$requestMethod === 'HEAD') { + if ('HEAD' === \$requestMethod) { \$isLikeGetMethod = 'GET'; } 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 c4e03183972a8..6fabb970bc6f6 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -30,7 +30,7 @@ public function match($pathinfo) $requestMethod = $isLikeGetMethod = $context->getMethod(); $schema = $context->getScheme(); - if ($requestMethod === 'HEAD') { + if ('HEAD' === $requestMethod) { $isLikeGetMethod = 'GET'; } 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 224336acded1f..fd0b603be3e88 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -30,7 +30,7 @@ public function match($pathinfo) $requestMethod = $isLikeGetMethod = $context->getMethod(); $schema = $context->getScheme(); - if ($requestMethod === 'HEAD') { + if ('HEAD' === $requestMethod) { $isLikeGetMethod = 'GET'; } 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 2e0196b66045f..83894a39480c7 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -30,7 +30,7 @@ public function match($pathinfo) $requestMethod = $isLikeGetMethod = $context->getMethod(); $schema = $context->getScheme(); - if ($requestMethod === 'HEAD') { + if ('HEAD' === $requestMethod) { $isLikeGetMethod = 'GET'; } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php index 37517bf5be249..e83995c144aac 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php @@ -30,7 +30,7 @@ public function match($pathinfo) $requestMethod = $isLikeGetMethod = $context->getMethod(); $schema = $context->getScheme(); - if ($requestMethod === 'HEAD') { + if ('HEAD' === $requestMethod) { $isLikeGetMethod = 'GET'; } From bb68154c5e432146cb50c73a67b3ee0617f1a3c0 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Tue, 28 Feb 2017 18:17:48 +0100 Subject: [PATCH 11/12] Renamed isLikeGetMethod to canonicalMethod. --- .../Routing/Matcher/Dumper/PhpMatcherDumper.php | 8 ++++---- .../Routing/Tests/Fixtures/dumper/url_matcher1.php | 12 ++++++------ .../Routing/Tests/Fixtures/dumper/url_matcher2.php | 12 ++++++------ .../Routing/Tests/Fixtures/dumper/url_matcher3.php | 4 ++-- .../Routing/Tests/Fixtures/dumper/url_matcher4.php | 8 ++++---- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 6fd18588f7b9d..89c069ab3371d 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -108,11 +108,11 @@ public function match(\$pathinfo) \$trimmedPathinfo = rtrim(\$pathinfo, '/'); \$context = \$this->context; \$request = \$this->request; - \$requestMethod = \$isLikeGetMethod = \$context->getMethod(); + \$requestMethod = \$canonicalMethod = \$context->getMethod(); \$schema = \$context->getScheme(); if ('HEAD' === \$requestMethod) { - \$isLikeGetMethod = 'GET'; + \$canonicalMethod = 'GET'; } @@ -280,7 +280,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren EOF; } else { $code .= <<context; $request = $this->request; - $requestMethod = $isLikeGetMethod = $context->getMethod(); + $requestMethod = $canonicalMethod = $context->getMethod(); $schema = $context->getScheme(); if ('HEAD' === $requestMethod) { - $isLikeGetMethod = 'GET'; + $canonicalMethod = 'GET'; } @@ -43,7 +43,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if ('GET' !== $isLikeGetMethod) { + if ('GET' !== $canonicalMethod) { $allow[] = 'GET'; goto not_bar; } @@ -54,7 +54,7 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if ('GET' !== $isLikeGetMethod) { + if ('GET' !== $canonicalMethod) { $allow[] = 'GET'; goto not_barhead; } @@ -91,7 +91,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ('POST' !== $isLikeGetMethod) { + if ('POST' !== $canonicalMethod) { $allow[] = 'POST'; goto not_baz5; } @@ -102,7 +102,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ('PUT' !== $isLikeGetMethod) { + if ('PUT' !== $canonicalMethod) { $allow[] = 'PUT'; goto not_bazbaz6; } 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 fd0b603be3e88..33cfebfb20384 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -27,11 +27,11 @@ public function match($pathinfo) $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; - $requestMethod = $isLikeGetMethod = $context->getMethod(); + $requestMethod = $canonicalMethod = $context->getMethod(); $schema = $context->getScheme(); if ('HEAD' === $requestMethod) { - $isLikeGetMethod = 'GET'; + $canonicalMethod = 'GET'; } @@ -43,7 +43,7 @@ public function match($pathinfo) if (0 === strpos($pathinfo, '/bar')) { // bar if (preg_match('#^/bar/(?P[^/]++)$#s', $pathinfo, $matches)) { - if ('GET' !== $isLikeGetMethod) { + if ('GET' !== $canonicalMethod) { $allow[] = 'GET'; goto not_bar; } @@ -54,7 +54,7 @@ public function match($pathinfo) // barhead if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P[^/]++)$#s', $pathinfo, $matches)) { - if ('GET' !== $isLikeGetMethod) { + if ('GET' !== $canonicalMethod) { $allow[] = 'GET'; goto not_barhead; } @@ -99,7 +99,7 @@ public function match($pathinfo) // baz5 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ('POST' !== $isLikeGetMethod) { + if ('POST' !== $canonicalMethod) { $allow[] = 'POST'; goto not_baz5; } @@ -110,7 +110,7 @@ public function match($pathinfo) // baz.baz6 if (preg_match('#^/test/(?P[^/]++)/$#s', $pathinfo, $matches)) { - if ('PUT' !== $isLikeGetMethod) { + if ('PUT' !== $canonicalMethod) { $allow[] = 'PUT'; goto not_bazbaz6; } 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 83894a39480c7..135f498af23a5 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -27,11 +27,11 @@ public function match($pathinfo) $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; - $requestMethod = $isLikeGetMethod = $context->getMethod(); + $requestMethod = $canonicalMethod = $context->getMethod(); $schema = $context->getScheme(); if ('HEAD' === $requestMethod) { - $isLikeGetMethod = 'GET'; + $canonicalMethod = 'GET'; } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php index e83995c144aac..5e87bfc66195f 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php @@ -27,11 +27,11 @@ public function match($pathinfo) $trimmedPathinfo = rtrim($pathinfo, '/'); $context = $this->context; $request = $this->request; - $requestMethod = $isLikeGetMethod = $context->getMethod(); + $requestMethod = $canonicalMethod = $context->getMethod(); $schema = $context->getScheme(); if ('HEAD' === $requestMethod) { - $isLikeGetMethod = 'GET'; + $canonicalMethod = 'GET'; } @@ -48,7 +48,7 @@ public function match($pathinfo) // head_and_get if ('/head_and_get' === $pathinfo) { - if ('GET' !== $isLikeGetMethod) { + if ('GET' !== $canonicalMethod) { $allow[] = 'GET'; goto not_head_and_get; } @@ -83,7 +83,7 @@ public function match($pathinfo) // put_and_get_and_head if ('/put_and_post' === $pathinfo) { - if (!in_array($isLikeGetMethod, array('PUT', 'GET'))) { + if (!in_array($canonicalMethod, array('PUT', 'GET'))) { $allow = array_merge($allow, array('PUT', 'GET')); goto not_put_and_get_and_head; } From 4ab821bc5f3bbc8db4faff97bf19fee2f91d3ad8 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Tue, 28 Feb 2017 18:27:45 +0100 Subject: [PATCH 12/12] It's scheme not schema. --- .../Component/Routing/Matcher/Dumper/PhpMatcherDumper.php | 4 ++-- .../Routing/Tests/Fixtures/dumper/url_matcher1.php | 2 +- .../Routing/Tests/Fixtures/dumper/url_matcher2.php | 6 +++--- .../Routing/Tests/Fixtures/dumper/url_matcher3.php | 2 +- .../Routing/Tests/Fixtures/dumper/url_matcher4.php | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index 89c069ab3371d..7926b625785b9 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -109,7 +109,7 @@ public function match(\$pathinfo) \$context = \$this->context; \$request = \$this->request; \$requestMethod = \$canonicalMethod = \$context->getMethod(); - \$schema = \$context->getScheme(); + \$scheme = \$context->getScheme(); if ('HEAD' === \$requestMethod) { \$canonicalMethod = 'GET'; @@ -337,7 +337,7 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $schemes = str_replace("\n", '', var_export(array_flip($schemes), true)); $code .= <<redirect(\$pathinfo, '$name', key(\$requiredSchemes)); } 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 9abd8b679e82e..60303595f1b49 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher1.php @@ -28,7 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $canonicalMethod = $context->getMethod(); - $schema = $context->getScheme(); + $scheme = $context->getScheme(); if ('HEAD' === $requestMethod) { $canonicalMethod = 'GET'; 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 33cfebfb20384..e48d9172a1e58 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -28,7 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $canonicalMethod = $context->getMethod(); - $schema = $context->getScheme(); + $scheme = $context->getScheme(); if ('HEAD' === $requestMethod) { $canonicalMethod = 'GET'; @@ -330,7 +330,7 @@ public function match($pathinfo) // secure if ('/secure' === $pathinfo) { $requiredSchemes = array ( 'https' => 0,); - if (!isset($requiredSchemes[$schema])) { + if (!isset($requiredSchemes[$scheme])) { return $this->redirect($pathinfo, 'secure', key($requiredSchemes)); } @@ -340,7 +340,7 @@ public function match($pathinfo) // nonsecure if ('/nonsecure' === $pathinfo) { $requiredSchemes = array ( 'http' => 0,); - if (!isset($requiredSchemes[$schema])) { + if (!isset($requiredSchemes[$scheme])) { return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)); } 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 135f498af23a5..48cd6dfac6c1c 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher3.php @@ -28,7 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $canonicalMethod = $context->getMethod(); - $schema = $context->getScheme(); + $scheme = $context->getScheme(); if ('HEAD' === $requestMethod) { $canonicalMethod = 'GET'; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php index 5e87bfc66195f..70d92657d9e83 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher4.php @@ -28,7 +28,7 @@ public function match($pathinfo) $context = $this->context; $request = $this->request; $requestMethod = $canonicalMethod = $context->getMethod(); - $schema = $context->getScheme(); + $scheme = $context->getScheme(); if ('HEAD' === $requestMethod) { $canonicalMethod = 'GET';