From 2ed208b1e08785fa529a27ae95356ae4ee410cc9 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Mon, 28 May 2012 22:06:11 -0500 Subject: [PATCH] Add pattern outline generation to the route compilation. --- .../lib/Drupal/Core/Routing/CompiledRoute.php | 19 +++++++++++++++---- .../lib/Drupal/Core/Routing/RouteCompiler.php | 19 ++++++++++++++++++- core/modules/system/tests/routing.test | 4 ++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/core/lib/Drupal/Core/Routing/CompiledRoute.php b/core/lib/Drupal/Core/Routing/CompiledRoute.php index 52292b31123..e2e595ebb42 100644 --- a/core/lib/Drupal/Core/Routing/CompiledRoute.php +++ b/core/lib/Drupal/Core/Routing/CompiledRoute.php @@ -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; + } + } diff --git a/core/lib/Drupal/Core/Routing/RouteCompiler.php b/core/lib/Drupal/Core/Routing/RouteCompiler.php index 99dd915dc9a..8c3b7b9ef93 100644 --- a/core/lib/Drupal/Core/Routing/RouteCompiler.php +++ b/core/lib/Drupal/Core/Routing/RouteCompiler.php @@ -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. diff --git a/core/modules/system/tests/routing.test b/core/modules/system/tests/routing.test index 28eff1ea564..63cb65ec4a3 100644 --- a/core/modules/system/tests/routing.test +++ b/core/modules/system/tests/routing.test @@ -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.')); }