class LocaleLookup

Extends CacheArray to allow for dynamic building of the locale cache.

Hierarchy

Expanded class hierarchy of LocaleLookup

1 file declares its use of LocaleLookup
LocaleTranslation.php in drupal/core/modules/locale/lib/Drupal/locale/LocaleTranslation.php
Contains \Drupal\Core\Language\LocaleTranslation.

File

drupal/core/modules/locale/lib/Drupal/locale/LocaleLookup.php, line 18
Contains \Drupal\locale\Locale\Lookup.

Namespace

Drupal\locale
View source
class LocaleLookup extends CacheArray implements DestructableInterface {

  /**
   * A language code.
   *
   * @var string
   */
  protected $langcode;

  /**
   * The msgctxt context.
   *
   * @var string
   */
  protected $context;

  /**
   * The locale storage.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected $stringStorage;

  /**
   * Constructs a LocaleCache object.
   */
  public function __construct($langcode, $context, $string_storage) {
    $this->langcode = $langcode;
    $this->context = (string) $context;
    $this->stringStorage = $string_storage;

    // Add the current user's role IDs to the cache key, this ensures that, for
    // example, strings for admin menu items and settings forms are not cached
    // for anonymous users.
    $rids = isset($GLOBALS['user']) ? implode(':', array_keys($GLOBALS['user']->roles)) : '0';
    parent::__construct("locale:{$langcode}:{$context}:{$rids}", 'cache', array(
      'locale' => TRUE,
    ));
  }

  /**
   * {@inheritdoc}
   */
  protected function resolveCacheMiss($offset) {
    $translation = $this->stringStorage
      ->findTranslation(array(
      'language' => $this->langcode,
      'source' => $offset,
      'context' => $this->context,
    ));
    if ($translation) {
      $value = !empty($translation->translation) ? $translation->translation : TRUE;
    }
    else {

      // We don't have the source string, update the {locales_source} table to
      // indicate the string is not translated.
      $this->stringStorage
        ->createString(array(
        'source' => $offset,
        'context' => $this->context,
        'version' => VERSION,
      ))
        ->addLocation('path', request_uri())
        ->save();
      $value = TRUE;
    }
    $this->storage[$offset] = $value;

    // Disabling the usage of string caching allows a module to watch for
    // the exact list of strings used on a page. From a performance
    // perspective that is a really bad idea, so we have no user
    // interface for this. Be careful when turning this option off!
    if (config('locale.settings')
      ->get('cache_strings')) {
      $this
        ->persist($offset);
    }
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function destruct() {
    parent::__destruct();
  }

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

    // Do nothing to avoid segmentation faults. This can be restored after the
    // cache collector from http://drupal.org/node/1786490 is used.
  }

}

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::clear public function Clear the cache. 1
CacheArray::offsetExists public function Implements ArrayAccess::offsetExists(). 1
CacheArray::offsetGet public function Implements ArrayAccess::offsetGet(). 2
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::set protected function Writes a value to the persistent cache immediately. 2
LocaleLookup::$context protected property The msgctxt context.
LocaleLookup::$langcode protected property A language code.
LocaleLookup::$stringStorage protected property The locale storage.
LocaleLookup::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
LocaleLookup::resolveCacheMiss protected function Resolves a cache miss. Overrides CacheArray::resolveCacheMiss
LocaleLookup::__construct public function Constructs a LocaleCache object. Overrides CacheArray::__construct
LocaleLookup::__destruct public function Destructs the CacheArray object. Overrides CacheArray::__destruct