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'
1 service uses ResponseSubscriber
File
- src/
EventSubscriber/ ResponseSubscriber.php, line 17
Namespace
Drupal\jsonapi_include\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ResponseSubscriber:: |
protected | property | The config factory. | |
ResponseSubscriber:: |
protected | property | The parse interface. | |
ResponseSubscriber:: |
protected | property | The route match service. | |
ResponseSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
ResponseSubscriber:: |
public | function | This method is called the KernelEvents::RESPONSE event is dispatched. | |
ResponseSubscriber:: |
public | function | Set config factory. | |
ResponseSubscriber:: |
public | function | Set jsonapi parse. | |
ResponseSubscriber:: |
public | function | Set route match service. |