Skip to content

Commit

Permalink
[Router] added static requirement optimization to php matcher dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Sep 7, 2013
1 parent 76b6449 commit 5a720f2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php
Expand Up @@ -193,6 +193,8 @@ private function compilePrefixRoutes(DumperPrefixCollection $collection, $suppor
*/
private function compileRoute(Route $route, $name, $supportsRedirections, $parentPrefix = null)
{
$route = $this->optimizeRoute($route);

$code = '';
$compiledRoute = $route->compile();
$conditions = array();
Expand Down Expand Up @@ -375,4 +377,34 @@ private function buildPrefixTree(DumperCollection $collection)

return $tree;
}

/**
* Optimizes route before the dump.
*
* @param Route $route
*
* @return Route
*/
private function optimizeRoute(Route $route)
{
$optimizedRoute = clone $route;
$optimized = false;

foreach ($route->getRequirements() as $parameter => $pattern) {
$placeholder = sprintf('{%s}', $parameter);

if (false !== strpos($optimizedRoute->getPath(), $placeholder) && preg_match('~^[\w]+$~i', $pattern)) {
$optimizedRoute->setPath(str_replace($placeholder, $pattern, $optimizedRoute->getPath()));

$requirements = $optimizedRoute->getRequirements();
unset($requirements[$parameter]);
$optimizedRoute->setRequirements($requirements);
$optimizedRoute->setDefault($parameter, $pattern);

$optimized = true;
}
}

return $optimized ? $optimizedRoute : $route;
}
}
@@ -0,0 +1,35 @@
<?php

use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;

/**
* ProjectUrlMatcher
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
{
/**
* Constructor.
*/
public function __construct(RequestContext $context)
{
$this->context = $context;
}

public function match($pathinfo)
{
$allow = array();
$pathinfo = rawurldecode($pathinfo);

// name
if ($pathinfo === '/test/static') {
return array ( 'param' => 'static', '_route' => 'name',);
}

throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}
}
Expand Up @@ -258,4 +258,14 @@ public function getRouteCollections()
array($rootprefixCollection, 'url_matcher3.php', array())
);
}

public function testOptimizedRouteDump()
{
$collection = new RouteCollection();
$collection->add('name', new Route('/test/{param}', array(), array('param' => 'static')));
$dumper = new PhpMatcherDumper($collection);
$fixture = __DIR__.'/../../Fixtures/dumper/optimized_route_matcher.php';

$this->assertStringEqualsFile($fixture, $dumper->dump());
}
}

0 comments on commit 5a720f2

Please sign in to comment.