Speed up url matching for route without variable

This commit is contained in:
Victor Berchet 2010-12-20 18:13:02 +01:00 committed by Fabien Potencier
parent 98db58ac17
commit 5e94807668
3 changed files with 21 additions and 5 deletions

View File

@ -60,17 +60,25 @@ class PhpMatcherDumper extends MatcherDumper
$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']);
$conditions[] = sprintf("preg_match('%s', \$url, \$matches)", $compiledRoute->getRegex());
$matches = 'array()';
} else {
if ($compiledRoute->getStaticPrefix()) {
$conditions[] = sprintf("0 === strpos(\$url, '%s')", $compiledRoute->getStaticPrefix());
}
$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 File

@ -29,6 +29,10 @@ class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
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 File

@ -38,6 +38,10 @@ class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
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.');
}