class ResourceResponseSubscriber implements EventSubscriberInterface (View source)

Response subscriber that serializes and removes ResourceResponses' data.

Properties

protected SerializerInterface $serializer

The serializer.

protected RendererInterface $renderer

The renderer.

protected RouteMatchInterface $routeMatch

The current route match.

Methods

__construct(SerializerInterface $serializer, RendererInterface $renderer, RouteMatchInterface $route_match)

Constructs a ResourceResponseSubscriber object.

onResponse(ResponseEvent $event)

Serializes ResourceResponse responses' data, and removes that data.

string
getResponseFormat(RouteMatchInterface $route_match, Request $request)

Determines the format to respond in.

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

Renders a resource response body.

flattenResponse(ResourceResponseInterface $response)

Flattens a fully rendered resource response.

static 
getSubscribedEvents()

{@inheritdoc}

Details

__construct(SerializerInterface $serializer, RendererInterface $renderer, RouteMatchInterface $route_match)

Constructs a ResourceResponseSubscriber object.

Parameters

SerializerInterface $serializer

The serializer.

RendererInterface $renderer

The renderer.

RouteMatchInterface $route_match

The current route match.

onResponse(ResponseEvent $event)

Serializes ResourceResponse responses' data, and removes that data.

Parameters

ResponseEvent $event

The event to process.

string getResponseFormat(RouteMatchInterface $route_match, Request $request)

Determines the format to respond in.

Respects the requested format if one is specified. However, it is common to forget to specify a response format in case of a POST or PATCH. Rather than simply throwing an error, we apply the robustness principle: when POSTing or PATCHing using a certain format, you probably expect a response in that same format.

Parameters

RouteMatchInterface $route_match

The current route match.

Request $request

The current request.

Return Value

string

The response format.

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

Renders a resource response body.

During serialization, encoders and normalizers are able to explicitly bubble cacheability metadata via the 'cacheability' key-value pair in the received context. This bubbled cacheability metadata will be applied to the the response.

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

Parameters

Request $request

The request object.

ResourceResponseInterface $response

The response from the REST 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.

protected CacheableResponse|Response flattenResponse(ResourceResponseInterface $response)

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

ResourceResponseInterface $response

A fully rendered resource response.

Return Value

CacheableResponse|Response

The flattened response.

static getSubscribedEvents()

{@inheritdoc}