Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\views\ViewsData.
Angie Byron
committed
*/
namespace Drupal\views;
use Drupal\Component\Utility\NestedArray;
Alex Pott
committed
use Drupal\Core\Cache\Cache;
Angie Byron
committed
use Drupal\Core\Cache\CacheBackendInterface;
Alex Pott
committed
use Drupal\Core\Config\ConfigFactoryInterface;
Alex Pott
committed
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
Angie Byron
committed
/**
* Class to manage and lazy load cached views data.
*
* If a table is requested and cannot be loaded from cache, all data is then
* requested from cache. A table-specific cache entry will then be created for
* the requested table based on this cached data. Table data is only rebuilt
* when no cache entry for all table data can be retrieved.
Angie Byron
committed
*/
class ViewsData {
Angie Byron
committed
/**
* The base cache ID to use.
*
* @var string
*/
protected $baseCid = 'views_data';
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheBackend;
/**
* Table data storage.
*
* This is used for explicitly requested tables.
Angie Byron
committed
*
* @var array
*/
protected $storage = array();
/**
* All table storage data loaded from cache.
*
* This is used when all data has been loaded from the cache to prevent
* further cache get calls when rebuilding all data or for single tables.
*
* @var array
*/
protected $allStorage = array();
Angie Byron
committed
/**
* Whether the data has been fully loaded in this request.
Angie Byron
committed
*
* @var bool
Angie Byron
committed
*/
protected $fullyLoaded = FALSE;
Angie Byron
committed
/**
* Whether or not to skip data caching and rebuild data each time.
*
* @var bool
*/
protected $skipCache = FALSE;
Angie Byron
committed
/**
* The current language code.
Angie Byron
committed
*
* @var string
Angie Byron
committed
*/
protected $langcode;
Angie Byron
committed
Alex Pott
committed
/**
* Stores a module manager to invoke hooks.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The language manager
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
Alex Pott
committed
/**
* Constructs this ViewsData object.
Alex Pott
committed
*
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* The cache backend to use.
Alex Pott
committed
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
Alex Pott
committed
* The configuration factory object to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler class to use for invoking hooks.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
Alex Pott
committed
*/
Alex Pott
committed
public function __construct(CacheBackendInterface $cache_backend, ConfigFactoryInterface $config, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
Angie Byron
committed
$this->cacheBackend = $cache_backend;
Alex Pott
committed
$this->moduleHandler = $module_handler;
$this->languageManager = $language_manager;
Angie Byron
committed
$this->langcode = $this->languageManager->getCurrentLanguage()->getId();
$this->skipCache = $config->get('views.settings')->get('skip_cache');
Angie Byron
committed
}
/**
* Gets data for a particular table, or all tables.
Angie Byron
committed
*
* @param string|null $key
* The key of the cache entry to retrieve. Defaults to NULL, this will
* return all table data.
Angie Byron
committed
*
* @return array $data
* An array of table data.
Angie Byron
committed
*/
public function get($key = NULL) {
if ($key) {
if (!isset($this->storage[$key])) {
Angie Byron
committed
// Prepare a cache ID for get and set.
Angie Byron
committed
$cid = $this->baseCid . ':' . $key;
$from_cache = FALSE;
if ($data = $this->cacheGet($cid)) {
Angie Byron
committed
$this->storage[$key] = $data->data;
$from_cache = TRUE;
Angie Byron
committed
}
// If there is no cached entry and data is not already fully loaded,
// rebuild. This will stop requests for invalid tables calling getData.
elseif (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
Angie Byron
committed
}
if (!$from_cache) {
if (!isset($this->allStorage[$key])) {
// Write an empty cache entry if no information for that table
// exists to avoid repeated cache get calls for this table and
// prevent loading all tables unnecessarily.
$this->storage[$key] = array();
$this->allStorage[$key] = array();
else {
$this->storage[$key] = $this->allStorage[$key];
}
// Create a cache entry for the requested table.
$this->cacheSet($cid, $this->allStorage[$key]);
}
Angie Byron
committed
return $this->storage[$key];
Angie Byron
committed
}
else {
if (!$this->fullyLoaded) {
$this->allStorage = $this->getData();
Angie Byron
committed
}
// Set storage from allStorage outside of the fullyLoaded check to prevent
// cache calls on requests that have requested all data to get a single
// tables data. Make sure $this->storage is populated in this case.
$this->storage = $this->allStorage;
Angie Byron
committed
}
return $this->allStorage;
Angie Byron
committed
}
/**
* Gets data from the cache backend.
*
* @param string $cid
* The cache ID to return.
*
* @return mixed
* The cached data, if any. This will immediately return FALSE if the
* $skipCache property is TRUE.
*/
protected function cacheGet($cid) {
if ($this->skipCache) {
return FALSE;
}
return $this->cacheBackend->get($this->prepareCid($cid));
}
Angie Byron
committed
Angie Byron
committed
/**
* Sets data to the cache backend.
*
* @param string $cid
* The cache ID to set.
* @param mixed $data
* The data that will be cached.
*/
protected function cacheSet($cid, $data) {
return $this->cacheBackend->set($this->prepareCid($cid), $data, Cache::PERMANENT, array('views_data', 'extension', 'extension:views'));
Angie Byron
committed
}
/**
* Prepares the cache ID by appending a language code.
*
* @param string $cid
* The cache ID to prepare.
*
* @return string
* The prepared cache ID.
*/
protected function prepareCid($cid) {
return $cid . ':' . $this->langcode;
Angie Byron
committed
}
/**
* Gets all data invoked by hook_views_data().
*
* This is requested from the cache before being rebuilt.
*
Angie Byron
committed
* @return array
* An array of all data.
*/
protected function getData() {
$this->fullyLoaded = TRUE;
Angie Byron
committed
if ($data = $this->cacheGet($this->baseCid)) {
return $data->data;
}
else {
$modules = $this->moduleHandler->getImplementations('views_data');
$data = [];
foreach ($modules as $module) {
$views_data = $this->moduleHandler->invoke($module, 'views_data');
// Set the provider key for each base table.
foreach ($views_data as &$table) {
if (isset($table['table']) && !isset($table['table']['provider'])) {
$table['table']['provider'] = $module;
}
}
$data = NestedArray::mergeDeep($data, $views_data);
}
Alex Pott
committed
$this->moduleHandler->alter('views_data', $data);
$this->processEntityTypes($data);
Angie Byron
committed
// Keep a record with all data.
Angie Byron
committed
$this->cacheSet($this->baseCid, $data);
Angie Byron
committed
return $data;
}
Angie Byron
committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
}
/**
* Links tables with 'entity type' to respective generic entity-type tables.
*
* @param array $data
* The array of data to alter entity data for, passed by reference.
*/
protected function processEntityTypes(array &$data) {
foreach ($data as $table_name => $table_info) {
// Add in a join from the entity-table if an entity-type is given.
if (!empty($table_info['table']['entity type'])) {
$entity_table = 'views_entity_' . $table_info['table']['entity type'];
$data[$entity_table]['table']['join'][$table_name] = array(
'left_table' => $table_name,
);
$data[$entity_table]['table']['entity type'] = $table_info['table']['entity type'];
// Copy over the default table group if we have none yet.
if (!empty($table_info['table']['group']) && empty($data[$entity_table]['table']['group'])) {
$data[$entity_table]['table']['group'] = $table_info['table']['group'];
}
}
}
}
catch
committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Fetches a list of all base tables available.
*
* @return array
* An array of base table data keyed by table name. Each item contains the
* following keys:
* - title: The title label for the base table.
* - help: The help text for the base table.
* - weight: The weight of the base table.
*/
public function fetchBaseTables() {
$tables = array();
foreach ($this->get() as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = array(
'title' => $info['table']['base']['title'],
'help' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
);
}
}
// Sorts by the 'weight' and then by 'title' element.
uasort($tables, function ($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return $a['title'] < $b['title'] ? -1 : 1;
}
return 0;
});
return $tables;
}
/**
* Clears the class storage and cache.
*/
public function clear() {
$this->storage = array();
$this->allStorage = array();
$this->fullyLoaded = FALSE;
catch
committed
Cache::invalidateTags(array('views_data'));