diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index 6148a7b..cbc19c7 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -128,7 +128,7 @@ protected function doGet($name, $immutable = TRUE) { } // Propagate cache contexts to the config object. - $this->propagateCacheableDependencyOverrides($cache_key); + $this->propagateCacheableDependencyOverrides($cache_key, $name); return $this->cache[$cache_key]; } @@ -189,7 +189,7 @@ protected function doLoadMultiple(array $names, $immutable = TRUE) { } // Propagate cacheable dependencies to the config object. - $this->propagateCacheableDependencyOverrides($cache_key); + $this->propagateCacheableDependencyOverrides($cache_key, $name); $list[$name] = $this->cache[$cache_key]; } @@ -222,13 +222,15 @@ protected function loadOverrides(array $names) { * * @param string $cache_key * The key of the cached config object to update. + * @param string $name + * The name of the configuration object to construct. */ - protected function propagateCacheableDependencyOverrides($cache_key) { + protected function propagateCacheableDependencyOverrides($cache_key, $name) { foreach ($this->configFactoryOverrides as $override) { $this->cache[$cache_key] - ->addCacheContexts($override->getCacheContexts()) - ->addCacheTags($override->getCacheTags()) - ->mergeCacheMaxAge($override->getCacheMaxAge()); + ->addCacheContexts($override->getCacheContexts($name)) + ->addCacheTags($override->getCacheTags($name)) + ->mergeCacheMaxAge($override->getCacheMaxAge($name)); } } diff --git a/core/lib/Drupal/Core/Config/ConfigFactoryOverrideInterface.php b/core/lib/Drupal/Core/Config/ConfigFactoryOverrideInterface.php index 7b07c1a..0a6852d 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactoryOverrideInterface.php +++ b/core/lib/Drupal/Core/Config/ConfigFactoryOverrideInterface.php @@ -7,12 +7,10 @@ namespace Drupal\Core\Config; -use Drupal\Core\Cache\CacheableDependencyInterface; - /** * Defines the interface for a configuration factory override object. */ -interface ConfigFactoryOverrideInterface extends CacheableDependencyInterface { +interface ConfigFactoryOverrideInterface { /** * Returns config overrides. @@ -60,4 +58,51 @@ public function getCacheSuffix(); */ public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION); + /** + * The cache contexts associated with this config factory override. + * + * These identify a specific variation/representation of the object. + * + * Cache contexts are tokens: placeholders that are converted to cache keys by + * the @cache_contexts_manager service. The replacement value depends on the + * request context (the current URL, language, and so on). They're converted + * before storing an object in cache. + * + * @param string $name + * The name of the configuration object that is being constructed. + * + * @return string[] + * An array of cache context tokens, used to generate a cache ID. + * + * @see \Drupal\Core\Cache\Context\CacheContextsManager::convertTokensToKeys() + */ + public function getCacheContexts($name); + + /** + * The cache tags associated with this config factory override. + * + * When this object is modified, these cache tags will be invalidated. + * + * Since a cache tag is already associated with every config object this + * should only be used if multiple config objects are being overridden. + * + * @param string $name + * The name of the configuration object that is being constructed. + * + * @return string[] + * A set of cache tags. + */ + public function getCacheTags($name); + + /** + * The maximum age for which this object may be cached. + * + * @param string $name + * The name of the configuration object that is being constructed. + * + * @return int + * The maximum time in seconds that this object may be cached. + */ + public function getCacheMaxAge($name); + } diff --git a/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php b/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php index 8dad5bb..93837cc 100644 --- a/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php +++ b/core/modules/config/tests/config_entity_static_cache_test/src/ConfigOverrider.php @@ -44,21 +44,21 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL /** * {@inheritdoc} */ - public function getCacheContexts() { + public function getCacheContexts($name) { return []; } /** * {@inheritdoc} */ - public function getCacheTags() { + public function getCacheTags($name) { return []; } /** * {@inheritdoc} */ - public function getCacheMaxAge() { + public function getCacheMaxAge($name) { return Cache::PERMANENT; } diff --git a/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php b/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php index e80eeec..c4956df 100644 --- a/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php +++ b/core/modules/config/tests/config_override_integration_test/src/CacheabilityMetadataConfigOverride.php @@ -52,21 +52,27 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL /** * {@inheritdoc} */ - public function getCacheContexts() { - return ['config_override_integration_test']; + public function getCacheContexts($name) { + if ($name === 'block.block.config_override_test') { + return ['config_override_integration_test']; + } + return []; } /** * {@inheritdoc} */ - public function getCacheTags() { - return ['config_override_integration_test_tag']; + public function getCacheTags($name) { + if ($name === 'block.block.config_override_test') { + return ['config_override_integration_test_tag']; + } + return []; } /** * {@inheritdoc} */ - public function getCacheMaxAge() { + public function getCacheMaxAge($name) { return Cache::PERMANENT; } diff --git a/core/modules/config/tests/config_override_test/src/ConfigOverrider.php b/core/modules/config/tests/config_override_test/src/ConfigOverrider.php index b985782..a06cb2e 100644 --- a/core/modules/config/tests/config_override_test/src/ConfigOverrider.php +++ b/core/modules/config/tests/config_override_test/src/ConfigOverrider.php @@ -48,21 +48,21 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL /** * {@inheritdoc} */ - public function getCacheContexts() { + public function getCacheContexts($name) { return []; } /** * {@inheritdoc} */ - public function getCacheTags() { + public function getCacheTags($name) { return []; } /** * {@inheritdoc} */ - public function getCacheMaxAge() { + public function getCacheMaxAge($name) { return Cache::PERMANENT; } diff --git a/core/modules/config/tests/config_override_test/src/ConfigOverriderLowPriority.php b/core/modules/config/tests/config_override_test/src/ConfigOverriderLowPriority.php index 1d6f073..ca4f5bf 100644 --- a/core/modules/config/tests/config_override_test/src/ConfigOverriderLowPriority.php +++ b/core/modules/config/tests/config_override_test/src/ConfigOverriderLowPriority.php @@ -53,21 +53,21 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL /** * {@inheritdoc} */ - public function getCacheContexts() { + public function getCacheContexts($name) { return []; } /** * {@inheritdoc} */ - public function getCacheTags() { + public function getCacheTags($name) { return []; } /** * {@inheritdoc} */ - public function getCacheMaxAge() { + public function getCacheMaxAge($name) { return Cache::PERMANENT; } diff --git a/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php b/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php index 86779d7..2cb421d 100644 --- a/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php +++ b/core/modules/config/tests/config_override_test/src/PirateDayCacheabilityMetadataConfigOverride.php @@ -8,6 +8,7 @@ namespace Drupal\config_override_test; use Drupal\config_override_test\Cache\PirateDayCacheContext; +use Drupal\Core\Cache\Cache; use Drupal\Core\Config\ConfigFactoryOverrideInterface; use Drupal\Core\Config\StorageInterface; @@ -56,22 +57,44 @@ public function createConfigObject($name, $collection = StorageInterface::DEFAUL /** * {@inheritdoc} */ - public function getCacheContexts() { - return ['pirate_day']; + public function getCacheContexts($name) { + if ($this->isCacheabilityMetadataApplicable($name)) { + return ['pirate_day']; + } + return []; } /** * {@inheritdoc} */ - public function getCacheTags() { - return ['pirate-day-tag']; + public function getCacheTags($name) { + if ($this->isCacheabilityMetadataApplicable($name)) { + return ['pirate-day-tag']; + } + return []; } /** * {@inheritdoc} */ - public function getCacheMaxAge() { - return PirateDayCacheContext::PIRATE_DAY_MAX_AGE; + public function getCacheMaxAge($name) { + if ($this->isCacheabilityMetadataApplicable($name)) { + return PirateDayCacheContext::PIRATE_DAY_MAX_AGE; + } + return Cache::PERMANENT; + } + + /** + * Returns whether or not our overrides are potentially applicable. + * + * @param string $name + * The name of the config object that is being constructed. + * + * @return bool + * TRUE if the merchant ship will be boarded. FALSE if we drink rum instead. + */ + protected function isCacheabilityMetadataApplicable($name) { + return in_array($name, ['system.theme', 'block.block.call_to_action']); } } diff --git a/core/modules/language/src/Config/LanguageConfigFactoryOverride.php b/core/modules/language/src/Config/LanguageConfigFactoryOverride.php index e2e8c3b..4015573 100644 --- a/core/modules/language/src/Config/LanguageConfigFactoryOverride.php +++ b/core/modules/language/src/Config/LanguageConfigFactoryOverride.php @@ -226,21 +226,24 @@ public function onConfigDelete(ConfigCrudEvent $event) { /** * {@inheritdoc} */ - public function getCacheContexts() { - return ['languages:language_interface']; + public function getCacheContexts($name) { + if ($this->language) { + return ['languages:language_interface']; + } + return []; } /** * {@inheritdoc} */ - public function getCacheTags() { + public function getCacheTags($name) { return []; } /** * {@inheritdoc} */ - public function getCacheMaxAge() { + public function getCacheMaxAge($name) { return Cache::PERMANENT; }