Commit 2ed208b1 authored by Crell's avatar Crell Committed by effulgentsia
Browse files

Add pattern outline generation to the route compilation.

parent a524a35d
...@@ -17,6 +17,13 @@ class CompiledRoute extends SymfonyCompiledRoute { ...@@ -17,6 +17,13 @@ class CompiledRoute extends SymfonyCompiledRoute {
*/ */
protected $fit; protected $fit;
/**
* The pattern outline of this route.
*
* @var string
*/
protected $patternOutline;
/** /**
* Constructor. * Constructor.
* *
...@@ -24,22 +31,26 @@ class CompiledRoute extends SymfonyCompiledRoute { ...@@ -24,22 +31,26 @@ class CompiledRoute extends SymfonyCompiledRoute {
* A original Route instance. * A original Route instance.
* @param int $fit * @param int $fit
* The fitness of the route. * The fitness of the route.
* @param string $regex The regular expression to use to match this route * @param string $fit
* @param array $tokens An array of tokens to use to generate URL for this route * The pattern outline for this route.
* @param array $variables An array of variables
*/ */
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 // 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 // an interface, but the Symfony component doesn't have one. This is a bug
// in Symfony. // in Symfony.
parent::__construct($route, '', '', array(), array()); parent::__construct($route, '', '', array(), array());
$this->fit = $fit; $this->fit = $fit;
$this->patternOutline = $pattern_outline;
} }
public function getFit() { public function getFit() {
return $this->fit; return $this->fit;
} }
public function getPatternOutline() {
return $this->patternOutline;
}
} }
...@@ -29,10 +29,27 @@ public function compile(Route $route) { ...@@ -29,10 +29,27 @@ public function compile(Route $route) {
$fit = $this->getFit($route->getPattern()); $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. * Determines the fitness of the provided path.
......
...@@ -194,13 +194,13 @@ class RouteTestCase extends WebTestBase { ...@@ -194,13 +194,13 @@ class RouteTestCase extends WebTestBase {
} }
public function testCompilation() { public function testCompilation() {
$route = new Route('/test/{something}/more');
$route = new Route('test/{something}/more');
$route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler'); $route->setOption('compiler_class', 'Drupal\Core\Routing\RouteCompiler');
$compiled = $route->compile(); $compiled = $route->compile();
$this->assertEqual($route, $compiled->getRoute(), t('Compiled route has the correct route object.')); $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->getFit(), 5 /* That's 101 binary*/, t('The fit was correct.'));
$this->assertEqual($compiled->getPatternOutline(), '/test/%/more', t('The pattern outline was correct.'));
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment