function ResourceResponseSubscriber::flattenResponse

Same name in this branch
  1. 8.9.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
Same name in other branches
  1. 9 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
  2. 9 core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
  3. 10 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
  4. 10 core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
  5. 11.x core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
  6. 11.x core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::flattenResponse()

Flattens a fully rendered resource response.

Ensures that complex data structures in ResourceResponse::getResponseData() are not serialized. Not doing this means that caching this response object requires unserializing the PHP data when reading this response object from cache, which can be very costly, and is unnecessary.

Parameters

\Drupal\rest\ResourceResponseInterface $response: A fully rendered resource response.

Return value

\Drupal\Core\Cache\CacheableResponse|\Symfony\Component\HttpFoundation\Response The flattened response.

1 call to ResourceResponseSubscriber::flattenResponse()
ResourceResponseSubscriber::onResponse in core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php
Serializes ResourceResponse responses' data, and removes that data.

File

core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php, line 205

Class

ResourceResponseSubscriber
Response subscriber that serializes and removes ResourceResponses' data.

Namespace

Drupal\rest\EventSubscriber

Code

protected function flattenResponse(ResourceResponseInterface $response) {
    $final_response = $response instanceof CacheableResponseInterface ? new CacheableResponse() : new Response();
    $final_response->setContent($response->getContent());
    $final_response->setStatusCode($response->getStatusCode());
    $final_response->setProtocolVersion($response->getProtocolVersion());
    if ($response->getCharset()) {
        $final_response->setCharset($response->getCharset());
    }
    $final_response->headers = clone $response->headers;
    if ($final_response instanceof CacheableResponseInterface) {
        $final_response->addCacheableDependency($response->getCacheableMetadata());
    }
    return $final_response;
}

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