annotate core/modules/views/src/ViewsData.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
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@17 136 * return all table data. NULL $key deprecated in Drupal 8.2.x and will be
Chris@17 137 * removed in 9.0.0. Use getAll() instead.
Chris@0 138 *
Chris@0 139 * @see https://www.drupal.org/node/2723553
Chris@0 140 *
Chris@0 141 * @return array
Chris@0 142 * An array of table data.
Chris@0 143 */
Chris@0 144 public function get($key = NULL) {
Chris@0 145 if (!$key) {
Chris@0 146 return $this->getAll();
Chris@0 147 }
Chris@0 148 if (!isset($this->storage[$key])) {
Chris@0 149 // Prepare a cache ID for get and set.
Chris@0 150 $cid = $this->baseCid . ':' . $key;
Chris@0 151 $from_cache = FALSE;
Chris@0 152
Chris@0 153 if ($data = $this->cacheGet($cid)) {
Chris@0 154 $this->storage[$key] = $data->data;
Chris@0 155 $from_cache = TRUE;
Chris@0 156 }
Chris@0 157 // If there is no cached entry and data is not already fully loaded,
Chris@0 158 // rebuild. This will stop requests for invalid tables calling getData.
Chris@0 159 elseif (!$this->fullyLoaded) {
Chris@0 160 $this->allStorage = $this->getData();
Chris@0 161 }
Chris@0 162
Chris@0 163 if (!$from_cache) {
Chris@0 164 if (!isset($this->allStorage[$key])) {
Chris@0 165 // Write an empty cache entry if no information for that table
Chris@0 166 // exists to avoid repeated cache get calls for this table and
Chris@0 167 // prevent loading all tables unnecessarily.
Chris@0 168 $this->storage[$key] = [];
Chris@0 169 $this->allStorage[$key] = [];
Chris@0 170 }
Chris@0 171 else {
Chris@0 172 $this->storage[$key] = $this->allStorage[$key];
Chris@0 173 }
Chris@0 174
Chris@0 175 // Create a cache entry for the requested table.
Chris@0 176 $this->cacheSet($cid, $this->allStorage[$key]);
Chris@0 177 }
Chris@0 178 }
Chris@0 179 return $this->storage[$key];
Chris@0 180 }
Chris@0 181
Chris@0 182 /**
Chris@0 183 * Gets data from the cache backend.
Chris@0 184 *
Chris@0 185 * @param string $cid
Chris@0 186 * The cache ID to return.
Chris@0 187 *
Chris@0 188 * @return mixed
Chris@0 189 * The cached data, if any. This will immediately return FALSE if the
Chris@0 190 * $skipCache property is TRUE.
Chris@0 191 */
Chris@0 192 protected function cacheGet($cid) {
Chris@0 193 if ($this->skipCache) {
Chris@0 194 return FALSE;
Chris@0 195 }
Chris@0 196
Chris@0 197 return $this->cacheBackend->get($this->prepareCid($cid));
Chris@0 198 }
Chris@0 199
Chris@0 200 /**
Chris@0 201 * Sets data to the cache backend.
Chris@0 202 *
Chris@0 203 * @param string $cid
Chris@0 204 * The cache ID to set.
Chris@0 205 * @param mixed $data
Chris@0 206 * The data that will be cached.
Chris@0 207 */
Chris@0 208 protected function cacheSet($cid, $data) {
Chris@0 209 return $this->cacheBackend->set($this->prepareCid($cid), $data, Cache::PERMANENT, ['views_data', 'config:core.extension']);
Chris@0 210 }
Chris@0 211
Chris@0 212 /**
Chris@0 213 * Prepares the cache ID by appending a language code.
Chris@0 214 *
Chris@0 215 * @param string $cid
Chris@0 216 * The cache ID to prepare.
Chris@0 217 *
Chris@0 218 * @return string
Chris@0 219 * The prepared cache ID.
Chris@0 220 */
Chris@0 221 protected function prepareCid($cid) {
Chris@0 222 return $cid . ':' . $this->langcode;
Chris@0 223 }
Chris@0 224
Chris@0 225 /**
Chris@0 226 * Gets all data invoked by hook_views_data().
Chris@0 227 *
Chris@0 228 * This is requested from the cache before being rebuilt.
Chris@0 229 *
Chris@0 230 * @return array
Chris@0 231 * An array of all data.
Chris@0 232 */
Chris@0 233 protected function getData() {
Chris@0 234 $this->fullyLoaded = TRUE;
Chris@0 235
Chris@0 236 if ($data = $this->cacheGet($this->baseCid)) {
Chris@0 237 return $data->data;
Chris@0 238 }
Chris@0 239 else {
Chris@0 240 $modules = $this->moduleHandler->getImplementations('views_data');
Chris@0 241 $data = [];
Chris@0 242 foreach ($modules as $module) {
Chris@0 243 $views_data = $this->moduleHandler->invoke($module, 'views_data');
Chris@0 244 // Set the provider key for each base table.
Chris@0 245 foreach ($views_data as &$table) {
Chris@0 246 if (isset($table['table']) && !isset($table['table']['provider'])) {
Chris@0 247 $table['table']['provider'] = $module;
Chris@0 248 }
Chris@0 249 }
Chris@0 250 $data = NestedArray::mergeDeep($data, $views_data);
Chris@0 251 }
Chris@0 252 $this->moduleHandler->alter('views_data', $data);
Chris@0 253
Chris@0 254 $this->processEntityTypes($data);
Chris@0 255
Chris@0 256 // Keep a record with all data.
Chris@0 257 $this->cacheSet($this->baseCid, $data);
Chris@0 258
Chris@0 259 return $data;
Chris@0 260 }
Chris@0 261 }
Chris@0 262
Chris@0 263 /**
Chris@0 264 * Links tables with 'entity type' to respective generic entity-type tables.
Chris@0 265 *
Chris@0 266 * @param array $data
Chris@0 267 * The array of data to alter entity data for, passed by reference.
Chris@0 268 */
Chris@0 269 protected function processEntityTypes(array &$data) {
Chris@0 270 foreach ($data as $table_name => $table_info) {
Chris@0 271 // Add in a join from the entity-table if an entity-type is given.
Chris@0 272 if (!empty($table_info['table']['entity type'])) {
Chris@0 273 $entity_table = 'views_entity_' . $table_info['table']['entity type'];
Chris@0 274
Chris@0 275 $data[$entity_table]['table']['join'][$table_name] = [
Chris@0 276 'left_table' => $table_name,
Chris@0 277 ];
Chris@0 278 $data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
Chris@0 279 // Copy over the default table group if we have none yet.
Chris@0 280 if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
Chris@0 281 $data[$entity_table]['table']['group'] = $table_info['table']['group'];
Chris@0 282 }
Chris@0 283 }
Chris@0 284 }
Chris@0 285 }
Chris@0 286
Chris@0 287 /**
Chris@0 288 * Fetches a list of all base tables available.
Chris@0 289 *
Chris@0 290 * @return array
Chris@0 291 * An array of base table data keyed by table name. Each item contains the
Chris@0 292 * following keys:
Chris@0 293 * - title: The title label for the base table.
Chris@0 294 * - help: The help text for the base table.
Chris@0 295 * - weight: The weight of the base table.
Chris@0 296 */
Chris@0 297 public function fetchBaseTables() {
Chris@0 298 $tables = [];
Chris@0 299
Chris@0 300 foreach ($this->get() as $table => $info) {
Chris@0 301 if (!empty($info['table']['base'])) {
Chris@0 302 $tables[$table] = [
Chris@0 303 'title' => $info['table']['base']['title'],
Chris@0 304 'help' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
Chris@0 305 'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
Chris@0 306 ];
Chris@0 307 }
Chris@0 308 }
Chris@0 309
Chris@0 310 // Sorts by the 'weight' and then by 'title' element.
Chris@0 311 uasort($tables, function ($a, $b) {
Chris@0 312 if ($a['weight'] != $b['weight']) {
Chris@0 313 return $a['weight'] < $b['weight'] ? -1 : 1;
Chris@0 314 }
Chris@0 315 if ($a['title'] != $b['title']) {
Chris@0 316 return $a['title'] < $b['title'] ? -1 : 1;
Chris@0 317 }
Chris@0 318 return 0;
Chris@0 319 });
Chris@0 320
Chris@0 321 return $tables;
Chris@0 322 }
Chris@0 323
Chris@0 324 /**
Chris@0 325 * Clears the class storage and cache.
Chris@0 326 */
Chris@0 327 public function clear() {
Chris@0 328 $this->storage = [];
Chris@0 329 $this->allStorage = [];
Chris@0 330 $this->fullyLoaded = FALSE;
Chris@0 331 Cache::invalidateTags(['views_data']);
Chris@0 332 }
Chris@0 333
Chris@0 334 }