class FinishResponseSubscriber implements EventSubscriberInterface (View source)

Response subscriber to handle finished responses.

Properties

protected LanguageManagerInterface $languageManager

The language manager object for retrieving the correct language code.

protected Config $config

A config object for the system performance configuration.

protected RequestPolicyInterface $requestPolicy

A policy rule determining the cacheability of a request.

protected ResponsePolicyInterface $responsePolicy

A policy rule determining the cacheability of the response.

protected CacheContextsManager $cacheContexts

The cache contexts manager service.

protected bool $debugCacheabilityHeaders

Whether to send cacheability headers for debugging purposes.

Methods

__construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContextsManager $cache_contexts_manager, bool $http_response_debug_cacheability_headers = FALSE)

Constructs a FinishResponseSubscriber object.

onAllResponds(ResponseEvent $event)

Sets extra headers on any responses, also subrequest ones.

onRespond(ResponseEvent $event)

Sets extra headers on successful responses.

bool
isCacheControlCustomized(Response $response)

Determine whether the given response has a custom Cache-Control header.

setResponseNotCacheable(Response $response, Request $request)

Add Cache-Control and Expires headers to a response which is not cacheable.

setResponseCacheable(Response $response, Request $request)

Add Cache-Control and Expires headers to a cacheable response.

setCacheControlNoCache(Response $response)

Disable caching in the browser and for HTTP/1.1 proxies and clients.

setExpiresNoCache(Response $response)

Disable caching in ancient browsers and for HTTP/1.0 proxies and clients.

static array
getSubscribedEvents()

Registers the methods in this class that should be listeners.

Details

__construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, RequestPolicyInterface $request_policy, ResponsePolicyInterface $response_policy, CacheContextsManager $cache_contexts_manager, bool $http_response_debug_cacheability_headers = FALSE)

Constructs a FinishResponseSubscriber object.

Parameters

LanguageManagerInterface $language_manager

The language manager object for retrieving the correct language code.

ConfigFactoryInterface $config_factory

A config factory for retrieving required config objects.

RequestPolicyInterface $request_policy

A policy rule determining the cacheability of a request.

ResponsePolicyInterface $response_policy

A policy rule determining the cacheability of a response.

CacheContextsManager $cache_contexts_manager

The cache contexts manager service.

bool $http_response_debug_cacheability_headers

(optional) Whether to send cacheability headers for debugging purposes.

onAllResponds(ResponseEvent $event)

Sets extra headers on any responses, also subrequest ones.

Parameters

ResponseEvent $event

The event to process.

onRespond(ResponseEvent $event)

Sets extra headers on successful responses.

Parameters

ResponseEvent $event

The event to process.

protected bool isCacheControlCustomized(Response $response)

Determine whether the given response has a custom Cache-Control header.

Upon construction, the ResponseHeaderBag is initialized with an empty Cache-Control header. Consequently it is not possible to check whether the header was set explicitly by simply checking its presence. Instead, it is necessary to examine the computed Cache-Control header and compare with values known to be present only when Cache-Control was never set explicitly.

When neither Cache-Control nor any of the ETag, Last-Modified, Expires headers are set on the response, ::get('Cache-Control') returns the value 'no-cache, private'. If any of ETag, Last-Modified or Expires are set but not Cache-Control, then 'private, must-revalidate' (in exactly this order) is returned.

Parameters

Response $response

The response object.

Return Value

bool

TRUE when Cache-Control header was set explicitly on the given response.

See also

\Symfony\Component\HttpFoundation\ResponseHeaderBag::computeCacheControlValue()

protected setResponseNotCacheable(Response $response, Request $request)

Add Cache-Control and Expires headers to a response which is not cacheable.

Parameters

Response $response

A response object.

Request $request

A request object.

protected setResponseCacheable(Response $response, Request $request)

Add Cache-Control and Expires headers to a cacheable response.

Parameters

Response $response

A response object.

Request $request

A request object.

protected setCacheControlNoCache(Response $response)

Disable caching in the browser and for HTTP/1.1 proxies and clients.

Parameters

Response $response

A response object.

protected setExpiresNoCache(Response $response)

Disable caching in ancient browsers and for HTTP/1.0 proxies and clients.

HTTP/1.0 proxies do not support the Vary header, so prevent any caching by sending an Expires date in the past. HTTP/1.1 clients ignore the Expires header if a Cache-Control: max-age= directive is specified (see RFC 2616, section 14.9.3).

Parameters

Response $response

A response object.

static array getSubscribedEvents()

Registers the methods in this class that should be listeners.

Return Value

array

An array of event listener definitions.