diff --git a/core/lib/Drupal/Core/Routing/HttpMethodMatcher.php b/core/lib/Drupal/Core/Routing/HttpMethodMatcher.php new file mode 100644 index 000000000000..54ce82eaf89d --- /dev/null +++ b/core/lib/Drupal/Core/Routing/HttpMethodMatcher.php @@ -0,0 +1,44 @@ +routes = $routes; + } + + /** + * Matches a request against multiple routes. + * + * @param Request $request + * A Request object against which to match. + * + * @return RouteCollection + * A RouteCollection of matched routes. + */ + public function matchByRequest(Request $request) { + + $method = $request->getMethod(); + + $collection = new RouteCollection(); + + foreach ($this->routes->all() as $name => $route) { + $allowed_methods = $route->getRequirement('_method'); + if ($allowed_methods === NULL || in_array($method, explode('|', strtoupper($allowed_methods)))) { + $collection->add($name, $route); + } + } + return $collection; + } + +} + diff --git a/core/lib/Drupal/Core/Routing/NestedMatcherInterface.php b/core/lib/Drupal/Core/Routing/NestedMatcherInterface.php new file mode 100644 index 000000000000..ec9664ec5562 --- /dev/null +++ b/core/lib/Drupal/Core/Routing/NestedMatcherInterface.php @@ -0,0 +1,36 @@ +context = new RequestContext(); + } + + /** + * Sets the request context. + * + * This method is just to satisfy the interface, and is largely vestigial. + * The request context object does not contain the information we need, so + * we will use the original request object. + * + * @param Symfony\Component\Routing\RequestContext $context + * The context. + * + * @api + */ + public function setContext(RequestContext $context) { + $this->context = $context; + } + + /** + * Gets the request context. + * + * This method is just to satisfy the interface, and is largely vestigial. + * The request context object does not contain the information we need, so + * we will use the original request object. + * + * @return Symfony\Component\Routing\RequestContext + * The context. + */ + public function getContext() { + return $this->context; + } + + /** + * Sets the request object to use. + * + * This is used by the RouterListener to make additional request attributes + * available. + * + * @param Symfony\Component\HttpFoundation\Request $request + * The request object. + */ + public function setRequest(Request $request) { + $this->request = $request; + } + + /** + * Gets the request object. + * + * @return Symfony\Component\HttpFoundation\Request $request + * The request object. + */ + public function getRequest() { + return $this->request; + } + + public function match($pathinfo) { + + } + +} \ No newline at end of file diff --git a/core/lib/Drupal/Core/Routing/UrlMatcherDumper.php b/core/lib/Drupal/Core/Routing/UrlMatcherDumper.php index 9a0bf0626cff..f7969aa8bf40 100644 --- a/core/lib/Drupal/Core/Routing/UrlMatcherDumper.php +++ b/core/lib/Drupal/Core/Routing/UrlMatcherDumper.php @@ -64,12 +64,12 @@ class UrlMatcherDumper implements MatcherDumperInterface { * * Available options: * - * * class: The class name + * * route_set: The route grouping that is being dumped. All existing + * routes with this route set will be deleted on dump. * * base_class: The base class name * - * @param array $options An array of options - * - * @return string A PHP class representing the matcher class + * @param $options array + * $options An array of options */ public function dump(array $options = array()) { $options += array( @@ -88,8 +88,6 @@ class UrlMatcherDumper implements MatcherDumperInterface { 'route', )); - - foreach ($this->routes as $name => $route) { $compiled = $route->compile(); $values = array( @@ -115,13 +113,14 @@ class UrlMatcherDumper implements MatcherDumperInterface { $insert->execute(); // Transaction ends here. - } /** * Gets the routes to match. * - * @return RouteCollection A RouteCollection instance + * @return RouteCollection + * A RouteCollection instance representing all routes currently in the + * dumper. */ public function getRoutes() { return $this->routes; diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/PartialMatcherTest.php b/core/modules/system/lib/Drupal/system/Tests/Routing/PartialMatcherTest.php new file mode 100644 index 000000000000..9e73678efa9e --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/PartialMatcherTest.php @@ -0,0 +1,69 @@ + 'Partial matcher HTTP Method tests', + 'description' => 'Confirm that the Http Method partial matcher is functioning properly.', + 'group' => 'Routing', + ); + } + + function setUp() { + parent::setUp(); + } + + /** + * Confirms that the HttpMethod matcher matches properly. + */ + function testFilterRoutes() { + $collection = new RouteCollection(); + + $route = new Route('path/one'); + $route->setRequirement('_method', 'GET'); + $collection->add('route_a', $route); + + $route = new Route('path/one'); + $route->setRequirement('_method', 'PUT'); + $collection->add('route_b', $route); + + $route = new Route('path/two'); + $route->setRequirement('_method', 'GET'); + $collection->add('route_c', $route); + + $route = new Route('path/three'); + $collection->add('route_d', $route); + + $route = new Route('path/two'); + $route->setRequirement('_method', 'GET|HEAD'); + $collection->add('route_e', $route); + + $matcher = new HttpMethodMatcher($collection, 'GET'); + + $routes = $matcher->matchByRequest(Request::create('path/one', 'GET')); + + $this->assertEqual(count($routes->all()), 4, t('The correct number of routes was found.')); + $this->assertNotNull($routes->get('route_a'), t('The first matching route was found.')); + $this->assertNull($routes->get('route_b'), t('The non-matching route was not found.')); + $this->assertNotNull($routes->get('route_c'), t('The second matching route was found.')); + $this->assertNotNull($routes->get('route_d'), t('The all-matching route was found.')); + $this->assertNotNull($routes->get('route_e'), t('The multi-matching route was found.')); + } +} +