You are here

class AliasWhitelist in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Path/AliasWhitelist.php \Drupal\Core\Path\AliasWhitelist

Extends CacheCollector to build the path alias whitelist over time.

Hierarchy

Expanded class hierarchy of AliasWhitelist

1 file declares its use of AliasWhitelist
AliasTest.php in core/modules/system/src/Tests/Path/AliasTest.php
Contains \Drupal\system\Tests\Path\AliasTest.
1 string reference to 'AliasWhitelist'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses AliasWhitelist
path.alias_whitelist in core/core.services.yml
Drupal\Core\Path\AliasWhitelist

File

core/lib/Drupal/Core/Path/AliasWhitelist.php, line 18
Contains \Drupal\Core\Path\AliasWhitelist.

Namespace

Drupal\Core\Path
View source
class AliasWhitelist extends CacheCollector implements AliasWhitelistInterface {

  /**
   * The Key/Value Store to use for state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The Path CRUD service.
   *
   * @var \Drupal\Core\Path\AliasStorageInterface
   */
  protected $aliasStorage;

  /**
   * Constructs an AliasWhitelist object.
   *
   * @param string $cid
   *   The cache id to use.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache backend.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state keyvalue store.
   * @param \Drupal\Core\Path\AliasStorageInterface $alias_storage
   *   The alias storage service.
   */
  public function __construct($cid, CacheBackendInterface $cache, LockBackendInterface $lock, StateInterface $state, AliasStorageInterface $alias_storage) {
    parent::__construct($cid, $cache, $lock);
    $this->state = $state;
    $this->aliasStorage = $alias_storage;
  }

  /**
   * {@inheritdoc}
   */
  protected function lazyLoadCache() {
    parent::lazyLoadCache();

    // On a cold start $this->storage will be empty and the whitelist will
    // need to be rebuilt from scratch. The whitelist is initialized from the
    // list of all valid path roots stored in the 'router.path_roots' state,
    // with values initialized to NULL. During the request, each path requested
    // that matches one of these keys will be looked up and the array value set
    // to either TRUE or FALSE. This ensures that paths which do not exist in
    // the router are not looked up, and that paths that do exist in the router
    // are only looked up once.
    if (empty($this->storage)) {
      $this
        ->loadMenuPathRoots();
    }
  }

  /**
   * Loads menu path roots to prepopulate cache.
   */
  protected function loadMenuPathRoots() {
    if ($roots = $this->state
      ->get('router.path_roots')) {
      foreach ($roots as $root) {
        $this->storage[$root] = NULL;
        $this
          ->persist($root);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function get($offset) {
    $this
      ->lazyLoadCache();

    // this may be called with paths that are not represented by menu router
    // items such as paths that will be rewritten by hook_url_outbound_alter().
    // Therefore internally TRUE is used to indicate whitelisted paths. FALSE is
    // used to indicate paths that have already been checked but are not
    // whitelisted, and NULL indicates paths that have not been checked yet.
    if (isset($this->storage[$offset])) {
      if ($this->storage[$offset]) {
        return TRUE;
      }
    }
    elseif (array_key_exists($offset, $this->storage)) {
      return $this
        ->resolveCacheMiss($offset);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function resolveCacheMiss($root) {
    $exists = $this->aliasStorage
      ->pathHasMatchingAlias('/' . $root);
    $this->storage[$root] = $exists;
    $this
      ->persist($root);
    if ($exists) {
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function clear() {
    parent::clear();
    $this
      ->loadMenuPathRoots();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasWhitelist::$aliasStorage protected property The Path CRUD service.
AliasWhitelist::$state protected property The Key/Value Store to use for state.
AliasWhitelist::clear public function Clears the collected cache entry. Overrides CacheCollector::clear
AliasWhitelist::get public function Gets value from the cache. Overrides CacheCollector::get
AliasWhitelist::lazyLoadCache protected function Loads the cache if not already done. Overrides CacheCollector::lazyLoadCache
AliasWhitelist::loadMenuPathRoots protected function Loads menu path roots to prepopulate cache.
AliasWhitelist::resolveCacheMiss public function Resolves a cache miss. Overrides CacheCollector::resolveCacheMiss
AliasWhitelist::__construct public function Constructs an AliasWhitelist object. Overrides CacheCollector::__construct
CacheCollector::$cache protected property The cache backend that should be used. 2
CacheCollector::$cacheCreated protected property Stores the cache creation time.
CacheCollector::$cacheInvalidated protected property Flag that indicates of the cache has been invalidated.
CacheCollector::$cacheLoaded protected property Indicates if the collected cache was already loaded.
CacheCollector::$cid protected property The cache id that is used for the cache entry.
CacheCollector::$keysToPersist protected property An array of keys to add to the cache on service termination.
CacheCollector::$keysToRemove protected property An array of keys to remove from the cache on service termination.
CacheCollector::$lock protected property The lock backend that should be used. 2
CacheCollector::$storage protected property Storage for the data itself.
CacheCollector::$tags protected property A list of tags that are used for the cache entry.
CacheCollector::delete public function Deletes the element. Overrides CacheCollectorInterface::delete
CacheCollector::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
CacheCollector::getCid protected function Gets the cache ID. 3
CacheCollector::has public function Returns whether data exists for this key. Overrides CacheCollectorInterface::has 1
CacheCollector::invalidateCache protected function Invalidate the cache.
CacheCollector::normalizeLockName protected function Normalizes a cache ID in order to comply with database limitations.
CacheCollector::persist protected function Flags an offset value to be written to the persistent cache.
CacheCollector::reset public function Resets the local cache. Overrides CacheCollectorInterface::reset 1
CacheCollector::set public function Implements \Drupal\Core\Cache\CacheCollectorInterface::set(). Overrides CacheCollectorInterface::set 1
CacheCollector::updateCache protected function Writes a value to the persistent cache immediately. 1