Add pattern outline generation to the route compilation.

8.0.x
Larry Garfield 2012-05-28 22:06:11 -05:00 committed by effulgentsia
parent a524a35d1d
commit 2ed208b1e0
3 changed files with 35 additions and 7 deletions

View File

@ -17,6 +17,13 @@ class CompiledRoute extends SymfonyCompiledRoute {
*/
protected $fit;
/**
* The pattern outline of this route.
*
* @var string
*/
protected $patternOutline;
/**
* Constructor.
*
@ -24,22 +31,26 @@ class CompiledRoute extends SymfonyCompiledRoute {
* A original Route instance.
* @param int $fit
* The fitness of the route.
* @param string $regex The regular expression to use to match this route
* @param array $tokens An array of tokens to use to generate URL for this route
* @param array $variables An array of variables
* @param string $fit
* The pattern outline for this route.
*/
public function __construct(Route $route, $fit) {
public function __construct(Route $route, $fit, $pattern_outline) {
// We're ignoring the rest of this stuff; really this should be just using
// an interface, but the Symfony component doesn't have one. This is a bug
// in Symfony.
parent::__construct($route, '', '', array(), array());
$this->fit = $fit;
$this->patternOutline = $pattern_outline;
}
public function getFit() {
return $this->fit;
}
public function getPatternOutline() {
return $this->patternOutline;
}
}

View File

@ -29,10 +29,27 @@ class RouteCompiler implements RouteCompilerInterface {
$fit = $this->getFit($route->getPattern());
return new CompiledRoute($route, $fit);
$pattern_outline = $this->getPatternOutline($route->getPattern());
return new CompiledRoute($route, $fit, $pattern_outline);
}
/**
* Returns the pattern outline.
*
* The pattern outline is the path pattern but normalized so that all
* placeholders are equal strings.
*
* @param string $path
* The path pattern to normalize to an outline.
*
* @return string
* The path pattern outline.
*/
public function getPatternOutline($path) {
return preg_replace('#\{\w+\}#', '%', $path);
}
/**
* Determines the fitness of the provided path.

View File

@ -194,13 +194,13 @@ class RouteTestCase extends WebTestBase {
}
public function testCompilation() {
$route = new Route('test/{something}/more');
$route = new Route('/test/{something}/more');
$route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
$compiled = $route->compile();
$this->assertEqual($route, $compiled->getRoute(), t('Compiled route has the correct route object.'));
$this->assertEqual($compiled->getFit(), 5 /* That's 101 binary*/, t('The fit was correct.'));
$this->assertEqual($compiled->getPatternOutline(), '/test/%/more', t('The pattern outline was correct.'));
}