AliasWhitelist.php 4.44 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
<?php

/**
 * @file
 * Contains \Drupal\Core\Path\AliasWhitelist.
 */

namespace Drupal\Core\Path;

use Drupal\Core\Database\Connection;
use Drupal\Core\DestructableInterface;
use Drupal\Core\KeyValueStore\KeyValueFactory;
use Drupal\Core\Utility\CacheArray;

/**
 * Extends CacheArray to build the path alias whitelist over time.
 */
class AliasWhitelist extends CacheArray implements DestructableInterface {

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

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructs an AliasWhitelist object.
   *
   * @param string $cid
   *   The cache id to use.
   * @param string $bin
   *   The cache bin that should be used.
   * @param \Drupal\Core\KeyValueStore\KeyValueFactory $keyvalue
   *   The keyvalue factory to get the state cache from.
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection.
   */
  public function __construct($cid, $bin, KeyValueFactory $keyvalue, Connection $connection) {
    parent::__construct($cid, $bin);
    $this->state = $keyvalue->get('state');
    $this->connection = $connection;

    // 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 'menu_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('menu_path_roots')) {
      foreach ($roots as $root) {
        $this->storage[$root] = NULL;
        $this->persist($root);
      }
    }
  }

  /**
   * Overrides \ArrayAccess::offsetGet().
   */
  public function offsetGet($offset) {
    // url() 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);
    }
  }

  /**
   * Overrides \Drupal\Core\Utility\CacheArray::resolveCacheMiss().
   */
  public function resolveCacheMiss($root) {
    $query = $this->connection->select('url_alias', 'u');
    $query->addExpression(1);
    $exists = (bool) $query
      ->condition('u.source', $this->connection->escapeLike($root) . '%', 'LIKE')
      ->range(0, 1)
      ->execute()
      ->fetchField();
    $this->storage[$root] = $exists;
    $this->persist($root);
    if ($exists) {
      return TRUE;
    }
  }

  /**
   * Overrides \Drupal\Core\Utility\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);
      }
      cache($this->bin)->set($this->cid, $data);
      if ($lock) {
        lock()->release($lock_name);
      }
    }
  }

  /**
   * Overrides \Drupal\Core\Utility\CacheArray::clear().
   */
  public function clear() {
    parent::clear();
    $this->loadMenuPathRoots();
  }

  /**
   * Implements Drupal\Core\DestructableInterface::destruct().
   */
  public function destruct() {
    parent::__destruct();
  }

  /**
   * Overrides \Drupal\Core\Utility\CacheArray::clear().
   */
  public function __destruct() {
    // Do nothing to avoid segmentation faults. This can go away after the
    // cache collector from http://drupal.org/node/1786490 is used.
  }
}