Skip to content
Browse files

Speed up url matching for route without variable

  • Loading branch information...
1 parent 98db58a commit 5e948076687091b44e8f5b11ed1f3213e8211e81 @vicb vicb committed with fabpot Dec 20, 2010
View
18 src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
@@ -60,17 +60,25 @@ protected function addMatcher()
$conditions[] = sprintf("isset(\$this->context['method']) && preg_match('#^(%s)$#xi', \$this->context['method'])", $req);
}
- if ($compiledRoute->getStaticPrefix()) {
- $conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
- }
+ if (!count($compiledRoute->getVariables()) && false !== preg_match('#^(.)\^(?P<url>.*?)\$\1#', $compiledRoute->getRegex(), $m)) {
+ $conditions[] = sprintf("\$url === '%s'", $m['url']);
+
+ $matches = 'array()';
+ } else {
+ if ($compiledRoute->getStaticPrefix()) {
+ $conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
+ }
- $conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $compiledRoute->getRegex());
+ $conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $compiledRoute->getRegex());
+
+ $matches = '$matches';
+ }
$conditions = implode(' && ', $conditions);
$code[] = sprintf(<<<EOF
if ($conditions) {
- return array_merge(\$this->mergeDefaults(\$matches, %s), array('_route' => '%s'));
+ return array_merge(\$this->mergeDefaults($matches, %s), array('_route' => '%s'));
}
EOF
View
4 tests/Symfony/Tests/Component/Routing/Fixtures/dumper/url_matcher1.php
@@ -29,6 +29,10 @@ public function match($url)
return array_merge($this->mergeDefaults($matches, array ()), array('_route' => 'bar'));
}
+ if ($url === '/test/baz') {
+ return array_merge($this->mergeDefaults(array(), array ()), array('_route' => 'baz'));
+ }
+
return false;
}
}
View
4 tests/Symfony/Tests/Component/Routing/Matcher/Dumper/PhpMatcherDumperTest.php
@@ -38,6 +38,10 @@ public function testDump()
array(),
array('_method' => 'GET|head')
));
+ $collection->add('baz', new Route(
+ '/test/baz'
+ ));
+
$dumper = new PhpMatcherDumper($collection);
$this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.php', $dumper->dump(), '->dump() dumps basic routes to the correct PHP file.');
}

0 comments on commit 5e94807

Please sign in to comment.
Something went wrong with that request. Please try again.