You are here

class PageController in Render cache 7.2

PageController - Provides render caching for page objects.

Hierarchy

Expanded class hierarchy of PageController

File

modules/controller/render_cache_page/src/RenderCache/Controller/PageController.php, line 16
Contains \Drupal\render_cache_page\RenderCache\Controller\PageController

Namespace

Drupal\render_cache_page\RenderCache\Controller
View source
class PageController extends BaseController implements PageControllerInterface {

  /**
   * {@inheritdoc}
   */
  public function hook_init() {

    // We need to increase the recursion level before entering here to avoid
    // early rendering of #post_render_cache data.
    $this->renderStack
      ->increaseRecursion();
  }

  /**
   * {@inheritdoc}
   */
  public function view(array $objects) {

    // We need to decrease recursion again.
    // Because this only adds to the recursion storage, it is safe to call.
    foreach ($objects as $id => $page) {

      // Transform into a render array.
      if (!is_array($page->content)) {
        $page->content = array(
          'main' => array(
            '#markup' => $page->content,
          ),
        );
      }
      $storage = $this->renderStack
        ->decreaseRecursion();
      $page->content['x_render_cache_page_recursion_storage'] = $storage;
    }
    return parent::view($objects);
  }

  /**
   * {@inheritdoc}
   */
  protected function getDefaultCacheInfo($context) {
    $default_cache_info = parent::getDefaultCacheInfo($context);

    // The page cache is per page and per role by default.
    $default_cache_info['granularity'] = DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE;
    $default_cache_info['render_cache_cache_strategy'] = \RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER;
    $default_cache_info['render_cache_preserve_original'] = TRUE;
    return $default_cache_info;
  }

  /**
   * {@inheritdoc}
   */
  protected function isCacheable(array $default_cache_info, array $context) {

    // Disabled caching for now.
    return variable_get('render_cache_' . $this
      ->getPluginId() . '_' . $context['page_callback'] . '_enabled', FALSE) && parent::isCacheable($default_cache_info, $context);
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheKeys($object, array $context) {
    return array_merge(parent::getCacheKeys($object, $context), array(
      $context['page_callback'],
    ));
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheHash($object, array $context) {

    // Simple 1 hour cache to begin with.
    $hash['expiration'] = round(time() / 3600);
    return $hash;
  }

  /**
   * {@inheritdoc}
   */
  protected function getCacheTags($object, array $context) {
    $tags = parent::getCacheTags($object, $context);

    // @see drupal_pre_render_page() in Drupal 8.
    $tags[] = 'theme_global_settings';

    // Deliberately commented out as the theme might not be loaded, yet.
    // We do this in render() instead.

    //global $theme;

    //$tags[] = 'theme:' . $theme;
    return $tags;
  }

  /**
   * {@inheritdoc}
   */
  protected function render(array $objects) {
    foreach ($objects as $id => $page) {
      if ($this->renderStack
        ->supportsDynamicAssets()) {
        $storage = $page->content['x_render_cache_page_recursion_storage'];
        unset($page->content['x_render_cache_page_recursion_storage']);
        $this->renderStack
          ->addRecursionStorage($storage, TRUE);
      }
      $build[$id] = render_cache_page_drupal_render_page_helper($page->content);
    }

    // @see drupal_pre_render_page() in Drupal 8.
    global $theme;
    $page_id = current(array_keys($objects));
    $build[$page_id]['#cache']['tags'][] = 'theme:' . $theme;
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseController::$cache protected property The injected cache backend adapter.
BaseController::$context protected property An optional context provided by this controller.
BaseController::$renderStack protected property The injected render stack.
BaseController::alter protected function Overrides AbstractBaseController::alter
BaseController::determineCachingStrategy protected function Determines the caching strategy for a given cache info structure.
BaseController::getCacheContext protected function Overrides AbstractBaseController::getCacheContext 2
BaseController::getCacheIdInfo protected function Provides the fully pouplated cache information for a specific object. Overrides AbstractBaseController::getCacheIdInfo
BaseController::getCacheInfo protected function Specific cache info overrides based on the $object. Overrides AbstractBaseController::getCacheInfo 1
BaseController::getCacheInfoMap protected function Returns the cache information map for the given objects.
BaseController::getCacheValidate protected function Overrides AbstractBaseController::getCacheValidate
BaseController::getContext public function Overrides ControllerInterface::getContext
BaseController::getPlaceholders protected function Get the placeholders from the cache information map.
BaseController::renderPlaceholders public static function Overrides ControllerInterface::renderPlaceholders
BaseController::renderRecursive protected function Renders uncached objects in a recursion compatible way. Overrides AbstractBaseController::renderRecursive 1
BaseController::setContext public function Overrides ControllerInterface::setContext
BaseController::viewPlaceholders public function Overrides ControllerInterface::viewPlaceholders
BaseController::__construct public function Constructs a controller plugin object.
PageController::getCacheHash protected function Overrides BaseController::getCacheHash
PageController::getCacheKeys protected function Overrides BaseController::getCacheKeys
PageController::getCacheTags protected function Overrides BaseController::getCacheTags
PageController::getDefaultCacheInfo protected function Provides the cache info for all objects based on the context. Overrides BaseController::getDefaultCacheInfo
PageController::hook_init public function Implements delegated hook_init(). Overrides PageControllerInterface::hook_init
PageController::isCacheable protected function Overrides BaseController::isCacheable
PageController::render protected function Render uncached objects. Overrides AbstractBaseController::render
PageController::view public function Overrides BaseController::view