class ResourceResponseSubscriber implements EventSubscriberInterface (View source)

internal  JSON:API maintains no PHP API. The API is the HTTP API. This class may change at any time and could break any dependencies on it.
 

Response subscriber that serializes and removes ResourceResponses' data.

Properties

protected SerializerInterface $serializer

The serializer.

Methods

__construct(SerializerInterface $serializer)

Constructs a ResourceResponseSubscriber object.

static array
getSubscribedEvents()

{@inheritdoc}

onResponse(ResponseEvent $event)

Serializes ResourceResponse responses' data, and removes that data.

renderResponseBody(Request $request, ResourceResponse $response, SerializerInterface $serializer, string|null $format)

Renders a resource response body.

static array
generateContext(Request $request)

Generates a top-level JSON:API normalization context.

static CacheableResponse|Response
flattenResponse(ResourceResponse $response, Request $request)

Flattens a fully rendered resource response.

Details

__construct(SerializerInterface $serializer)

Constructs a ResourceResponseSubscriber object.

Parameters

SerializerInterface $serializer

The serializer.

static array getSubscribedEvents()

onResponse(ResponseEvent $event)

Serializes ResourceResponse responses' data, and removes that data.

Parameters

ResponseEvent $event

The event to process.

protected renderResponseBody(Request $request, ResourceResponse $response, SerializerInterface $serializer, string|null $format)

Renders a resource response body.

Serialization can invoke rendering (e.g., generating URLs), but the serialization API does not provide a mechanism to collect the bubbleable metadata associated with that (e.g., language and other contexts), so instead, allow those to "leak" and collect them here in a render context.

Add test coverage for language negotiation contexts in https://www.drupal.org/node/2135829.

Parameters

Request $request

The request object.

ResourceResponse $response

The response from the JSON:API resource.

SerializerInterface $serializer

The serializer to use.

string|null $format

The response format, or NULL in case the response does not need a format, for example for the response to a DELETE request.

static protected array generateContext(Request $request)

Generates a top-level JSON:API normalization context.

Parameters

Request $request

The request from which the context can be derived.

Return Value

array

The generated context.

static protected CacheableResponse|Response flattenResponse(ResourceResponse $response, Request $request)

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

ResourceResponse $response

A fully rendered resource response.

Request $request

The request for which this response is generated.

Return Value

CacheableResponse|Response

The flattened response.