3 namespace Drupal\pbt_entity_test\EventSubscriber;
5 use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber;
6 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
7 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10 * Mocked dynamic page cache subscriber.
12 * Ensures the requests are cached.
14 class MockedDynamicPageCacheSubscriber extends DynamicPageCacheSubscriber {
19 public function onRouteMatch(GetResponseEvent $event) {
20 // Sets the response for the current route, if cached.
21 $cached = $this->renderCache->get($this->dynamicPageCacheRedirectRenderArray);
23 $response = $this->renderArrayToResponse($cached);
24 $response->headers->set(self::HEADER, 'HIT');
25 $event->setResponse($response);
32 public function onResponse(FilterResponseEvent $event) {
33 $response = $event->getResponse();
35 // Embed the response object in a render array so that RenderCache is able
36 // to cache it, handling cache redirection for us.
37 $response_as_render_array = $this->responseToRenderArray($response);
38 $this->renderCache->set($response_as_render_array, $this->dynamicPageCacheRedirectRenderArray);
40 // The response was generated, mark the response as a cache miss. The next
41 // time, it will be a cache hit.
42 $response->headers->set(self::HEADER, 'MISS');