AliasManagerCacheDecorator.php 3.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?php

/**
 * @file
 * Contains Drupal\Core\CacheDecorator\AliasManagerCacheDecorator.
 */

namespace Drupal\Core\CacheDecorator;

use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Path\AliasManagerInterface;

/**
 * Class used by the PathSubscriber to get the system path and cache path lookups.
 */
class AliasManagerCacheDecorator implements CacheDecoratorInterface, AliasManagerInterface {

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

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

  /**
29
   * The cache key to use when caching system paths.
30
   *
31
   * @var string
32
   */
33
  protected $cacheKey;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

  /**
   * 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;
  }

  /**
58
   * {@inheritdoc}
59 60
   */
  public function setCacheKey($key) {
61
    $this->cacheKey = $key;
62 63 64
  }

  /**
65
   * {@inheritdoc}
66 67 68 69 70 71 72 73 74 75
   *
   * 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.
76
    if ($this->cacheNeedsWriting && !empty($path_lookups) && !empty($this->cacheKey)) {
77 78
      // Set the path cache to expire in 24 hours.
      $expire = REQUEST_TIME + (60 * 60 * 24);
79
      $this->cache->set($this->cacheKey, $path_lookups, $expire);
80 81 82 83
    }
  }

  /**
84
   * {@inheritdoc}
85
   */
86 87
  public function getPathByAlias($alias, $langcode = NULL) {
    $path = $this->aliasManager->getPathByAlias($alias, $langcode);
88 89
    // We need to pass on the list of previously cached system paths for this
    // key to the alias manager for use in subsequent lookups.
90
    $cached = $this->cache->get($path);
91 92 93 94 95 96
    $cached_paths = array();
    if ($cached) {
      $cached_paths = $cached->data;
      $this->cacheNeedsWriting = FALSE;
    }
    $this->preloadPathLookups($cached_paths);
97
    return $path;
98 99 100
  }

  /**
101
   * {@inheritdoc}
102
   */
103 104
  public function getAliasByPath($path, $langcode = NULL) {
    return $this->aliasManager->getAliasByPath($path, $langcode);
105 106 107
  }

  /**
108
   * {@inheritdoc}
109 110 111 112 113 114
   */
  public function getPathLookups() {
    return $this->aliasManager->getPathLookups();
  }

  /**
115
   * {@inheritdoc}
116 117 118 119
   */
  public function preloadPathLookups(array $path_list) {
    $this->aliasManager->preloadPathLookups($path_list);
  }
120 121 122 123 124 125 126 127

  /**
   * {@inheritdoc}
   */
  public function cacheClear($source = NULL) {
    $this->cache->delete($this->cacheKey);
    $this->aliasManager->cacheClear($source);
  }
128
}