You are here

class RequestSubscriber in Persistent URL 8

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

Hierarchy

  • class \Drupal\purl\Event\RequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RequestSubscriber

1 string reference to 'RequestSubscriber'
purl.services.yml in ./purl.services.yml
purl.services.yml
1 service uses RequestSubscriber
purl.request_subscriber in ./purl.services.yml
Drupal\purl\Event\RequestSubscriber

File

src/Event/RequestSubscriber.php, line 21

Namespace

Drupal\purl\Event
View source
class RequestSubscriber implements EventSubscriberInterface {

  /**
   * @var MethodManager
   */
  protected $methodManager;

  /**
   * @var ProviderManager
   */
  protected $providerManager;

  /**
   * @var ModifierIndex
   */
  protected $modifierIndex;

  /**
   * @var MatchedModifiers
   */
  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(
      // RouterListener comes in at 32. We need to go before it.
      KernelEvents::REQUEST => array(
        'onRequest',
        50,
      ),
    );
  }
  protected function getModifiers() {
    $modifiers = $this->modifierIndex
      ->findModifiers();

    // This should no longer be necessary once caching issue in
    // ProviderManager is resolved.
    foreach ($modifiers as $i => $modifier) {
      $config = $this
        ->getProviderConfig($modifier['provider']);
      $modifiers[$i]['method'] = $config['method'];
    }
    return $modifiers;
  }

  /**
   * This should no longer be necessary once caching issue in
   * ProviderManager is resolved.
   *
   * @return array
   */
  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,
        );

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

  /**
   * Since the Request object is absent of APIs for modifying parts of the
   * request, we will need to run its iniitalize method to make it do it
   * itself. This will be done after a method plugin alters the server
   * attributes i.e. $request->server->set('REQUEST_URI', '/new/uri')
   *
   * I don't have a better solution that doesn't feel hacky.
   */
  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());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestSubscriber::$matchedModifiers protected property
RequestSubscriber::$methodManager protected property
RequestSubscriber::$modifierIndex protected property
RequestSubscriber::$providerManager protected property
RequestSubscriber::getModifiers protected function
RequestSubscriber::getProviderConfig protected function This should no longer be necessary once caching issue in ProviderManager is resolved.
RequestSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RequestSubscriber::onRequest public function
RequestSubscriber::reinitializeRequest private function Since the Request object is absent of APIs for modifying parts of the request, we will need to run its iniitalize method to make it do it itself. This will be done after a method plugin alters the server attributes i.e.…
RequestSubscriber::__construct public function