You are here

class RenderCache in Render cache 7.2

Static Service Container wrapper wrapping Drupal class.

Hierarchy

Expanded class hierarchy of RenderCache

5 files declare their use of RenderCache
BaseController.php in src/RenderCache/Controller/BaseController.php
Contains \Drupal\render_cache\RenderCache\Controller\BaseController
RenderCacheBackendAdapter.php in src/Cache/RenderCacheBackendAdapter.php
Contains \Drupal\render_cache\Cache\RenderCacheBackendAdapter
RenderCacheBackendAdapterTest.php in tests/src/Cache/RenderCacheBackendAdapterTest.php
Contains \Drupal\render_cache\Tests\Cache\RenderCacheBackendAdapterTest
RenderCachePlaceholder.php in src/Cache/RenderCachePlaceholder.php
Contains \Drupal\render_cache\Cache\RenderCachePlaceholder
RenderStack.php in src/Cache/RenderStack.php
Contains \Drupal\render_cache\Cache\RenderStack
2 string references to 'RenderCache'
RenderCacheServiceProvider::getContainerDefinition in src/ServiceContainer/ServiceProvider/RenderCacheServiceProvider.php
RenderCacheTest::getInfo in lib/Drupal/render_cache/Tests/RenderCacheTest.php

File

lib/RenderCache.php, line 10
Contains RenderCache

View source
class RenderCache extends Drupal {

  /**
   * Indicates that the item should not be rendered before it is cached.
   *
   * This is useful if the unrendered output is changed dynamically, before
   * it is cached.
   *
   * This is also the slowest option and should be regarded as a 'last-resort'
   * option.
   */
  const RENDER_CACHE_STRATEGY_NO_RENDER = 0;

  /**
   * Indicates that the item should be rendered before it is cached.
   *
   * If there is code that needs to change some properties,
   * the 'render_cache_preserve_properties' can be used.
   *
   * This is used for example to preserve page_footer / page_top properties
   * for #theme => html.
   */
  const RENDER_CACHE_STRATEGY_DIRECT_RENDER = 1;

  /**
   * Indicates that the item should not be rendered and it should be cached via
   * drupal_render() and drupal_render_cache_set().
   *
   * This is useful for e.g display suite when you want to change the markup
   * after entity_view(), but before calling drupal_render().
   *
   * This is most useful in combination with 'render_cache_preserve_properties',
   * because the entries retrieved from cache will only have #markup
   * and #attached keys by default.
   */
  const RENDER_CACHE_STRATEGY_LATE_RENDER = 2;

  // Drupal 8's constants differ from those in Drupal 7.
  // So we explicitly define them again as class constants.
  // Note: Drupal 8 has CACHE_PERMANENT == -1.

  /**
   * Indicates that the item should never be removed unless explicitly selected.
   *
   * The item may be removed using cache_clear_all() with a cache ID.
   */
  const CACHE_PERMANENT = 0;

  /**
   * Indicates that the item should be removed at the next general cache wipe.
   */
  const CACHE_TEMPORARY = -1;

  /**
   * The currently active container object.
   *
   * @var \Drupal\render_cache\DependencyInjection\ContainerInterface
   */
  protected static $container;

  /**
   * The currently active render stack - for improved performance.
   *
   * @var \Drupal\render_cache\Cache\RenderStack
   */
  protected static $renderStack;

  /**
   * Container Ready event listener.
   *
   * @param $container
   *   The service container.
   */
  public static function containerReady($container) {
    static::$container = $container;

    // Last get the render stack for improved performance.
    if (static::$container) {
      static::$renderStack = static::$container
        ->get('render_stack');

      // Check if we support dynamic asset loading via drupal_add_js/css.
      if (variable_get('render_cache_supports_dynamic_assets', FALSE)) {
        static::$renderStack
          ->supportsDynamicAssets(TRUE);
      }
    }
  }

  /**
   * Returns a render cache controller plugin.
   *
   * @param string $type
   *   The type of the controller plugin, e.g. block, entity, ...
   *
   * @return \Drupal\render_cache\RenderCache\Controller\ControllerInterface|NULL
   *   The instantiated controller with the given type or NULL.
   */
  public static function getController($type) {
    return static::$container
      ->get('render_cache.controller')
      ->createInstance($type);
  }

  /**
   * Returns a render cache render strategy plugin.
   *
   * @param string $type
   *   The type of the render strategy plugin, e.g. big_pipe, esi_validate, ...
   *
   * @return \Drupal\render_cache\RenderCache\RenderStrategy\RenderStrategyInterface|NULL
   *   The instantiated render strategy with the given type or NULL.
   */
  public static function getRenderStrategy($type) {
    return static::$container
      ->get('render_cache.render_strategy')
      ->createInstance($type);
  }

  /**
   * Returns a render cache validation strategy plugin.
   *
   * @param string $type
   *   The type of the validation strategy plugin, e.g. cache tags, ttl, ...
   *
   * @return \Drupal\render_cache\RenderCache\ValidationStrategy\ValidationStrategyInterface|NULL
   *   The instantiated validation strategy with the given type or NULL.
   */
  public static function getValidationStrategy($type) {
    return static::$container
      ->get('render_cache.validation_strategy')
      ->createInstance($type);
  }

  /**
   * Overrides drupal_render().
   *
   * If we really need to render early, at least collect the cache tags, etc.
   *
   * @param array $render
   *
   * @return string
   */
  public static function drupalRender(&$render) {
    return static::$renderStack
      ->drupalRender($render);
  }

  /**
   * Returns if we are within a recursive rendering context.
   *
   * This is useful to determine if its safe to output a placeholder, so that
   * #post_render_cache will work.
   *
   * @return bool
   *   TRUE if we are within a recursive context, FALSE otherwise.
   */
  public static function isRecursive() {
    return static::$renderStack
      ->isRecursive();
  }

  /**
   * Overrides drupal_add_js().
   *
   * @see drupal_add_js()
   */
  public static function drupal_add_js($data = NULL, $options = NULL) {
    if (!static::$renderStack) {
      return static::callOriginalFunction('drupal_add_js', func_get_args());
    }
    return static::$renderStack
      ->drupal_add_assets('js', $data, $options);
  }

  /**
   * Overrides drupal_add_css().
   *
   * @see drupal_add_css()
   */
  public static function drupal_add_css($data = NULL, $options = NULL) {
    if (!static::$renderStack) {
      return static::callOriginalFunction('drupal_add_css', func_get_args());
    }
    return static::$renderStack
      ->drupal_add_assets('css', $data, $options);
  }

  /**
   * Overrides drupal_add_library().
   *
   * @see drupal_add_library()
   */
  public static function drupal_add_library($module, $name, $every_page = NULL) {
    if (!static::$renderStack) {
      return static::callOriginalFunction('drupal_add_library', func_get_args());
    }
    return static::$renderStack
      ->drupal_add_library($module, $name, $every_page);
  }

  /**
   * Overrides drupal_process_attached().
   *
   * @see drupal_process_attached()
   */
  public static function drupal_process_attached($elements, $group = JS_DEFAULT, $dependency_check = FALSE, $every_page = NULL) {
    if (!static::$renderStack) {
      return static::callOriginalFunction('drupal_process_attached', func_get_args());
    }
    return static::$renderStack
      ->drupal_process_attached($elements, $group, $dependency_check, $every_page);
  }

  /**
   * Calls the original function in case the container is not yet booted.
   *
   * @param string $function
   *   The function name.
   * @param array $args
   *   The function args.
   * @return NULL|mixed
   *   Returns what the original function returns.
   */
  public static function callOriginalFunction($function, $args) {
    global $conf;
    $name = $function . "_function";
    $old = $conf[$name];
    unset($conf[$name]);
    $return = call_user_func_array($function, $args);
    $conf[$name] = $old;
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RenderCache::$container protected static property The currently active container object.
RenderCache::$renderStack protected static property The currently active render stack - for improved performance.
RenderCache::CACHE_PERMANENT constant Indicates that the item should never be removed unless explicitly selected.
RenderCache::CACHE_TEMPORARY constant Indicates that the item should be removed at the next general cache wipe.
RenderCache::callOriginalFunction public static function Calls the original function in case the container is not yet booted.
RenderCache::containerReady public static function Container Ready event listener.
RenderCache::drupalRender public static function Overrides drupal_render().
RenderCache::drupal_add_css public static function Overrides drupal_add_css().
RenderCache::drupal_add_js public static function Overrides drupal_add_js().
RenderCache::drupal_add_library public static function Overrides drupal_add_library().
RenderCache::drupal_process_attached public static function Overrides drupal_process_attached().
RenderCache::getController public static function Returns a render cache controller plugin.
RenderCache::getRenderStrategy public static function Returns a render cache render strategy plugin.
RenderCache::getValidationStrategy public static function Returns a render cache validation strategy plugin.
RenderCache::isRecursive public static function Returns if we are within a recursive rendering context.
RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER constant Indicates that the item should be rendered before it is cached.
RenderCache::RENDER_CACHE_STRATEGY_LATE_RENDER constant Indicates that the item should not be rendered and it should be cached via drupal_render() and drupal_render_cache_set().
RenderCache::RENDER_CACHE_STRATEGY_NO_RENDER constant Indicates that the item should not be rendered before it is cached.