浏览代码

[Routing] optimized the output of the PHP matcher dumper

Fabien Potencier 14 年之前
父节点
当前提交
7c0a39c353

+ 16 - 12
src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php

@@ -57,6 +57,7 @@ class PhpMatcherDumper extends MatcherDumper
             $conditions = array();
 
             $hasTrailingSlash = false;
+            $matches = false;
             if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
                 if (substr($m['url'], -1) === '/') {
                     $conditions[] = sprintf("rtrim(\$pathinfo, '/') === '%s'", rtrim(str_replace('\\', '', $m['url']), '/'));
@@ -64,8 +65,6 @@ class PhpMatcherDumper extends MatcherDumper
                 } else {
                     $conditions[] = sprintf("\$pathinfo === '%s'", str_replace('\\', '', $m['url']));
                 }
-
-                $matches = 'array()';
             } else {
                 if ($compiledRoute->getStaticPrefix()) {
                     $conditions[] = sprintf("0 === strpos(\$pathinfo, '%s')", $compiledRoute->getStaticPrefix());
@@ -80,13 +79,13 @@ class PhpMatcherDumper extends MatcherDumper
                     $conditions[] = sprintf("preg_match('%s', \$pathinfo, \$matches)", $regex);
                 }
 
-                $matches = '$matches';
+                $matches = true;
             }
 
             $conditions = implode(' && ', $conditions);
 
             $gotoname = 'not_'.preg_replace('/[^A-Za-z0-9_]/', '', $name);
-            
+
             $code[] = <<<EOF
         // $name
         if ($conditions) {
@@ -111,16 +110,21 @@ EOF
             , $name);
             }
 
-            $code[] = sprintf(<<<EOF
-            return array_merge(\$this->mergeDefaults($matches, %s), array('_route' => '%s'));
-        }
-EOF
-            , str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
+            // optimize parameters array
+            if (true === $matches && $compiledRoute->getDefaults()) {
+                $code[] = sprintf("            return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));"
+                    , str_replace("\n", '', var_export($compiledRoute->getDefaults(), true)), $name);
+            } elseif (true === $matches) {
+                $code[] = sprintf("            \$matches['_route'] = '%s';\n            return \$matches;", $name);
+            } elseif ($compiledRoute->getDefaults()) {
+                $code[] = sprintf('            return %s;', str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
+            } else {
+                $code[] = sprintf("            return array('_route' => '%s');", $name);
+            }
+            $code[] = "        }";
 
             if ($req) {
-                $code[] = <<<EOF
-        $gotoname:
-EOF;
+                $code[] = "        $gotoname:";
             }
 
             $code[] = '';

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

@@ -35,18 +35,19 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
                 $allow = array_merge($allow, array('get', 'head'));
                 goto not_bar;
             }
-            return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'bar'));
+            $matches['_route'] = 'bar';
+            return $matches;
         }
         not_bar:
 
         // baz
         if ($pathinfo === '/test/baz') {
-            return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz'));
+            return array('_route' => 'baz');
         }
 
         // baz2
         if ($pathinfo === '/test/baz.html') {
-            return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz2'));
+            return array('_route' => 'baz2');
         }
 
         // baz3
@@ -54,7 +55,7 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
             if (substr($pathinfo, -1) !== '/') {
                 return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz3');
             }
-            return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz3'));
+            return array('_route' => 'baz3');
         }
 
         // baz4
@@ -62,7 +63,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
             if (substr($pathinfo, -1) !== '/') {
                 return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz4');
             }
-            return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz4'));
+            $matches['_route'] = 'baz4';
+            return $matches;
         }
 
         // baz5
@@ -74,7 +76,8 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
             if (substr($pathinfo, -1) !== '/') {
                 return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz5');
             }
-            return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz5'));
+            $matches['_route'] = 'baz5';
+            return $matches;
         }
         not_baz5:
 
@@ -87,10 +90,16 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
             if (substr($pathinfo, -1) !== '/') {
                 return array('_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', 'url' => $this->context['base_url'].$pathinfo.'/', 'permanent' => true, '_route' => 'baz.baz6');
             }
-            return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'baz.baz6'));
+            $matches['_route'] = 'baz.baz6';
+            return $matches;
         }
         not_bazbaz6:
 
+        // foofoo
+        if ($pathinfo === '/foofoo') {
+            return array (  'def' => 'test',  '_route' => 'foofoo',);
+        }
+
         throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new NotFoundException();
     }
 }

+ 5 - 0
tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php

@@ -68,6 +68,11 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
             array(),
             array('_method' => 'put')
         ));
+        // defaults without variable
+        $collection->add('foofoo', new Route(
+            '/foofoo',
+            array('def' => 'test')
+        ));
 
         $dumper = new PhpMatcherDumper($collection);
         $this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');