[Routing] optimized PHP route dumper
This commit is contained in:
parent
7c95bda751
commit
944a98086e
@ -12,6 +12,7 @@
|
||||
namespace Symfony\Component\Routing\Matcher\Dumper;
|
||||
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
/**
|
||||
* PhpMatcherDumper creates a PHP class able to match URLs for a given set of routes.
|
||||
@ -53,9 +54,53 @@ class PhpMatcherDumper extends MatcherDumper
|
||||
|
||||
private function addMatcher($supportsRedirections)
|
||||
{
|
||||
$code = array();
|
||||
$code = implode("\n", $this->compileRoutes($this->getRoutes(), $supportsRedirections));
|
||||
|
||||
foreach ($this->getRoutes()->all() as $name => $route) {
|
||||
return <<<EOF
|
||||
|
||||
public function match(\$pathinfo)
|
||||
{
|
||||
\$allow = array();
|
||||
|
||||
$code
|
||||
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new NotFoundException();
|
||||
}
|
||||
|
||||
EOF;
|
||||
}
|
||||
|
||||
private function compileRoutes(RouteCollection $routes, $supportsRedirections)
|
||||
{
|
||||
$code = array();
|
||||
foreach ($routes as $name => $route) {
|
||||
if ($route instanceof RouteCollection) {
|
||||
$indent = '';
|
||||
if (count($route->all()) > 1 && $prefix = $route->getPrefix()) {
|
||||
$code[] = sprintf(" if (0 === strpos(\$pathinfo, '%s')) {", $prefix);
|
||||
$indent = ' ';
|
||||
}
|
||||
|
||||
foreach ($this->compileRoutes($route, $supportsRedirections) as $line) {
|
||||
foreach (explode("\n", $line) as $l) {
|
||||
$code[] = $indent.$l;
|
||||
}
|
||||
}
|
||||
|
||||
if ($indent) {
|
||||
$code[] = " }\n";
|
||||
}
|
||||
} else {
|
||||
foreach ($this->compileRoute($route, $name, $supportsRedirections) as $line) {
|
||||
$code[] = $line;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $code;
|
||||
}
|
||||
|
||||
private function compileRoute(Route $route, $name, $supportsRedirections)
|
||||
{
|
||||
$compiledRoute = $route->compile();
|
||||
$conditions = array();
|
||||
$hasTrailingSlash = false;
|
||||
@ -138,7 +183,8 @@ EOF
|
||||
$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);
|
||||
$code[] = sprintf(" \$matches['_route'] = '%s';", $name);
|
||||
$code[] = sprintf(" return \$matches;", $name);
|
||||
} elseif ($compiledRoute->getDefaults()) {
|
||||
$code[] = sprintf(' return %s;', str_replace("\n", '', var_export(array_merge($compiledRoute->getDefaults(), array('_route' => $name)), true)));
|
||||
} else {
|
||||
@ -151,21 +197,8 @@ EOF
|
||||
}
|
||||
|
||||
$code[] = '';
|
||||
}
|
||||
|
||||
$code = implode("\n", $code);
|
||||
|
||||
return <<<EOF
|
||||
|
||||
public function match(\$pathinfo)
|
||||
{
|
||||
\$allow = array();
|
||||
|
||||
$code
|
||||
throw 0 < count(\$allow) ? new MethodNotAllowedException(array_unique(\$allow)) : new NotFoundException();
|
||||
}
|
||||
|
||||
EOF;
|
||||
return $code;
|
||||
}
|
||||
|
||||
private function startClass($class, $baseClass)
|
||||
|
@ -72,9 +72,32 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
*/
|
||||
public function match($pathinfo)
|
||||
{
|
||||
$allow = array();
|
||||
$this->allow = array();
|
||||
|
||||
if ($ret = $this->matchCollection($pathinfo, $this->routes)) {
|
||||
return $ret;
|
||||
}
|
||||
|
||||
throw 0 < count($this->allow)
|
||||
? new MethodNotAllowedException(array_unique(array_map('strtolower', $this->allow)))
|
||||
: new NotFoundException();
|
||||
}
|
||||
|
||||
protected function matchCollection($pathinfo, RouteCollection $routes)
|
||||
{
|
||||
foreach ($routes as $name => $route) {
|
||||
if ($route instanceof RouteCollection) {
|
||||
if ($route->getPrefix() !== substr($pathinfo, 0, strlen($route->getPrefix()))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$ret = $this->matchCollection($pathinfo, $route)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
foreach ($this->routes->all() as $name => $route) {
|
||||
$compiledRoute = $route->compile();
|
||||
|
||||
// check the static prefix of the URL first. Only use the more expensive preg_match when it matches
|
||||
@ -88,16 +111,13 @@ class UrlMatcher implements UrlMatcherInterface
|
||||
|
||||
// check HTTP method requirement
|
||||
if ($route->getRequirement('_method') && ($req = explode('|', $route->getRequirement('_method'))) && !in_array($this->context->getMethod(), array_map('strtolower', $req))) {
|
||||
$allow = array_merge($allow, $req);
|
||||
$this->allow = array_merge($this->allow, $req);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
return array_merge($this->mergeDefaults($matches, $route->getDefaults()), array('_route' => $name));
|
||||
}
|
||||
|
||||
throw 0 < count($allow)
|
||||
? new MethodNotAllowedException(array_unique(array_map('strtolower', $allow)))
|
||||
: new NotFoundException();
|
||||
}
|
||||
|
||||
protected function mergeDefaults($params, $defaults)
|
||||
|
@ -18,10 +18,11 @@ use Symfony\Component\Config\Resource\ResourceInterface;
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*/
|
||||
class RouteCollection
|
||||
class RouteCollection implements \IteratorAggregate
|
||||
{
|
||||
private $routes;
|
||||
private $resources;
|
||||
private $prefix;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -30,6 +31,12 @@ class RouteCollection
|
||||
{
|
||||
$this->routes = array();
|
||||
$this->resources = array();
|
||||
$this->prefix = '';
|
||||
}
|
||||
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->routes);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -56,7 +63,16 @@ class RouteCollection
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->routes;
|
||||
$routes = array();
|
||||
foreach ($this->routes as $name => $route) {
|
||||
if ($route instanceof RouteCollection) {
|
||||
$routes = array_merge($routes, $route->all());
|
||||
} else {
|
||||
$routes[$name] = $route;
|
||||
}
|
||||
}
|
||||
|
||||
return $routes;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -68,7 +84,20 @@ class RouteCollection
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
return isset($this->routes[$name]) ? $this->routes[$name] : null;
|
||||
// get the latest defined route
|
||||
foreach (array_reverse($this->routes) as $routes) {
|
||||
if (!$routes instanceof RouteCollection) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (null !== $route = $routes->get($name)) {
|
||||
return $route;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->routes[$name])) {
|
||||
return $this->routes[$name];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@ -81,11 +110,7 @@ class RouteCollection
|
||||
{
|
||||
$collection->addPrefix($prefix);
|
||||
|
||||
foreach ($collection->getResources() as $resource) {
|
||||
$this->addResource($resource);
|
||||
}
|
||||
|
||||
$this->routes = array_merge($this->routes, $collection->all());
|
||||
$this->routes[] = $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,11 +124,18 @@ class RouteCollection
|
||||
return;
|
||||
}
|
||||
|
||||
$this->prefix = $prefix.$this->prefix;
|
||||
|
||||
foreach ($this->all() as $route) {
|
||||
$route->setPattern($prefix.$route->getPattern());
|
||||
}
|
||||
}
|
||||
|
||||
public function getPrefix()
|
||||
{
|
||||
return $this->prefix;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of resources loaded to build this collection.
|
||||
*
|
||||
@ -111,7 +143,14 @@ class RouteCollection
|
||||
*/
|
||||
public function getResources()
|
||||
{
|
||||
return array_unique($this->resources);
|
||||
$resources = $this->resources;
|
||||
foreach ($this as $routes) {
|
||||
if ($routes instanceof RouteCollection) {
|
||||
$resources = array_merge($resources, $routes->getResources());
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($resources);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user