Commit c707cf79 authored by alexpott's avatar alexpott

Issue #2004300 by dawehner: Extend the router provider interface so that Views can override routes.

parent 2635b665
......@@ -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;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\Routing\RouteProviderInterface.
*/
namespace Drupal\Core\Routing;
use Symfony\Cmf\Component\Routing\RouteProviderInterface as RouteProviderBaseInterface;
use Symfony\Component\Routing\RouteCollection;
/**
* Extends the router provider interface
*
* @see \Symfony\Cmf\Component\Routing
*/
interface RouteProviderInterface extends RouteProviderBaseInterface {
/**
* Get all routes which match a certain pattern.
*
* @param string $pattern
* The route pattern to search for (contains {} as placeholders).
*
* @return \Symfony\Component\Routing\RouteCollection
* Returns a route collection of matching routes.
*/
public function getRoutesByPattern($pattern);
}
......@@ -14,7 +14,6 @@
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
use Drupal\Component\Utility\Settings;
use Drupal\Component\Utility\UrlValidator;
......@@ -71,7 +70,7 @@ class UrlGenerator extends ProviderBasedGenerator implements PathBasedGeneratorI
/**
* Constructs a new generator object.
*
* @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $provider
* @param \Drupal\Core\Routing\RouteProviderInterface $provider
* The route provider to be searched for routes.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The alias manager responsible for path aliasing.
......
......@@ -8,9 +8,9 @@
namespace Drupal\system\Tests\Routing;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Cmf\Component\Routing\RouteProviderInterface;
use Drupal\Core\Routing\RouteProviderInterface;
/**
* Easily configurable mock route provider.
......@@ -68,4 +68,12 @@ public function getRoutesByNames($names, $parameters = array()) {
return $routes;
}
/**
* {@inheritdoc}
*/
public function getRoutesByPattern($pattern) {
return new RouteCollection();
}
}
......@@ -324,6 +324,9 @@ function testOutlinePathNoMatch() {
$request = Request::create($path, 'GET');
try {
$routes = $provider->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(array_keys($routes_by_pattern->all()), array_keys($routes->all()), 'Ensure the expected routes are found.');
foreach ($routes as $route) {
$this->assertEqual($route->getPattern(), '/path/two', 'Found path has correct pattern');
......
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