annotate core/modules/views/src/ViewsData.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\NestedArray;
Chris@0 6 use Drupal\Core\Cache\Cache;
Chris@0 7 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 8 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 9 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 10 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Class to manage and lazy load cached views data.
Chris@0 14 *
Chris@0 15 * If a table is requested and cannot be loaded from cache, all data is then
Chris@0 16 * requested from cache. A table-specific cache entry will then be created for
Chris@0 17 * the requested table based on this cached data. Table data is only rebuilt
Chris@0 18 * when no cache entry for all table data can be retrieved.
Chris@0 19 */
Chris@0 20 class ViewsData {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The base cache ID to use.
Chris@0 24 *
Chris@0 25 * @var string
Chris@0 26 */
Chris@0 27 protected $baseCid = 'views_data';
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The cache backend to use.
Chris@0 31 *
Chris@0 32 * @var \Drupal\Core\Cache\CacheBackendInterface
Chris@0 33 */
Chris@0 34 protected $cacheBackend;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Table data storage.
Chris@0 38 *
Chris@0 39 * This is used for explicitly requested tables.
Chris@0 40 *
Chris@0 41 * @var array
Chris@0 42 */
Chris@0 43 protected $storage = [];
Chris@0 44
Chris@0 45 /**
Chris@0 46 * All table storage data loaded from cache.
Chris@0 47 *
Chris@0 48 * This is used when all data has been loaded from the cache to prevent
Chris@0 49 * further cache get calls when rebuilding all data or for single tables.
Chris@0 50 *
Chris@0 51 * @var array
Chris@0 52 */
Chris@0 53 protected $allStorage = [];
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Whether the data has been fully loaded in this request.
Chris@0 57 *
Chris@0 58 * @var bool
Chris@0 59 */
Chris@0 60 protected $fullyLoaded = FALSE;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Whether or not to skip data caching and rebuild data each time.
Chris@0 64 *
Chris@0 65 * @var bool
Chris@0 66 */
Chris@0 67 protected $skipCache = FALSE;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * The current language code.
Chris@0 71 *
Chris@0 72 * @var string
Chris@0 73 */
Chris@0 74 protected $langcode;
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Stores a module manager to invoke hooks.
Chris@0 78 *
Chris@0 79 * @var \Drupal\Core\Extension\ModuleHandlerInterface
Chris@0 80 */
Chris@0 81 protected $moduleHandler;
Chris@0 82
Chris@0 83 /**
Chris@0 84 * The language manager.
Chris@0 85 *
Chris@0 86 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@0 87 */
Chris@0 88 protected $languageManager;
Chris@0 89
Chris@0 90 /**
Chris@0 91 * Constructs this ViewsData object.
Chris@0 92 *
Chris@0 93 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
Chris@0 94 * The cache backend to use.
Chris@0 95 * @param \Drupal\Core\Config\ConfigFactoryInterface $config
Chris@0 96 * The configuration factory object to use.
Chris@0 97 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 98 * The module handler class to use for invoking hooks.
Chris@0 99 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 100 * The language manager.
Chris@0 101 */
Chris@0 102 public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryInterface $config, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
Chris@0 103 $this->cacheBackend = $cache_backend;
Chris@0 104 $this->moduleHandler = $module_handler;
Chris@0 105 $this->languageManager = $language_manager;
Chris@0 106
Chris@0 107 $this->langcode = $this->languageManager->getCurrentLanguage()->getId();
Chris@0 108 $this->skipCache = $config->get('views.settings')->get('skip_cache');
Chris@0 109 }
Chris@0 110
Chris@0 111 /**
Chris@0 112 * Gets all table data.
Chris@0 113 *
Chris@0 114 * @see https://www.drupal.org/node/2723553
Chris@0 115 *
Chris@0 116 * @return array
Chris@0 117 * An array of table data.
Chris@0 118 */
Chris@0 119 public function getAll() {
Chris@0 120 if (!$this->fullyLoaded) {
Chris@0 121 $this->allStorage = $this->getData();
Chris@0 122 }
Chris@0 123
Chris@0 124 // Set storage from allStorage outside of the fullyLoaded check to prevent
Chris@0 125 // cache calls on requests that have requested all data to get a single
Chris@0 126 // tables data. Make sure $this->storage is populated in this case.
Chris@0 127 $this->storage = $this->allStorage;
Chris@0 128 return $this->allStorage;
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * Gets data for a particular table, or all tables.
Chris@0 133 *
Chris@0 134 * @param string|null $key
Chris@0 135 * The key of the cache entry to retrieve. Defaults to NULL, this will
Chris@0 136 * return all table data.
Chris@0 137 *
Chris@0 138 * @deprecated NULL $key deprecated in Drupal 8.2.x and will be removed in
Chris@0 139 * 9.0.0. Use getAll() instead.
Chris@0 140 *
Chris@0 141 * @see https://www.drupal.org/node/2723553
Chris@0 142 *
Chris@0 143 * @return array
Chris@0 144 * An array of table data.
Chris@0 145 */
Chris@0 146 public function get($key = NULL) {
Chris@0 147 if (!$key) {
Chris@0 148 return $this->getAll();
Chris@0 149 }
Chris@0 150 if (!isset($this->storage[$key])) {
Chris@0 151 // Prepare a cache ID for get and set.
Chris@0 152 $cid = $this->baseCid . ':' . $key;
Chris@0 153 $from_cache = FALSE;
Chris@0 154
Chris@0 155 if ($data = $this->cacheGet($cid)) {
Chris@0 156 $this->storage[$key] = $data->data;
Chris@0 157 $from_cache = TRUE;
Chris@0 158 }
Chris@0 159 // If there is no cached entry and data is not already fully loaded,
Chris@0 160 // rebuild. This will stop requests for invalid tables calling getData.
Chris@0 161 elseif (!$this->fullyLoaded) {
Chris@0 162 $this->allStorage = $this->getData();
Chris@0 163 }
Chris@0 164
Chris@0 165 if (!$from_cache) {
Chris@0 166 if (!isset($this->allStorage[$key])) {
Chris@0 167 // Write an empty cache entry if no information for that table
Chris@0 168 // exists to avoid repeated cache get calls for this table and
Chris@0 169 // prevent loading all tables unnecessarily.
Chris@0 170 $this->storage[$key] = [];
Chris@0 171 $this->allStorage[$key] = [];
Chris@0 172 }
Chris@0 173 else {
Chris@0 174 $this->storage[$key] = $this->allStorage[$key];
Chris@0 175 }
Chris@0 176
Chris@0 177 // Create a cache entry for the requested table.
Chris@0 178 $this->cacheSet($cid, $this->allStorage[$key]);
Chris@0 179 }
Chris@0 180 }
Chris@0 181 return $this->storage[$key];
Chris@0 182 }
Chris@0 183
Chris@0 184 /**
Chris@0 185 * Gets data from the cache backend.
Chris@0 186 *
Chris@0 187 * @param string $cid
Chris@0 188 * The cache ID to return.
Chris@0 189 *
Chris@0 190 * @return mixed
Chris@0 191 * The cached data, if any. This will immediately return FALSE if the
Chris@0 192 * $skipCache property is TRUE.
Chris@0 193 */
Chris@0 194 protected function cacheGet($cid) {
Chris@0 195 if ($this->skipCache) {
Chris@0 196 return FALSE;
Chris@0 197 }
Chris@0 198
Chris@0 199 return $this->cacheBackend->get($this->prepareCid($cid));
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Sets data to the cache backend.
Chris@0 204 *
Chris@0 205 * @param string $cid
Chris@0 206 * The cache ID to set.
Chris@0 207 * @param mixed $data
Chris@0 208 * The data that will be cached.
Chris@0 209 */
Chris@0 210 protected function cacheSet($cid, $data) {
Chris@0 211 return $this->cacheBackend->set($this->prepareCid($cid), $data, Cache::PERMANENT, ['views_data', 'config:core.extension']);
Chris@0 212 }
Chris@0 213
Chris@0 214 /**
Chris@0 215 * Prepares the cache ID by appending a language code.
Chris@0 216 *
Chris@0 217 * @param string $cid
Chris@0 218 * The cache ID to prepare.
Chris@0 219 *
Chris@0 220 * @return string
Chris@0 221 * The prepared cache ID.
Chris@0 222 */
Chris@0 223 protected function prepareCid($cid) {
Chris@0 224 return $cid . ':' . $this->langcode;
Chris@0 225 }
Chris@0 226
Chris@0 227 /**
Chris@0 228 * Gets all data invoked by hook_views_data().
Chris@0 229 *
Chris@0 230 * This is requested from the cache before being rebuilt.
Chris@0 231 *
Chris@0 232 * @return array
Chris@0 233 * An array of all data.
Chris@0 234 */
Chris@0 235 protected function getData() {
Chris@0 236 $this->fullyLoaded = TRUE;
Chris@0 237
Chris@0 238 if ($data = $this->cacheGet($this->baseCid)) {
Chris@0 239 return $data->data;
Chris@0 240 }
Chris@0 241 else {
Chris@0 242 $modules = $this->moduleHandler->getImplementations('views_data');
Chris@0 243 $data = [];
Chris@0 244 foreach ($modules as $module) {
Chris@0 245 $views_data = $this->moduleHandler->invoke($module, 'views_data');
Chris@0 246 // Set the provider key for each base table.
Chris@0 247 foreach ($views_data as &$table) {
Chris@0 248 if (isset($table['table']) && !isset($table['table']['provider'])) {
Chris@0 249 $table['table']['provider'] = $module;
Chris@0 250 }
Chris@0 251 }
Chris@0 252 $data = NestedArray::mergeDeep($data, $views_data);
Chris@0 253 }
Chris@0 254 $this->moduleHandler->alter('views_data', $data);
Chris@0 255
Chris@0 256 $this->processEntityTypes($data);
Chris@0 257
Chris@0 258 // Keep a record with all data.
Chris@0 259 $this->cacheSet($this->baseCid, $data);
Chris@0 260
Chris@0 261 return $data;
Chris@0 262 }
Chris@0 263 }
Chris@0 264
Chris@0 265 /**
Chris@0 266 * Links tables with 'entity type' to respective generic entity-type tables.
Chris@0 267 *
Chris@0 268 * @param array $data
Chris@0 269 * The array of data to alter entity data for, passed by reference.
Chris@0 270 */
Chris@0 271 protected function processEntityTypes(array &$data) {
Chris@0 272 foreach ($data as $table_name => $table_info) {
Chris@0 273 // Add in a join from the entity-table if an entity-type is given.
Chris@0 274 if (!empty($table_info['table']['entity type'])) {
Chris@0 275 $entity_table = 'views_entity_' . $table_info['table']['entity type'];
Chris@0 276
Chris@0 277 $data[$entity_table]['table']['join'][$table_name] = [
Chris@0 278 'left_table' => $table_name,
Chris@0 279 ];
Chris@0 280 $data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
Chris@0 281 // Copy over the default table group if we have none yet.
Chris@0 282 if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
Chris@0 283 $data[$entity_table]['table']['group'] = $table_info['table']['group'];
Chris@0 284 }
Chris@0 285 }
Chris@0 286 }
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Fetches a list of all base tables available.
Chris@0 291 *
Chris@0 292 * @return array
Chris@0 293 * An array of base table data keyed by table name. Each item contains the
Chris@0 294 * following keys:
Chris@0 295 * - title: The title label for the base table.
Chris@0 296 * - help: The help text for the base table.
Chris@0 297 * - weight: The weight of the base table.
Chris@0 298 */
Chris@0 299 public function fetchBaseTables() {
Chris@0 300 $tables = [];
Chris@0 301
Chris@0 302 foreach ($this->get() as $table => $info) {
Chris@0 303 if (!empty($info['table']['base'])) {
Chris@0 304 $tables[$table] = [
Chris@0 305 'title' => $info['table']['base']['title'],
Chris@0 306 'help' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
Chris@0 307 'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
Chris@0 308 ];
Chris@0 309 }
Chris@0 310 }
Chris@0 311
Chris@0 312 // Sorts by the 'weight' and then by 'title' element.
Chris@0 313 uasort($tables, function ($a, $b) {
Chris@0 314 if ($a['weight'] != $b['weight']) {
Chris@0 315 return $a['weight'] < $b['weight'] ? -1 : 1;
Chris@0 316 }
Chris@0 317 if ($a['title'] != $b['title']) {
Chris@0 318 return $a['title'] < $b['title'] ? -1 : 1;
Chris@0 319 }
Chris@0 320 return 0;
Chris@0 321 });
Chris@0 322
Chris@0 323 return $tables;
Chris@0 324 }
Chris@0 325
Chris@0 326 /**
Chris@0 327 * Clears the class storage and cache.
Chris@0 328 */
Chris@0 329 public function clear() {
Chris@0 330 $this->storage = [];
Chris@0 331 $this->allStorage = [];
Chris@0 332 $this->fullyLoaded = FALSE;
Chris@0 333 Cache::invalidateTags(['views_data']);
Chris@0 334 }
Chris@0 335
Chris@0 336 }