You are here

class MockedDynamicPageCacheSubscriber in Permissions by Term 8

Same name and namespace in other branches
  1. 8.2 modules/permissions_by_entity/tests/modules/pbt_entity_test/src/EventSubscriber/MockedDynamicPageCacheSubscriber.php \Drupal\pbt_entity_test\EventSubscriber\MockedDynamicPageCacheSubscriber

Mocked dynamic page cache subscriber.

Ensures the requests are cached.

Hierarchy

Expanded class hierarchy of MockedDynamicPageCacheSubscriber

1 string reference to 'MockedDynamicPageCacheSubscriber'
pbt_entity_test.services.yml in modules/permissions_by_entity/tests/modules/pbt_entity_test/pbt_entity_test.services.yml
modules/permissions_by_entity/tests/modules/pbt_entity_test/pbt_entity_test.services.yml
1 service uses MockedDynamicPageCacheSubscriber
mocked_dynamic_page_cache_subscriber in modules/permissions_by_entity/tests/modules/pbt_entity_test/pbt_entity_test.services.yml
Drupal\pbt_entity_test\EventSubscriber\MockedDynamicPageCacheSubscriber

File

modules/permissions_by_entity/tests/modules/pbt_entity_test/src/EventSubscriber/MockedDynamicPageCacheSubscriber.php, line 14

Namespace

Drupal\pbt_entity_test\EventSubscriber
View source
class MockedDynamicPageCacheSubscriber extends DynamicPageCacheSubscriber {

  /**
   * {@inheritdoc}
   */
  public function onRouteMatch(GetResponseEvent $event) {

    // Sets the response for the current route, if cached.
    $cached = $this->renderCache
      ->get($this->dynamicPageCacheRedirectRenderArray);
    if ($cached) {
      $response = $this
        ->renderArrayToResponse($cached);
      $response->headers
        ->set(self::HEADER, 'HIT');
      $event
        ->setResponse($response);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();

    // Embed the response object in a render array so that RenderCache is able
    // to cache it, handling cache redirection for us.
    $response_as_render_array = $this
      ->responseToRenderArray($response);
    $this->renderCache
      ->set($response_as_render_array, $this->dynamicPageCacheRedirectRenderArray);

    // The response was generated, mark the response as a cache miss. The next
    // time, it will be a cache hit.
    $response->headers
      ->set(self::HEADER, 'MISS');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DynamicPageCacheSubscriber::$dynamicPageCacheRedirectRenderArray protected property Dynamic Page Cache's redirect render array.
DynamicPageCacheSubscriber::$renderCache protected property The render cache.
DynamicPageCacheSubscriber::$rendererConfig protected property The renderer configuration array.
DynamicPageCacheSubscriber::$requestPolicy protected property A request policy rule determining the cacheability of a response.
DynamicPageCacheSubscriber::$requestPolicyResults protected property Internal cache of request policy results.
DynamicPageCacheSubscriber::$responsePolicy protected property A response policy rule determining the cacheability of the response.
DynamicPageCacheSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
DynamicPageCacheSubscriber::HEADER constant Name of Dynamic Page Cache's response header.
DynamicPageCacheSubscriber::onRequest public function Sets a response in case of a Dynamic Page Cache hit.
DynamicPageCacheSubscriber::renderArrayToResponse protected function Gets the embedded Response object in a render array.
DynamicPageCacheSubscriber::responseToRenderArray protected function Embeds a Response object in a render array so that RenderCache can cache it.
DynamicPageCacheSubscriber::shouldCacheResponse protected function Whether the given response should be cached by Dynamic Page Cache.
DynamicPageCacheSubscriber::__construct public function Constructs a new DynamicPageCacheSubscriber object.
MockedDynamicPageCacheSubscriber::onResponse public function Stores a response in case of a Dynamic Page Cache miss, if cacheable. Overrides DynamicPageCacheSubscriber::onResponse
MockedDynamicPageCacheSubscriber::onRouteMatch public function