You are here

class EventSubscriber in JS Callback Handler 8.3

Same name in this branch
  1. 8.3 src/EventSubscriber.php \Drupal\js\EventSubscriber
  2. 8.3 js_callback_examples/src/EventSubscriber.php \Drupal\js_callback_examples\EventSubscriber

RouteSubscriber.

Hierarchy

Expanded class hierarchy of EventSubscriber

1 string reference to 'EventSubscriber'
js.services.yml in ./js.services.yml
js.services.yml
1 service uses EventSubscriber
js.event_subscriber in ./js.services.yml
Drupal\js\EventSubscriber

File

src/EventSubscriber.php, line 16

Namespace

Drupal\js
View source
class EventSubscriber extends RouteSubscriberBase {

  /**
   * @var \Drupal\js\Js
   */
  protected $js;

  /**
   * The AJAX response attachments processor service.
   *
   * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
   */
  protected $jsAttachmentsProcessor;

  /**
   * EventSubscriber constructor.
   *
   * @param \Drupal\js\Js $js
   *   The JS Callback service.
   * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $js_attachments_processor
   *   The JS response attachments processor service.
   */
  public function __construct(Js $js, AttachmentsResponseProcessorInterface $js_attachments_processor) {
    $this->js = $js;
    $this->jsAttachmentsProcessor = $js_attachments_processor;
  }

  /**
   * {@inheritdoc}
   */
  public function alterRoutes(RouteCollection $collection) {

    // Change the  '/js' endpoint to something else.
    if (($endpoint = \Drupal::config('js.settings')
      ->get('endpoint')) && $endpoint !== '/js' && ($route = $collection
      ->get('js.callback'))) {
      $route
        ->setPath($endpoint);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = parent::getSubscribedEvents();
    $events[KernelEvents::EXCEPTION] = 'onException';
    $events[KernelEvents::REQUEST] = 'onRequest';
    $events[KernelEvents::RESPONSE][] = [
      'onResponse',
      -1000,
    ];
    return $events;
  }

  /**
   * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
   */
  public function onException(GetResponseForExceptionEvent $event) {
    $request = $event
      ->getRequest();
    if ($this->js
      ->isExecuting($request)) {
      $this->js
        ->exceptionHandler($event
        ->getException());
    }
  }

  /**
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   */
  public function onRequest(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    if ($this->js
      ->isExecuting($request)) {

      // Immediately start capturing any output.
      ob_start();

      // Override error and exception handlers to capture output.
      if (!$this->js
        ->silencePhpErrors()) {
        set_error_handler([
          $this->js,
          'errorHandler',
        ]);
        set_exception_handler([
          $this->js,
          'exceptionHandler',
        ]);
        register_shutdown_function([
          $this->js,
          'fatalErrorHandler',
        ]);
      }
    }
  }

  /**
   * Renders the ajax commands right before preparing the result.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The response event, which contains the possible AjaxResponse object.
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if ($response instanceof JsResponse) {
      $this->jsAttachmentsProcessor
        ->processAttachments($response);

      // Correct mime type.
      $response
        ->setHeader('Content-Type', $response
        ->getMimeType() . '; charset=utf-8');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EventSubscriber::$js protected property
EventSubscriber::$jsAttachmentsProcessor protected property The AJAX response attachments processor service.
EventSubscriber::alterRoutes public function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
EventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides RouteSubscriberBase::getSubscribedEvents
EventSubscriber::onException public function
EventSubscriber::onRequest public function
EventSubscriber::onResponse public function Renders the ajax commands right before preparing the result.
EventSubscriber::__construct public function EventSubscriber constructor.
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1