diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index fdf7a30..bd6c475 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -7,7 +7,6 @@ namespace Drupal\Core\Routing; -use Symfony\Cmf\Component\Routing\RouteProviderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\RouteCollection; @@ -97,26 +96,7 @@ public function getRouteCollectionForRequest(Request $request) { $path = rtrim($request->getPathInfo(), '/'); } - // Filter out each empty value, though allow '0' and 0, which would be - // filtered out by empty(). - $parts = array_slice(array_filter(explode('/', $path), function($value) { - return $value !== NULL && $value !== ''; - }), 0, MatcherDumper::MAX_PARTS); - - $ancestors = $this->getCandidateOutlines($parts); - - $routes = $this->connection->query("SELECT name, route FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN (:patterns) ORDER BY fit", array( - ':patterns' => $ancestors, - )) - ->fetchAllKeyed(); - - $collection = new RouteCollection(); - foreach ($routes as $name => $route) { - $route = unserialize($route); - if (preg_match($route->compile()->getRegex(), $path, $matches)) { - $collection->add($name, $route); - } - } + $collection = $this->getRoutesByPath($path); if (!count($collection)) { throw new ResourceNotFoundException(); @@ -239,4 +219,47 @@ public function getCandidateOutlines(array $parts) { return $ancestors; } + /** + * {@inheritdoc} + */ + public function getRoutesByPattern($pattern) { + $path = RouteCompiler::getPatternOutline($pattern); + + return $this->getRoutesByPath($path); + } + + /** + * Get all routes which match a certain pattern. + * + * @param string $path + * The route pattern to search for (contains % as placeholders). + * + * @return \Symfony\Component\Routing\RouteCollection + * Returns a route collection of matching routes. + */ + protected function getRoutesByPath($path) { + // Filter out each empty value, though allow '0' and 0, which would be + // filtered out by empty(). + $parts = array_slice(array_filter(explode('/', $path), function($value) { + return $value !== NULL && $value !== ''; + }), 0, MatcherDumper::MAX_PARTS); + + $ancestors = $this->getCandidateOutlines($parts); + + $routes = $this->connection->query("SELECT name, route FROM {" . $this->connection->escapeTable($this->tableName) . "} WHERE pattern_outline IN (:patterns) ORDER BY fit", array( + ':patterns' => $ancestors, + )) + ->fetchAllKeyed(); + + $collection = new RouteCollection(); + foreach ($routes as $name => $route) { + $route = unserialize($route); + if (preg_match($route->compile()->getRegex(), $path, $matches)) { + $collection->add($name, $route); + } + } + + return $collection; + } + } diff --git a/core/lib/Drupal/Core/Routing/RouteProviderInterface.php b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php new file mode 100644 index 0000000..aa0375f --- /dev/null +++ b/core/lib/Drupal/Core/Routing/RouteProviderInterface.php @@ -0,0 +1,31 @@ +getRoutesByPattern($path); + $this->assertFalse(count($routes), 'No path found with this pattern.'); + $routes = $provider->getRouteCollectionForRequest($request); $this->fail(t('No exception was thrown.')); } @@ -348,7 +351,9 @@ function testSystemPathMatch() { $request = Request::create('/path/one', 'GET'); $request->attributes->set('system_path', 'path/two'); + $routes_by_pattern = $provider->getRoutesByPattern('/path/two'); $routes = $provider->getRouteCollectionForRequest($request); + $this->assertEqual($routes_by_pattern, $routes); foreach ($routes as $route) { $this->assertEqual($route->getPattern(), '/path/two', 'Found path has correct pattern');