Issue #1892462 by tim.plunkett: EntityManager should use the CacheDecorator.

8.0.x
catch 2013-01-29 13:20:49 +00:00
parent 8c1caedacd
commit 9d276ca5f5
5 changed files with 13 additions and 76 deletions

View File

@ -6430,7 +6430,8 @@ function drupal_flush_all_caches() {
drupal_static_reset();
// Clear all non-drupal_static() static caches.
// None currently; kept if any static caches need to be reset in the future.
// @todo Rebuild the kernel/container.
drupal_container()->get('plugin.manager.entity')->clearCachedDefinitions();
// Rebuild module and theme data.
system_rebuild_module_data();

View File

@ -23,24 +23,15 @@ use Drupal\Core\Entity\EntityInterface;
*
* @see \Drupal\Core\Entity\EntityManager
* @see hook_entity_info_alter()
*
* @deprecated Use \Drupal\Core\Entity\EntityManager::getDefinitions() directly.
*/
function entity_get_info($entity_type = NULL) {
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['entity_info'] = &drupal_static(__FUNCTION__);
}
$entity_info = &$drupal_static_fast['entity_info'];
if (empty($entity_info)) {
$entity_info = drupal_container()->get('plugin.manager.entity')->getDefinitions();
}
if (empty($entity_type)) {
return $entity_info;
return drupal_container()->get('plugin.manager.entity')->getDefinitions();
}
elseif (isset($entity_info[$entity_type])) {
return $entity_info[$entity_type];
else {
return drupal_container()->get('plugin.manager.entity')->getDefinition($entity_type);
}
}
@ -48,11 +39,10 @@ function entity_get_info($entity_type = NULL) {
* Resets the cached information about entity types.
*/
function entity_info_cache_clear() {
drupal_static_reset('entity_get_info');
drupal_static_reset('entity_get_view_modes');
drupal_static_reset('entity_get_bundles');
// Clear all languages.
cache()->deleteTags(array('entity_info' => TRUE));
drupal_container()->get('plugin.manager.entity')->clearCachedDefinitions();
}
/**

View File

@ -13,7 +13,7 @@ namespace Drupal\Component\Plugin\Discovery;
interface CachedDiscoveryInterface extends DiscoveryInterface {
/**
* Clears cached plugin definitions.
* Clears static and persistent plugin definition caches.
*/
public function clearCachedDefinitions();

View File

@ -11,6 +11,7 @@ use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\InfoHookDecorator;
use Drupal\Core\Cache\CacheBackendInterface;
@ -129,34 +130,6 @@ use Drupal\Core\Cache\CacheBackendInterface;
*/
class EntityManager extends PluginManagerBase {
/**
* The cache bin used for entity plugin definitions.
*
* @var string
*/
protected $cacheBin = 'cache';
/**
* The cache key used for entity plugin definitions.
*
* @var string
*/
protected $cacheKey = 'entity_info';
/**
* The cache expiration for entity plugin definitions.
*
* @var int
*/
protected $cacheExpire = CacheBackendInterface::CACHE_PERMANENT;
/**
* The cache tags used for entity plugin definitions.
*
* @var array
*/
protected $cacheTags = array('entity_info' => TRUE);
/**
* Contains instantiated controllers keyed by controller type and entity type.
*
@ -197,36 +170,9 @@ class EntityManager extends PluginManagerBase {
$this->discovery = new InfoHookDecorator($this->discovery, 'entity_info');
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new AlterDecorator($this->discovery, 'entity_info');
$this->factory = new DefaultFactory($this);
$this->discovery = new CacheDecorator($this->discovery, 'entity_info:' . language(LANGUAGE_TYPE_INTERFACE)->langcode, 'cache', CacheBackendInterface::CACHE_PERMANENT, array('entity_info' => TRUE));
// Entity type plugins includes translated strings, so each language is
// cached separately.
$this->cacheKey .= ':' . language(LANGUAGE_TYPE_INTERFACE)->langcode;
}
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase::getDefinition().
*/
public function getDefinition($plugin_id) {
$definitions = $this->getDefinitions();
return isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : NULL;
}
/**
* Overrides Drupal\Component\Plugin\PluginManagerBase::getDefinitions().
*/
public function getDefinitions() {
// Because \Drupal\Core\Plugin\Discovery\CacheDecorator runs before
// definitions are processed and does not support cache tags, we perform our
// own caching.
if ($cache = cache($this->cacheBin)->get($this->cacheKey)) {
return $cache->data;
}
else {
$definitions = parent::getDefinitions();
cache($this->cacheBin)->set($this->cacheKey, $definitions, $this->cacheExpire, $this->cacheTags);
return $definitions;
}
$this->factory = new DefaultFactory($this->discovery);
}
/**

View File

@ -115,7 +115,7 @@ class EntityTranslationSettingsTest extends WebTestBase {
$this->drupalPost('admin/config/regional/content-language', $edit, t('Save'));
$args = array('@entity_type' => $entity_type, '@bundle' => $bundle, '@enabled' => $enabled ? 'enabled' : 'disabled');
$message = format_string('Translation for entity @entity_type (@bundle) is @enabled.', $args);
drupal_static_reset();
entity_info_cache_clear();
return $this->assertEqual(translation_entity_enabled($entity_type, $bundle), $enabled, $message);
}