diff --git a/core/lib/Drupal/Core/Cache/ExtensibleCacheContextsInterface.php b/core/lib/Drupal/Core/Cache/RuntimeCacheContextDependencyInterface.php similarity index 18% rename from core/lib/Drupal/Core/Cache/ExtensibleCacheContextsInterface.php rename to core/lib/Drupal/Core/Cache/RuntimeCacheContextDependencyInterface.php index 1f1256f..1c84991 100644 --- a/core/lib/Drupal/Core/Cache/ExtensibleCacheContextsInterface.php +++ b/core/lib/Drupal/Core/Cache/RuntimeCacheContextDependencyInterface.php @@ -2,26 +2,24 @@ /** * @file - * Contains \Drupal\Core\Cache\ExtensibleCacheContextsInterface. + * Contains \Drupal\Core\Cache\RuntimeCacheContextDependencyInterface. */ namespace Drupal\Core\Cache; -use Drupal\Core\Cache\Context\CacheContextInterface; - /** - * Defines an interface for objects that can extend the list of cache contexts. + * Defines an interface for objects that can add cache contexts during runtime. * * @ingroup cache */ -interface ExtensibleCacheContextsInterface { +interface RuntimeCacheContextDependencyInterface { /** - * Adds a cache context. + * Adds cache contexts during runtime. * - * @param \Drupal\Core\Cache\Context\CacheContextInterface $cache_context - * The cache context to add. + * @param string[] $cache_contexts + * An array of cache context tokens to add. */ - public function addCacheContext(CacheContextInterface $cache_context); + public function addRuntimeCacheContexts(array $cache_contexts); } diff --git a/core/lib/Drupal/Core/Config/ConfigBase.php b/core/lib/Drupal/Core/Config/ConfigBase.php index eee76a2..9fc9cdf 100644 --- a/core/lib/Drupal/Core/Config/ConfigBase.php +++ b/core/lib/Drupal/Core/Config/ConfigBase.php @@ -11,6 +11,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\RuntimeCacheContextDependencyInterface; use \Drupal\Core\DependencyInjection\DependencySerializationTrait; /** @@ -28,7 +29,7 @@ * @see \Drupal\Core\Config\Config * @see \Drupal\Core\Theme\ThemeSettings */ -abstract class ConfigBase implements CacheableDependencyInterface { +abstract class ConfigBase implements CacheableDependencyInterface, RuntimeCacheContextDependencyInterface { use DependencySerializationTrait; /** @@ -46,6 +47,13 @@ protected $data = array(); /** + * The cache contexts this configuration object provides. + * + * @var array + */ + protected $cacheContexts = []; + + /** * The maximum length of a configuration object name. * * Many filesystems (including HFS, NTFS, and ext4) have a maximum file name @@ -269,7 +277,7 @@ public function merge(array $data_to_merge) { * {@inheritdoc} */ public function getCacheContexts() { - return []; + return $this->cacheContexts; } /** @@ -286,4 +294,11 @@ public function getCacheMaxAge() { return Cache::PERMANENT; } + /** + * {@inheritdoc} + */ + public function addRuntimeCacheContexts(array $cache_contexts) { + $this->cacheContexts = Cache::mergeContexts($this->cacheContexts, $cache_contexts); + } + } diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index 93a3ac0..fdec551 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -10,7 +10,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Context\CacheContextInterface; -use Drupal\Core\Cache\ExtensibleCacheContextsInterface; +use Drupal\Core\Cache\RuntimeCacheContextDependencyInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -29,7 +29,7 @@ * * @ingroup config_api */ -class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface, ExtensibleCacheContextsInterface { +class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface { /** * A storage instance for reading and writing configuration data. @@ -128,6 +128,10 @@ protected function doGet($name, $immutable = TRUE) { $this->cache[$cache_key]->setSettingsOverride($GLOBALS['config'][$name]); } } + + // Propagate cache contexts to the config object. + $this->cache[$cache_key]->addRuntimeCacheContexts($this->loadCacheContextOverrides()); + return $this->cache[$cache_key]; } } @@ -185,6 +189,10 @@ protected function doLoadMultiple(array $names, $immutable = TRUE) { $this->cache[$cache_key]->setSettingsOverride($GLOBALS['config'][$name]); } } + + // Propagate cache contexts to the config object. + $this->cache[$cache_key]->addRuntimeCacheContexts($this->loadCacheContextOverrides()); + $list[$name] = $this->cache[$cache_key]; } } @@ -212,6 +220,20 @@ protected function loadOverrides(array $names) { } /** + * Get arbitrary cache context overrides. + * + * @return array + * An array of cache context tokens. + */ + protected function loadCacheContextOverrides() { + $overrides = []; + foreach ($this->configFactoryOverrides as $override) { + $overrides = Cache::mergeContexts($overrides, $override->getCacheContexts()); + } + return $overrides; + } + + /** * {@inheritdoc} */ public function reset($name = NULL) { @@ -382,10 +404,4 @@ protected function createConfigObject($name, $immutable) { return new Config($name, $this->storage, $this->eventDispatcher, $this->typedConfigManager); } - /** - * {@inheritdoc} - */ - public function addCacheContext(CacheContextInterface $cache_context) { - // @todo Implement this. - } } diff --git a/core/modules/config/src/Tests/CacheContextConfigOverrideTest.php b/core/modules/config/src/Tests/CacheContextConfigOverrideTest.php index 5db9e76..00ce58b 100644 --- a/core/modules/config/src/Tests/CacheContextConfigOverrideTest.php +++ b/core/modules/config/src/Tests/CacheContextConfigOverrideTest.php @@ -26,11 +26,12 @@ public function testConfigOverride() { $GLOBALS['arr_me_mateys'] = TRUE; $config_factory = $this->container->get('config.factory'); - $theme = $config_factory->get('system.theme')->get('default'); + $config = $config_factory->get('system.theme'); + $theme = $config->get('default'); $this->assertEqual('pirate', $theme); - // @todo Test if the cache context is set. + $this->assertEquals(['pirate_day'], $config->getCacheContexts); } } diff --git a/core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php b/core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php index a9567db..47f7581 100644 --- a/core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php +++ b/core/modules/config/tests/config_override_test/src/PirateDayCacheContextConfigOverride.php @@ -55,6 +55,8 @@ public function getCacheContexts() { * {@inheritdoc} */ public function getCacheTags() { + // @todo This is here because we now implement CacheableDependencyInterface, + // but this is not used. return []; } @@ -62,6 +64,8 @@ public function getCacheTags() { * {@inheritdoc} */ public function getCacheMaxAge() { + // @todo This is here because we now implement CacheableDependencyInterface, + // but this is not used. return Cache::PERMANENT; }