Browse Source

[Routing] optimized PHP dumper when the parent prefix is the same for several adjacent collections (avoids the same test to be made)

Fabien Potencier 14 năm trước cách đây
mục cha
commit
46a93c376c

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

@@ -79,7 +79,6 @@ EOF;
         $keysCount = count($keys);
 
         $i = 0;
-
         foreach ($routeIterator as $name => $route) {
             $i++;
 
@@ -113,8 +112,10 @@ EOF;
                         }
                     }
 
-                    $code[] = sprintf("        if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
-                    $indent = '    ';
+                    if ($prefix !== $parentPrefix) {
+                        $code[] = sprintf("        if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
+                        $indent = '    ';
+                    }
                 }
 
                 foreach ($this->compileRoutes($route, $supportsRedirections, $prefix) as $line) {
@@ -123,7 +124,7 @@ EOF;
                     }
                 }
 
-                if ($optimizable) {
+                if ($optimizable && $prefix !== $parentPrefix) {
                     $code[] = "            throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new ResourceNotFoundException();";
                     $code[] = "        }\n";
                 }

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

@@ -113,6 +113,18 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
                     return $matches;
                 }
         
+                // foo1
+                if (preg_match('#^/a/b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
+                    $matches['_route'] = 'foo1';
+                    return $matches;
+                }
+        
+                // bar1
+                if (preg_match('#^/a/b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
+                    $matches['_route'] = 'bar1';
+                    return $matches;
+                }
+        
                 throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
             }
     

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

@@ -125,6 +125,18 @@ class ProjectUrlMatcher extends Symfony\Tests\Component\Routing\Fixtures\Redirec
                     return $matches;
                 }
         
+                // foo1
+                if (preg_match('#^/a/b/(?P<foo1>[^/]+?)$#x', $pathinfo, $matches)) {
+                    $matches['_route'] = 'foo1';
+                    return $matches;
+                }
+        
+                // bar1
+                if (preg_match('#^/a/b/(?P<bar1>[^/]+?)$#x', $pathinfo, $matches)) {
+                    $matches['_route'] = 'bar1';
+                    return $matches;
+                }
+        
                 throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
             }
     

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

@@ -80,6 +80,10 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
         $collection1->add('bar', new Route('/{bar}'));
         $collection2 = new RouteCollection();
         $collection2->addCollection($collection1, '/b');
+        $collection1 = new RouteCollection();
+        $collection1->add('foo1', new Route('/{foo1}'));
+        $collection1->add('bar1', new Route('/{bar1}'));
+        $collection2->addCollection($collection1, '/b');
         $collection->addCollection($collection2, '/a');
 
         // "dynamic" prefix
@@ -98,6 +102,7 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
         $collection->addCollection($collection1, '/aba');
 
         $dumper = new PhpMatcherDumper($collection, new RequestContext());
+
         $this->assertStringEqualsFile(__DIR__.'/../../Fixtures/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');
 
         // force HTTPS redirection