You are here

class EnforcedFormResponseSubscriber in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php \Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber

Handle the EnforcedResponseException and deliver an EnforcedResponse.

Hierarchy

Expanded class hierarchy of EnforcedFormResponseSubscriber

1 string reference to 'EnforcedFormResponseSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses EnforcedFormResponseSubscriber
exception.enforced_form_response in core/core.services.yml
Drupal\Core\EventSubscriber\EnforcedFormResponseSubscriber

File

core/lib/Drupal/Core/EventSubscriber/EnforcedFormResponseSubscriber.php, line 14

Namespace

Drupal\Core\EventSubscriber
View source
class EnforcedFormResponseSubscriber implements EventSubscriberInterface {

  /**
   * Replaces the response in case an EnforcedResponseException was thrown.
   */
  public function onKernelException(ExceptionEvent $event) {
    if ($response = EnforcedResponse::createFromException($event
      ->getThrowable())) {

      // Setting the response stops the event propagation.
      $event
        ->setResponse($response);
    }
  }

  /**
   * Unwraps an enforced response.
   */
  public function onKernelResponse(ResponseEvent $event) {
    $response = $event
      ->getResponse();
    if ($response instanceof EnforcedResponse && $event
      ->isMainRequest()) {
      $event
        ->setResponse($response
        ->getResponse());
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::EXCEPTION] = [
      'onKernelException',
      128,
    ];
    $events[KernelEvents::RESPONSE] = [
      'onKernelResponse',
      128,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EnforcedFormResponseSubscriber::getSubscribedEvents public static function
EnforcedFormResponseSubscriber::onKernelException public function Replaces the response in case an EnforcedResponseException was thrown.
EnforcedFormResponseSubscriber::onKernelResponse public function Unwraps an enforced response.