Issue #2312135 by effulgentsia: Rename and protect ConfigFactory::getCacheKey(s)(); add a separate ConfigFactoryInterface::getCacheKeys().

8.0.x
Alex Pott 2014-08-12 10:45:11 -07:00
parent 55b18b6b79
commit 1e61d83a9b
2 changed files with 42 additions and 33 deletions

View File

@ -107,7 +107,7 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
return $config[$name]; return $config[$name];
} }
else { else {
$cache_key = $this->getCacheKey($name); $cache_key = $this->getConfigCacheKey($name);
// If the config object has been deleted it will already exist in the // If the config object has been deleted it will already exist in the
// cache but self::loadMultiple does not return such objects. // cache but self::loadMultiple does not return such objects.
// @todo Explore making ConfigFactory a listener to the config.delete // @todo Explore making ConfigFactory a listener to the config.delete
@ -143,7 +143,7 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
foreach ($names as $key => $name) { foreach ($names as $key => $name) {
// @todo: Deleted configuration stays in $this->cache, only return // @todo: Deleted configuration stays in $this->cache, only return
// configuration objects that are not new. // configuration objects that are not new.
$cache_key = $this->getCacheKey($name); $cache_key = $this->getConfigCacheKey($name);
if (isset($this->cache[$cache_key]) && !$this->cache[$cache_key]->isNew()) { if (isset($this->cache[$cache_key]) && !$this->cache[$cache_key]->isNew()) {
$list[$name] = $this->cache[$cache_key]; $list[$name] = $this->cache[$cache_key];
unset($names[$key]); unset($names[$key]);
@ -162,7 +162,7 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
} }
foreach ($storage_data as $name => $data) { foreach ($storage_data as $name => $data) {
$cache_key = $this->getCacheKey($name); $cache_key = $this->getConfigCacheKey($name);
$this->cache[$cache_key] = new Config($name, $this->storage, $this->eventDispatcher, $this->typedConfigManager); $this->cache[$cache_key] = new Config($name, $this->storage, $this->eventDispatcher, $this->typedConfigManager);
$this->cache[$cache_key]->initWithData($data); $this->cache[$cache_key]->initWithData($data);
@ -206,7 +206,7 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
public function reset($name = NULL) { public function reset($name = NULL) {
if ($name) { if ($name) {
// Clear all cached configuration for this name. // Clear all cached configuration for this name.
foreach ($this->getCacheKeys($name) as $cache_key) { foreach ($this->getConfigCacheKeys($name) as $cache_key) {
unset($this->cache[$cache_key]); unset($this->cache[$cache_key]);
} }
} }
@ -226,7 +226,7 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
*/ */
public function rename($old_name, $new_name) { public function rename($old_name, $new_name) {
$this->storage->rename($old_name, $new_name); $this->storage->rename($old_name, $new_name);
$old_cache_key = $this->getCacheKey($old_name); $old_cache_key = $this->getConfigCacheKey($old_name);
if (isset($this->cache[$old_cache_key])) { if (isset($this->cache[$old_cache_key])) {
unset($this->cache[$old_cache_key]); unset($this->cache[$old_cache_key]);
} }
@ -240,23 +240,42 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getCacheKey($name) { public function getCacheKeys() {
$keys = array();
if ($this->useOverrides) { if ($this->useOverrides) {
$cache_key = $name . ':overrides'; // Because get() adds overrides both from $GLOBALS and from
// $this->configFactoryOverrides, add cache keys for each.
$keys[] = 'global_overrides';
foreach($this->configFactoryOverrides as $override) { foreach($this->configFactoryOverrides as $override) {
$cache_key = $cache_key . ':' . $override->getCacheSuffix(); $keys[] = $override->getCacheSuffix();
} }
} }
else { return $keys;
$cache_key = $name . ':raw';
}
return $cache_key;
} }
/** /**
* {@inheritdoc} * Gets the cache key for a given config name.
*
* @param string $name
* The name of the configuration object.
*
* @return string
* The cache key.
*/ */
public function getCacheKeys($name) { protected function getConfigCacheKey($name) {
return $name . ':' . implode(':', $this->getCacheKeys());
}
/**
* Gets all the cache keys that match the provided config name.
*
* @param string $name
* The name of the configuration object.
*
* @return array
* An array of cache keys that match the provided config name.
*/
protected function getConfigCacheKeys($name) {
return array_filter(array_keys($this->cache), function($key) use ($name) { return array_filter(array_keys($this->cache), function($key) use ($name) {
// Return TRUE if the key starts with the configuration name. // Return TRUE if the key starts with the configuration name.
return strpos($key, $name . ':') === 0; return strpos($key, $name . ':') === 0;
@ -289,7 +308,7 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface
// replacing the data on any entries for the configuration object apart // replacing the data on any entries for the configuration object apart
// from the one that references the actual config object being saved. // from the one that references the actual config object being saved.
$saved_config = $event->getConfig(); $saved_config = $event->getConfig();
foreach ($this->getCacheKeys($saved_config->getName()) as $cache_key) { foreach ($this->getConfigCacheKeys($saved_config->getName()) as $cache_key) {
$cached_config = $this->cache[$cache_key]; $cached_config = $this->cache[$cache_key];
if ($cached_config !== $saved_config) { if ($cached_config !== $saved_config) {
$this->cache[$cache_key]->setData($saved_config->getRawData()); $this->cache[$cache_key]->setData($saved_config->getRawData());

View File

@ -7,8 +7,6 @@
namespace Drupal\Core\Config; namespace Drupal\Core\Config;
use Drupal\Core\Language\LanguageDefault;
/** /**
* Defines the interface for a configuration object factory. * Defines the interface for a configuration object factory.
*/ */
@ -82,26 +80,18 @@ interface ConfigFactoryInterface {
public function rename($old_name, $new_name); public function rename($old_name, $new_name);
/** /**
* Gets the cache key for a given config name. * The cache keys associated with the state of the config factory.
* *
* @param string $name * All state information that can influence the result of a get() should be
* The name of the configuration object. * included. Typically, this includes a key for each override added via
* * addOverride(). This allows external code to maintain caches of
* @return string * configuration data in addition to or instead of caches maintained by the
* The cache key. * factory.
*/
public function getCacheKey($name);
/**
* Gets all the cache keys that match the provided config name.
*
* @param string $name
* The name of the configuration object.
* *
* @return array * @return array
* An array of cache keys that match the provided config name. * An array of strings, used to generate a cache ID.
*/ */
public function getCacheKeys($name); public function getCacheKeys();
/** /**
* Clears the config factory static cache. * Clears the config factory static cache.