فهرست منبع

[Routing] made a small optimization to the route dumper

Fabien Potencier 14 سال پیش
والد
کامیت
0dbfa18c46

+ 14 - 4
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

@@ -92,13 +92,23 @@ class PhpMatcherDumper extends MatcherDumper
 EOF;
 
             if ($req = $route->getRequirement('_method')) {
-                $req = implode('\', \'', array_map('strtolower', explode('|', $req)));
-                $code[] = <<<EOF
-            if (!in_array(\$this->context->getMethod(), array('$req'))) {
-                \$allow = array_merge(\$allow, array('$req'));
+                $methods = array_map('strtolower', explode('|', $req));
+                if (1 === count($methods)) {
+                    $code[] = <<<EOF
+            if (\$this->context->getMethod() != '$methods[0]') {
+                \$allow[] = '$methods[0]';
                 goto $gotoname;
             }
 EOF;
+                } else {
+                    $methods = implode('\', \'', $methods);
+                    $code[] = <<<EOF
+            if (!in_array(\$this->context->getMethod(), array('$methods'))) {
+                \$allow = array_merge(\$allow, array('$methods'));
+                goto $gotoname;
+            }
+EOF;
+                }
             }
 
             if ($hasTrailingSlash) {

+ 4 - 4
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php

@@ -64,8 +64,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
 
         // baz5
         if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/$#x', $pathinfo, $matches)) {
-            if (!in_array($this->context->getMethod(), array('post'))) {
-                $allow = array_merge($allow, array('post'));
+            if ($this->context->getMethod() != 'post') {
+                $allow[] = 'post';
                 goto not_baz5;
             }
             $matches['_route'] = 'baz5';
@@ -75,8 +75,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
 
         // baz.baz6
         if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/$#x', $pathinfo, $matches)) {
-            if (!in_array($this->context->getMethod(), array('put'))) {
-                $allow = array_merge($allow, array('put'));
+            if ($this->context->getMethod() != 'put') {
+                $allow[] = 'put';
                 goto not_bazbaz6;
             }
             $matches['_route'] = 'baz.baz6';

+ 4 - 4
tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher2.php

@@ -70,8 +70,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
 
         // baz5
         if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) {
-            if (!in_array($this->context->getMethod(), array('post'))) {
-                $allow = array_merge($allow, array('post'));
+            if ($this->context->getMethod() != 'post') {
+                $allow[] = 'post';
                 goto not_baz5;
             }
             if (substr($pathinfo, -1) !== '/') {
@@ -84,8 +84,8 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
 
         // baz.baz6
         if (0 === strpos($pathinfo, '/test') && preg_match('#^/test/(?P<foo>[^/\.]+?)/?$#x', $pathinfo, $matches)) {
-            if (!in_array($this->context->getMethod(), array('put'))) {
-                $allow = array_merge($allow, array('put'));
+            if ($this->context->getMethod() != 'put') {
+                $allow[] = 'put';
                 goto not_bazbaz6;
             }
             if (substr($pathinfo, -1) !== '/') {