View source
<?php
namespace Drupal\purl\Event;
use Drupal\purl\Plugin\MethodPluginManager;
use Drupal\purl\Plugin\ProviderManager;
use Drupal\purl\Plugin\ModifierIndex;
use Drupal\purl\Plugin\Purl\Method\RequestAlteringInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\purl\PurlEvents;
use Drupal\purl\MatchedModifiers;
class RequestSubscriber implements EventSubscriberInterface {
protected $methodManager;
protected $providerManager;
protected $modifierIndex;
protected $matchedModifiers;
public function __construct(ModifierIndex $modifierIndex, ProviderManager $providerManager, MethodPluginManager $methodManager, MatchedModifiers $matchedModifiers) {
$this->modifierIndex = $modifierIndex;
$this->providerManager = $providerManager;
$this->methodManager = $methodManager;
$this->matchedModifiers = $matchedModifiers;
}
public static function getSubscribedEvents() {
return array(
KernelEvents::REQUEST => array(
'onRequest',
50,
),
);
}
protected function getModifiers() {
$modifiers = $this->modifierIndex
->findModifiers();
foreach ($modifiers as $i => $modifier) {
$config = $this
->getProviderConfig($modifier['provider']);
$modifiers[$i]['method'] = $config['method'];
}
return $modifiers;
}
protected function getProviderConfig($id) {
if (!isset($this->providerConfigs[$id])) {
$this->providerConfigs[$id] = $this->providerManager
->getProviderConfiguration($id);
}
return $this->providerConfigs[$id];
}
public function onRequest(GetResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher) {
$request = $event
->getRequest();
$modifiers = $this
->getModifiers();
$matchedModifiers = array();
$requestAlteringMethods = array();
foreach ($modifiers as $modifier) {
$providerConfig = $this
->getProviderConfig($modifier['provider']);
$methodKey = $providerConfig['method'];
$modifierKey = $modifier['modifier'];
if (!$this->methodManager
->hasMethodPlugin($methodKey)) {
continue;
}
$methodPlugin = $this->methodManager
->getMethodPlugin($methodKey);
$contains = $methodPlugin
->contains($request, $modifierKey);
if ($contains) {
$matchedModifiers[] = array(
'method_plugin' => $methodPlugin,
'modifier' => $modifierKey,
'provider' => $modifier['provider'],
'value' => $modifier['value'],
'method' => $methodKey,
);
}
}
foreach ($matchedModifiers as $matched) {
if (!$matched['method_plugin'] instanceof RequestAlteringInterface) {
continue;
}
$matched['method_plugin']
->alterRequest($request, $matched['modifier']);
$this
->reinitializeRequest($request);
}
foreach ($matchedModifiers as $identifier) {
$event = new ModifierMatchedEvent($request, $identifier['provider'], $identifier['method'], $identifier['modifier'], $identifier['value']);
$dispatcher
->dispatch(PurlEvents::MODIFIER_MATCHED, $event);
$this->matchedModifiers
->add($event);
}
$request->attributes
->set('purl.matched_modifiers', $matchedModifiers);
}
private function reinitializeRequest(Request $request) {
$request
->initialize($request->query
->all(), $request->request
->all(), $request->attributes
->all(), $request->cookies
->all(), $request->files
->all(), $request->server
->all(), $request
->getContent());
}
}