You are here

public function RequestSubscriber::onRequest in Persistent URL 8

File

src/Event/RequestSubscriber.php, line 94

Class

RequestSubscriber
ALTERNATIVE APPROACH IS ENCAPSULATE METHOD PLUGIN LOGIC WITH A PATH PROCESSOR, AND DO MOST LOGIC WITHIN THE CONFINES OF Symfony\(Cmf\)?Routing

Namespace

Drupal\purl\Event

Code

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,
      );

      //if ($methodPlugin instanceof RequestAlteringInterface) {

      //$requestAlteringMethods[] = $matchedModifiers;

      //}
    }
  }
  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);
}