class BigPipeTestSubscriber

Same name in other branches
  1. 9 core/modules/big_pipe/tests/modules/big_pipe_test/src/EventSubscriber/BigPipeTestSubscriber.php \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber
  2. 8.9.x core/modules/big_pipe/tests/modules/big_pipe_test/src/EventSubscriber/BigPipeTestSubscriber.php \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber
  3. 11.x core/modules/big_pipe/tests/modules/big_pipe_test/src/EventSubscriber/BigPipeTestSubscriber.php \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber

Hierarchy

  • class \Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of BigPipeTestSubscriber

1 file declares its use of BigPipeTestSubscriber
BigPipeTestController.php in core/modules/big_pipe/tests/modules/big_pipe_test/src/BigPipeTestController.php
1 string reference to 'BigPipeTestSubscriber'
big_pipe_test.services.yml in core/modules/big_pipe/tests/modules/big_pipe_test/big_pipe_test.services.yml
core/modules/big_pipe/tests/modules/big_pipe_test/big_pipe_test.services.yml
1 service uses BigPipeTestSubscriber
big_pipe_test_subscriber in core/modules/big_pipe/tests/modules/big_pipe_test/big_pipe_test.services.yml
Drupal\big_pipe_test\EventSubscriber\BigPipeTestSubscriber

File

core/modules/big_pipe/tests/modules/big_pipe_test/src/EventSubscriber/BigPipeTestSubscriber.php, line 11

Namespace

Drupal\big_pipe_test\EventSubscriber
View source
class BigPipeTestSubscriber implements EventSubscriberInterface {
    
    /**
     * @see \Drupal\big_pipe_test\BigPipeTestController::responseException()
     *
     * @var string
     */
    const CONTENT_TRIGGER_EXCEPTION = 'NOPE!NOPE!NOPE!';
    
    /**
     * Triggers exception for embedded HTML/AJAX responses with certain content.
     *
     * @see \Drupal\big_pipe_test\BigPipeTestController::responseException()
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The event to process.
     *
     * @throws \Exception
     */
    public function onRespondTriggerException(ResponseEvent $event) {
        $response = $event->getResponse();
        if (!$response instanceof AttachmentsInterface) {
            return;
        }
        $attachments = $response->getAttachments();
        if (!isset($attachments['big_pipe_placeholders']) && !isset($attachments['big_pipe_nojs_placeholders'])) {
            if (str_contains($response->getContent(), static::CONTENT_TRIGGER_EXCEPTION)) {
                throw new \Exception('Oh noes!');
            }
        }
    }
    
    /**
     * Exposes all BigPipe placeholders (JS and no-JS) via headers for testing.
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The event to process.
     */
    public function onRespondSetBigPipeDebugPlaceholderHeaders(ResponseEvent $event) {
        $response = $event->getResponse();
        if (!$response instanceof HtmlResponse) {
            return;
        }
        $attachments = $response->getAttachments();
        $response->headers
            ->set('BigPipe-Test-Placeholders', '<none>');
        $response->headers
            ->set('BigPipe-Test-No-Js-Placeholders', '<none>');
        if (!empty($attachments['big_pipe_placeholders'])) {
            $response->headers
                ->set('BigPipe-Test-Placeholders', implode(' ', array_keys($attachments['big_pipe_placeholders'])));
        }
        if (!empty($attachments['big_pipe_nojs_placeholders'])) {
            $response->headers
                ->set('BigPipe-Test-No-Js-Placeholders', implode(' ', array_map('rawurlencode', array_keys($attachments['big_pipe_nojs_placeholders']))));
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // Run just before \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond().
        $events[KernelEvents::RESPONSE][] = [
            'onRespondSetBigPipeDebugPlaceholderHeaders',
            -9999,
        ];
        // Run just after \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber::onRespond().
        $events[KernelEvents::RESPONSE][] = [
            'onRespondTriggerException',
            -10001,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
BigPipeTestSubscriber::CONTENT_TRIGGER_EXCEPTION constant
BigPipeTestSubscriber::getSubscribedEvents public static function
BigPipeTestSubscriber::onRespondSetBigPipeDebugPlaceholderHeaders public function Exposes all BigPipe placeholders (JS and no-JS) via headers for testing.
BigPipeTestSubscriber::onRespondTriggerException public function Triggers exception for embedded HTML/AJAX responses with certain content.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.