class AliasManagerCacheDecorator

Class used by the PathSubscriber to get the system path and cache path lookups.

Hierarchy

Expanded class hierarchy of AliasManagerCacheDecorator

1 file declares its use of AliasManagerCacheDecorator
PathSubscriber.php in drupal/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php
Definition of Drupal\Core\EventSubscriber\PathSubscriber.

File

drupal/core/lib/Drupal/Core/CacheDecorator/AliasManagerCacheDecorator.php, line 16
Contains Drupal\Core\CacheDecorator\AliasManagerCacheDecorator.

Namespace

Drupal\Core\CacheDecorator
View source
class AliasManagerCacheDecorator implements CacheDecoratorInterface, AliasManagerInterface {

  /**
   * @var \Drupal\Core\Path\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * @var \Drupal\Core\Cache\CacheBackendInterface;
   */
  protected $cache;

  /**
   * Stack of request paths for use as cids when caching system paths.
   *
   * @var array
   */
  protected $cacheKeys = array();

  /**
   * Holds an array of previously cached paths based on a request path.
   *
   * @var array
   */
  protected $preloadedPathLookups = array();

  /**
   * Whether the cache needs to be written.
   *
   * @var boolean
   */
  protected $cacheNeedsWriting = TRUE;

  /**
   * Constructs a \Drupal\Core\CacheDecorator\AliasManagerCacheDecorator.
   */
  public function __construct(AliasManagerInterface $alias_manager, CacheBackendInterface $cache) {
    $this->aliasManager = $alias_manager;
    $this->cache = $cache;
  }

  /**
   * Implements \Drupal\Core\CacheDecorator\CacheDecoratorInterface::setCacheKey().
   */
  public function setCacheKey($key) {
    $this->cacheKeys[] = $key;
  }

  /**
   * Implements \Drupal\Core\CacheDecorator\CacheDecoratorInterface::writeCache().
   *
   * Cache an array of the system paths available on each page. We assume
   * that aliases will be needed for the majority of these paths during
   * subsequent requests, and load them in a single query during path alias
   * lookup.
   */
  public function writeCache() {
    $path_lookups = $this
      ->getPathLookups();

    // Check if the system paths for this page were loaded from cache in this
    // request to avoid writing to cache on every request.
    if ($this->cacheNeedsWriting && !empty($path_lookups) && !empty($this->cacheKeys)) {

      // Use the system path of the current request for the cache ID (cid).
      $cid = end($this->cacheKeys);

      // Set the path cache to expire in 24 hours.
      $expire = REQUEST_TIME + 60 * 60 * 24;
      $this->cache
        ->set($cid, $path_lookups, $expire);
    }

    // We are at the end of the request, so pop off the last request path.
    array_pop($this->cacheKeys);
  }

  /**
   * Implements \Drupal\Core\Path\AliasManagerInterface::getSystemPath().
   */
  public function getSystemPath($path, $path_language = NULL) {
    $system_path = $this->aliasManager
      ->getSystemPath($path, $path_language);

    // We need to pass on the list of previously cached system paths for this
    // key to the alias manager for use in subsequent lookups.
    $cached = $this->cache
      ->get($system_path);
    $cached_paths = array();
    if ($cached) {
      $cached_paths = $cached->data;
      $this->cacheNeedsWriting = FALSE;
    }
    $this
      ->preloadPathLookups($cached_paths);
    return $system_path;
  }

  /**
   * Implements \Drupal\Core\Path\AliasManagerInterface::getPathAlias().
   */
  public function getPathAlias($path, $path_language = NULL) {
    return $this->aliasManager
      ->getPathAlias($path, $path_language);
  }

  /**
   * Implements \Drupal\Core\Path\AliasManagerInterface::getPathLookups().
   */
  public function getPathLookups() {
    return $this->aliasManager
      ->getPathLookups();
  }

  /**
   * Implements \Drupal\Core\Path\AliasManagerInterface::preloadPathLookups().
   */
  public function preloadPathLookups(array $path_list) {
    $this->aliasManager
      ->preloadPathLookups($path_list);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AliasManagerCacheDecorator::$aliasManager protected property
AliasManagerCacheDecorator::$cache protected property
AliasManagerCacheDecorator::$cacheKeys protected property Stack of request paths for use as cids when caching system paths.
AliasManagerCacheDecorator::$cacheNeedsWriting protected property Whether the cache needs to be written.
AliasManagerCacheDecorator::$preloadedPathLookups protected property Holds an array of previously cached paths based on a request path.
AliasManagerCacheDecorator::getPathAlias public function Implements \Drupal\Core\Path\AliasManagerInterface::getPathAlias(). Overrides AliasManagerInterface::getPathAlias
AliasManagerCacheDecorator::getPathLookups public function Implements \Drupal\Core\Path\AliasManagerInterface::getPathLookups(). Overrides AliasManagerInterface::getPathLookups
AliasManagerCacheDecorator::getSystemPath public function Implements \Drupal\Core\Path\AliasManagerInterface::getSystemPath(). Overrides AliasManagerInterface::getSystemPath
AliasManagerCacheDecorator::preloadPathLookups public function Implements \Drupal\Core\Path\AliasManagerInterface::preloadPathLookups(). Overrides AliasManagerInterface::preloadPathLookups
AliasManagerCacheDecorator::setCacheKey public function Implements \Drupal\Core\CacheDecorator\CacheDecoratorInterface::setCacheKey(). Overrides CacheDecoratorInterface::setCacheKey
AliasManagerCacheDecorator::writeCache public function Implements \Drupal\Core\CacheDecorator\CacheDecoratorInterface::writeCache(). Overrides CacheDecoratorInterface::writeCache
AliasManagerCacheDecorator::__construct public function Constructs a \Drupal\Core\CacheDecorator\AliasManagerCacheDecorator.