You are here

class RabbitHoleSubscriber in Rabbit Hole 2.x

Same name and namespace in other branches
  1. 8 src/EventSubscriber/RabbitHoleSubscriber.php \Drupal\rabbit_hole\EventSubscriber\RabbitHoleSubscriber

Class EventSubscriber.

@package Drupal\rabbit_hole

Hierarchy

  • class \Drupal\rabbit_hole\EventSubscriber\RabbitHoleSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RabbitHoleSubscriber

1 string reference to 'RabbitHoleSubscriber'
rabbit_hole.services.yml in ./rabbit_hole.services.yml
rabbit_hole.services.yml
1 service uses RabbitHoleSubscriber
rabbit_hole.rabbit_hole_subscriber in ./rabbit_hole.services.yml
Drupal\rabbit_hole\EventSubscriber\RabbitHoleSubscriber

File

src/EventSubscriber/RabbitHoleSubscriber.php, line 16

Namespace

Drupal\rabbit_hole\EventSubscriber
View source
class RabbitHoleSubscriber implements EventSubscriberInterface {

  /**
   * Drupal\rabbit_hole\BehaviorInvoker definition.
   *
   * @var \Drupal\rabbit_hole\BehaviorInvoker
   */
  protected $rabbitHoleBehaviorInvoker;

  /**
   * Constructor.
   */
  public function __construct(BehaviorInvoker $rabbit_hole_behavior_invoker) {
    $this->rabbitHoleBehaviorInvoker = $rabbit_hole_behavior_invoker;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['kernel.request'] = [
      'onRequest',
      28,
    ];
    $events['kernel.response'] = [
      'onResponse',
    ];
    return $events;
  }

  /**
   * A method to be called whenever a kernel.request event is dispatched.
   *
   * It invokes a rabbit hole behavior on an entity in the request if
   * applicable.
   *
   * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
   *   The event triggered by the request.
   */
  public function onRequest(KernelEvent $event) {
    $this
      ->processEvent($event);
  }

  /**
   * A method to be called whenever a kernel.response event is dispatched.
   *
   * Like the onRequest event, it invokes a rabbit hole behavior on an entity in
   * the request if possible. Unlike the onRequest event, it also passes in a
   * response.
   *
   * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
   *   The event triggered by the response.
   */
  public function onResponse(KernelEvent $event) {
    $this
      ->processEvent($event);
  }

  /**
   * Process events generically invoking rabbit hole behaviors if necessary.
   *
   * @param \Symfony\Component\HttpKernel\Event\KernelEvent $event
   *   The event to process.
   */
  private function processEvent(KernelEvent $event) {
    if ($entity = $this->rabbitHoleBehaviorInvoker
      ->getEntity($event)) {
      try {
        $new_response = $this->rabbitHoleBehaviorInvoker
          ->processEntity($entity, $event
          ->getResponse());
        if ($new_response instanceof Response) {
          $event
            ->setResponse($new_response);
        }
      } catch (PluginException $e) {

        // Do nothing if we got plugin-related exception.
        // Other exceptions (i.e. AccessDeniedHttpException) should be accepted.
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RabbitHoleSubscriber::$rabbitHoleBehaviorInvoker protected property Drupal\rabbit_hole\BehaviorInvoker definition.
RabbitHoleSubscriber::getSubscribedEvents public static function
RabbitHoleSubscriber::onRequest public function A method to be called whenever a kernel.request event is dispatched.
RabbitHoleSubscriber::onResponse public function A method to be called whenever a kernel.response event is dispatched.
RabbitHoleSubscriber::processEvent private function Process events generically invoking rabbit hole behaviors if necessary.
RabbitHoleSubscriber::__construct public function Constructor.