Switch to a new UrlMatcher and RouterListener so that we can use a full Request object for routing purposes, and look to the attributes for our main routing information.

8.0.x
Larry Garfield 2012-04-19 01:00:47 -05:00
parent fb620e79b2
commit 47e0233492
3 changed files with 84 additions and 12 deletions

View File

@ -17,7 +17,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\RouterListener; //use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener; use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
use Drupal\Core\EventSubscriber\ViewSubscriber; use Drupal\Core\EventSubscriber\ViewSubscriber;
use Drupal\Core\EventSubscriber\AccessSubscriber; use Drupal\Core\EventSubscriber\AccessSubscriber;
@ -25,6 +25,7 @@ use Drupal\Core\EventSubscriber\PathSubscriber;
use Drupal\Core\EventSubscriber\LegacyControllerSubscriber; use Drupal\Core\EventSubscriber\LegacyControllerSubscriber;
use Drupal\Core\EventSubscriber\MaintenanceModeSubscriber; use Drupal\Core\EventSubscriber\MaintenanceModeSubscriber;
use Drupal\Core\EventSubscriber\RequestCloseSubscriber; use Drupal\Core\EventSubscriber\RequestCloseSubscriber;
use Drupal\Core\EventSubscriber\RouterListener;
use Exception; use Exception;
@ -61,8 +62,7 @@ class DrupalKernel extends HttpKernel {
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
$this->resolver = $resolver; $this->resolver = $resolver;
$context = new RequestContext(); $this->matcher = new UrlMatcher();
$this->matcher = new UrlMatcher($context);
$this->dispatcher->addSubscriber(new RouterListener($this->matcher)); $this->dispatcher->addSubscriber(new RouterListener($this->matcher));
$negotiation = new ContentNegotiation(); $negotiation = new ContentNegotiation();

View File

@ -0,0 +1,28 @@
<?php
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpKernel\EventListener\RouterListener as SymfonyRouterListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
/**
* Description of RouterListener
*
* @author crell
*/
class RouterListener extends SymfonyRouterListener {
protected $urlMatcher;
public function __construct(UrlMatcherInterface $urlMatcher, LoggerInterface $logger = null) {
parent::__construct($urlMatcher, $logger);
$this->urlMatcher = $urlMatcher;
}
public function onKernelRequest(GetResponseEvent $event) {
$this->urlMatcher->setRequest($event->getRequest());
parent::onKernelRequest($event);
}
}

View File

@ -8,8 +8,10 @@
namespace Drupal\Core; namespace Drupal\Core;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Symfony\Component\Routing\Matcher\UrlMatcher as SymfonyUrlMatcher; use Symfony\Component\Routing\Matcher\UrlMatcher as SymfonyUrlMatcher;
use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
@ -18,7 +20,7 @@ use Symfony\Component\Routing\RouteCollection;
/** /**
* UrlMatcher matches URL based on a set of routes. * UrlMatcher matches URL based on a set of routes.
*/ */
class UrlMatcher extends SymfonyUrlMatcher { class UrlMatcher implements UrlMatcherInterface {
/** /**
* The request context for this matcher. * The request context for this matcher.
@ -28,15 +30,56 @@ class UrlMatcher extends SymfonyUrlMatcher {
protected $context; protected $context;
/** /**
* Constructor. * The request object for this matcher.
* *
* @param RequestContext $context * @var Symfony\Component\HttpFoundation\Request
* The request context object.
*/ */
public function __construct(RequestContext $context) { protected $request;
public function __construct() {
// We will not actually use this object, but it's needed to conform to
// the interface.
$this->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 RequestContext $context
* The context
*
* @api
*/
public function setContext(RequestContext $context) {
$this->context = $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 RequestContext
* The context
*/
public function getContext() {
return $this->context;
}
public function setRequest(Request $request) {
$this->request = $request;
}
public function getRequest() {
return $this->request;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* *
@ -44,15 +87,16 @@ class UrlMatcher extends SymfonyUrlMatcher {
*/ */
public function match($pathinfo) { public function match($pathinfo) {
$this->allow = array(); $dpathinfo = $this->request->attributes->get('system_path');
// Symfony uses a prefixing / but we don't yet. if (!$dpathinfo) {
$dpathinfo = ltrim($pathinfo, '/'); // Symfony uses a prefixing / but we don't yet.
$dpathinfo = ltrim($pathinfo, '/');
}
// Do our fancy frontpage logic. // Do our fancy frontpage logic.
if (empty($dpathinfo)) { if (empty($dpathinfo)) {
$dpathinfo = variable_get('site_frontpage', 'user'); $dpathinfo = variable_get('site_frontpage', 'user');
$pathinfo = '/' . $dpathinfo;
} }
if ($router_item = $this->matchDrupalItem($dpathinfo)) { if ($router_item = $this->matchDrupalItem($dpathinfo)) {