class ThemeRegistry

Builds the run-time theme registry.

Extends CacheArray to allow the theme registry to be accessed as a complete registry, while internally caching only the parts of the registry that are actually in use on the site. On cache misses the complete theme registry is loaded and used to update the run-time cache.

Hierarchy

Expanded class hierarchy of ThemeRegistry

2 files declare their use of ThemeRegistry
RegistryTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Theme/RegistryTest.php
Definition of Drupal\system\Tests\Theme\RegistryTest.
theme.inc in drupal/core/includes/theme.inc
The theme system, which controls the output of Drupal.
1 string reference to 'ThemeRegistry'
RegistryTest::getInfo in drupal/core/modules/system/lib/Drupal/system/Tests/Theme/RegistryTest.php

File

drupal/core/lib/Drupal/Core/Utility/ThemeRegistry.php, line 20
Definition of Drupal\Core\Utility\ThemeRegistry

Namespace

Drupal\Core\Utility
View source
class ThemeRegistry extends CacheArray {

  /**
   * Whether the partial registry can be persisted to the cache.
   *
   * This is only allowed if all modules and the request method is GET. theme()
   * should be very rarely called on POST requests and this avoids polluting
   * the runtime cache.
   */
  protected $persistable;

  /**
   * The complete theme registry array.
   */
  protected $completeRegistry;

  /**
   * Constructs a ThemeRegistry object.
   *
   * @param string $cid
   *   The cid for the array being cached.
   * @param string $bin
   *   The bin to cache the array.
   * @param array $tags
   *   (optional) The tags to specify for the cache item.
   */
  function __construct($cid, $bin, $tags) {
    $this->cid = $cid;
    $this->bin = $bin;
    $this->tags = $tags;
    $this->persistable = module_load_all(NULL) && $_SERVER['REQUEST_METHOD'] == 'GET';
    $data = array();
    if ($this->persistable && ($cached = cache($this->bin)
      ->get($this->cid))) {
      $data = $cached->data;
    }
    else {

      // If there is no runtime cache stored, fetch the full theme registry,
      // but then initialize each value to NULL. This allows offsetExists()
      // to function correctly on non-registered theme hooks without triggering
      // a call to resolveCacheMiss().
      $data = $this
        ->initializeRegistry();
      if ($this->persistable) {
        $this
          ->set($data);
      }
    }
    $this->storage = $data;
  }

  /**
   * Initializes the full theme registry.
   *
   * @return
   *   An array with the keys of the full theme registry, but the values
   *   initialized to NULL.
   */
  function initializeRegistry() {
    $this->completeRegistry = theme_get_registry();
    return array_fill_keys(array_keys($this->completeRegistry), NULL);
  }

  /**
   * Overrides CacheArray::offsetExists().
   */
  public function offsetExists($offset) {

    // Since the theme registry allows for theme hooks to be requested that
    // are not registered, just check the existence of the key in the registry.
    // Use array_key_exists() here since a NULL value indicates that the theme
    // hook exists but has not yet been requested.
    return array_key_exists($offset, $this->storage);
  }

  /**
   * Overrides CacheArray::offsetGet().
   */
  public function offsetGet($offset) {

    // If the offset is set but empty, it is a registered theme hook that has
    // not yet been requested. Offsets that do not exist at all were not
    // registered in hook_theme().
    if (isset($this->storage[$offset])) {
      return $this->storage[$offset];
    }
    elseif (array_key_exists($offset, $this->storage)) {
      return $this
        ->resolveCacheMiss($offset);
    }
  }

  /**
   * Implements CacheArray::resolveCacheMiss().
   */
  public function resolveCacheMiss($offset) {
    if (!isset($this->completeRegistry)) {
      $this->completeRegistry = theme_get_registry();
    }
    $this->storage[$offset] = $this->completeRegistry[$offset];
    if ($this->persistable) {
      $this
        ->persist($offset);
    }
    return $this->storage[$offset];
  }

  /**
   * Overrides CacheArray::set().
   */
  public function set($data, $lock = TRUE) {
    $lock_name = $this->cid . ':' . $this->bin;
    if (!$lock || lock()
      ->acquire($lock_name)) {
      if ($cached = cache($this->bin)
        ->get($this->cid)) {

        // Use array merge instead of union so that filled in values in $data
        // overwrite empty values in the current cache.
        $data = array_merge($cached->data, $data);
      }
      else {
        $registry = $this
          ->initializeRegistry();
        $data = array_merge($registry, $data);
      }
      cache($this->bin)
        ->set($this->cid, $data, CacheBackendInterface::CACHE_PERMANENT, $this->tags);
      if ($lock) {
        lock()
          ->release($lock_name);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheArray::$bin protected property A bin to pass to cache()->set() and cache()->get().
CacheArray::$cid protected property A cid to pass to cache()->set() and cache()->get().
CacheArray::$keysToPersist protected property An array of keys to add to the cache at the end of the request.
CacheArray::$storage protected property Storage for the data itself.
CacheArray::$tags protected property A tags array to pass to cache()->set().
CacheArray::offsetSet public function Implements ArrayAccess::offsetSet().
CacheArray::offsetUnset public function Implements ArrayAccess::offsetUnset().
CacheArray::persist protected function Flags an offset value to be written to the persistent cache.
CacheArray::__destruct public function Destructs the CacheArray object.
ThemeRegistry::$completeRegistry protected property The complete theme registry array.
ThemeRegistry::$persistable protected property Whether the partial registry can be persisted to the cache.
ThemeRegistry::initializeRegistry function Initializes the full theme registry.
ThemeRegistry::offsetExists public function Overrides CacheArray::offsetExists(). Overrides CacheArray::offsetExists
ThemeRegistry::offsetGet public function Overrides CacheArray::offsetGet(). Overrides CacheArray::offsetGet
ThemeRegistry::resolveCacheMiss public function Implements CacheArray::resolveCacheMiss(). Overrides CacheArray::resolveCacheMiss
ThemeRegistry::set public function Overrides CacheArray::set(). Overrides CacheArray::set
ThemeRegistry::__construct function Constructs a ThemeRegistry object. Overrides CacheArray::__construct