You are here

class ResponseSubscriber in JSON:API Include 8

Class ResponseSubscriber.

Hierarchy

  • class \Drupal\jsonapi_include\EventSubscriber\ResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of ResponseSubscriber

1 string reference to 'ResponseSubscriber'
jsonapi_include.services.yml in ./jsonapi_include.services.yml
jsonapi_include.services.yml
1 service uses ResponseSubscriber
jsonapi_include.response in ./jsonapi_include.services.yml
Drupal\jsonapi_include\EventSubscriber\ResponseSubscriber

File

src/EventSubscriber/ResponseSubscriber.php, line 17

Namespace

Drupal\jsonapi_include\EventSubscriber
View source
class ResponseSubscriber implements EventSubscriberInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $config;

  /**
   * The parse interface.
   *
   * @var \Drupal\jsonapi_include\JsonapiParseInterface
   */
  protected $jsonapiInclude;

  /**
   * The route match service.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

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

  /**
   * Set config factory.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config
   *   The config factory.
   */
  public function setConfig(ConfigFactoryInterface $config) {
    $this->config = $config;
  }

  /**
   * Set jsonapi parse.
   *
   * @param \Drupal\jsonapi_include\JsonapiParseInterface $jsonapi_include
   *   The parse interface.
   */
  public function setJsonapiInclude(JsonapiParseInterface $jsonapi_include) {
    $this->jsonapiInclude = $jsonapi_include;
  }

  /**
   * Set route match service.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The  route match service.
   */
  public function setRouteMatch(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * This method is called the KernelEvents::RESPONSE event is dispatched.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The filter event.
   */
  public function onResponse(FilterResponseEvent $event) {
    if (!$this->routeMatch
      ->getRouteObject()) {
      return;
    }
    if (Routes::isJsonApiRequest($this->routeMatch
      ->getRouteObject()
      ->getDefaults())) {
      $response = $event
        ->getResponse();
      if ($response instanceof CacheableResponseInterface) {
        $response
          ->getCacheableMetadata()
          ->addCacheContexts([
          'url.query_args:jsonapi_include',
        ]);
      }
      $need_parse = TRUE;
      if ($this->config
        ->get('jsonapi_include.settings')
        ->get('use_include_query')) {
        $need_parse = !empty($event
          ->getRequest()->query
          ->get('jsonapi_include'));
      }
      if ($need_parse) {
        $content = $event
          ->getResponse()
          ->getContent();
        if (strpos($content, '{"jsonapi"') === 0) {
          $content = $this->jsonapiInclude
            ->parse($content);
          $event
            ->getResponse()
            ->setContent($content);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ResponseSubscriber::$config protected property The config factory.
ResponseSubscriber::$jsonapiInclude protected property The parse interface.
ResponseSubscriber::$routeMatch protected property The route match service.
ResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ResponseSubscriber::onResponse public function This method is called the KernelEvents::RESPONSE event is dispatched.
ResponseSubscriber::setConfig public function Set config factory.
ResponseSubscriber::setJsonapiInclude public function Set jsonapi parse.
ResponseSubscriber::setRouteMatch public function Set route match service.