Newer
Older
Larry Garfield
committed
<?php
Niklas Fiekas
committed
* Definition of Drupal\Core\EventSubscriber\PathSubscriber.
Larry Garfield
committed
namespace Drupal\Core\EventSubscriber;
Dries Buytaert
committed
use Drupal\Core\CacheDecorator\AliasManagerCacheDecorator;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Larry Garfield
committed
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Dries Buytaert
committed
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
Larry Garfield
committed
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Access subscriber for controller requests.
*/
Larry Garfield
committed
class PathSubscriber extends PathListenerBase implements EventSubscriberInterface {
Larry Garfield
committed
Dries Buytaert
committed
protected $aliasManager;
public function __construct(AliasManagerCacheDecorator $alias_manager) {
$this->aliasManager = $alias_manager;
}
Larry Garfield
committed
/**
* Resolve the system path.
*
Niklas Fiekas
committed
* @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
Larry Garfield
committed
* The Event to process.
*/
public function onKernelRequestPathResolve(GetResponseEvent $event) {
$request = $event->getRequest();
$path = $this->extractPath($request);
Dries Buytaert
committed
$path = $this->aliasManager->getSystemPath($path);
$this->setPath($request, $path);
// If this is the master request, set the cache key for the caching of all
// system paths looked up during the request.
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
$this->aliasManager->setCacheKey($path);
}
Dries Buytaert
committed
}
/**
* Ensures system paths for the request get cached.
*/
public function onKernelTerminate(PostResponseEvent $event) {
$this->aliasManager->writeCache();
}
/**
* Resolve the front-page default path.
*
Niklas Fiekas
committed
* @todo The path system should be objectified to remove the function calls in
* this method.
Niklas Fiekas
committed
* @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Event to process.
*/
public function onKernelRequestFrontPageResolve(GetResponseEvent $event) {
$request = $event->getRequest();
$path = $this->extractPath($request);
if (empty($path)) {
// @todo Temporary hack. Fix when configuration is injectable.
$path = config('system.site')->get('page.front');
if (empty($path)) {
$path = 'user';
}
Larry Garfield
committed
$this->setPath($request, $path);
Larry Garfield
committed
}
/**
* Decode language information embedded in the request path.
*
Niklas Fiekas
committed
* @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The Event to process.
*/
public function onKernelRequestLanguageResolve(GetResponseEvent $event) {
// We need to act only on the master request, otherwise subrequests will
// inherit the main request path and an infinite loop will be started.
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
$path = _language_resolved_path();
if ($path !== NULL) {
$this->setPath($event->getRequest(), $path);
}
Dries Buytaert
committed
}
}
Larry Garfield
committed
/**
* Decodes the path of the request.
*
* Parameters in the URL sometimes represent code-meaningful strings. It is
* therefore useful to always urldecode() those values so that individual
Niklas Fiekas
committed
* controllers need not concern themselves with it. This is Drupal-specific
* logic and may not be familiar for developers used to other Symfony-family
Larry Garfield
committed
* projects.
*
* @todo Revisit whether or not this logic is appropriate for here or if
Niklas Fiekas
committed
* controllers should be required to implement this logic themselves. If we
* decide to keep this code, remove this TODO.
Larry Garfield
committed
*
Niklas Fiekas
committed
* @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
Larry Garfield
committed
* The Event to process.
*/
public function onKernelRequestDecodePath(GetResponseEvent $event) {
$request = $event->getRequest();
$path = $this->extractPath($request);
$path = urldecode($path);
$this->setPath($request, $path);
}
Larry Garfield
committed
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onKernelRequestDecodePath', 200);
$events[KernelEvents::REQUEST][] = array('onKernelRequestLanguageResolve', 150);
$events[KernelEvents::REQUEST][] = array('onKernelRequestFrontPageResolve', 101);
Larry Garfield
committed
$events[KernelEvents::REQUEST][] = array('onKernelRequestPathResolve', 100);
Dries Buytaert
committed
$events[KernelEvents::TERMINATE][] = array('onKernelTerminate', 200);
Larry Garfield
committed
return $events;
}
}