protected static function ResourceResponseSubscriber::flattenResponse in Drupal 10
Same name in this branch
- 10 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
- 10 core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
Same name and namespace in other branches
- 8 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\EventSubscriber\ResourceResponseSubscriber::flattenResponse()
- 9 core/modules/jsonapi/src/EventSubscriber/ResourceResponseSubscriber.php \Drupal\jsonapi\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 deserializing the PHP data when reading this response object from cache, which can be very costly, and is unnecessary.
Parameters
\Drupal\jsonapi\ResourceResponse $response: A fully rendered resource response.
\Symfony\Component\HttpFoundation\Request $request: The request for which this response is generated.
Return value
\Drupal\Core\Cache\CacheableResponse|\Symfony\Component\HttpFoundation\Response The flattened response.
File
- core/
modules/ jsonapi/ src/ EventSubscriber/ ResourceResponseSubscriber.php, line 173
Class
- ResourceResponseSubscriber
- Response subscriber that serializes and removes ResourceResponses' data.
Namespace
Drupal\jsonapi\EventSubscriberCode
protected static function flattenResponse(ResourceResponse $response, Request $request) {
$final_response = $response instanceof CacheableResponseInterface && $request
->isMethodCacheable() ? new CacheableResponse() : new Response();
$final_response
->setContent($response
->getContent());
$final_response
->setStatusCode($response
->getStatusCode());
$final_response
->setProtocolVersion($response
->getProtocolVersion());
if ($charset = $response
->getCharset()) {
$final_response
->setCharset($charset);
}
$final_response->headers = clone $response->headers;
if ($final_response instanceof CacheableResponseInterface) {
$final_response
->addCacheableDependency($response
->getCacheableMetadata());
}
return $final_response;
}