core/core.services.yml | 2 +- core/lib/Drupal/Core/Config/Config.php | 17 ++++++++++++++--- core/lib/Drupal/Core/Config/ConfigFactory.php | 17 ++++++++++++++--- core/lib/Drupal/Core/Menu/MenuTreeStorage.php | 19 +++++++++++++++---- .../src/EventSubscriber/ThemeSettingsCacheTag.php | 17 ++++++++++++++--- core/modules/system/system.services.yml | 2 +- core/tests/Drupal/Tests/Core/Config/ConfigTest.php | 6 +----- 7 files changed, 60 insertions(+), 20 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 9ab7af7..880c561 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -134,7 +134,7 @@ services: tags: - { name: event_subscriber } - { name: service_collector, tag: 'config.factory.override', call: addOverride } - arguments: ['@config.storage', '@event_dispatcher', '@config.typed'] + arguments: ['@config.storage', '@event_dispatcher', '@config.typed', '@cache_tags.invalidator'] config.installer: class: Drupal\Core\Config\ConfigInstaller arguments: ['@config.factory', '@config.storage', '@config.typed', '@config.manager', '@event_dispatcher'] diff --git a/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php index bc6ba29..a5e745d 100644 --- a/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -55,6 +56,13 @@ class Config extends StorableConfigBase { protected $settingsOverrides; /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $cacheTagsInvalidator; + + /** * Constructs a configuration object. * * @param string $name @@ -66,12 +74,15 @@ class Config extends StorableConfigBase { * An event dispatcher instance to use for configuration events. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config * The typed configuration manager service. + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator + * The cache tags invalidator. */ - public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) { + public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, CacheTagsInvalidatorInterface $cache_tags_invalidator) { $this->name = $name; $this->storage = $storage; $this->eventDispatcher = $event_dispatcher; $this->typedConfigManager = $typed_config; + $this->cacheTagsInvalidator = $cache_tags_invalidator; } /** @@ -224,7 +235,7 @@ public function save() { } $this->storage->write($this->name, $this->data); - Cache::invalidateTags($this->getCacheTags()); + $this->cacheTagsInvalidator->invalidateTags($this->getCacheTags()); $this->isNew = FALSE; $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this)); $this->originalData = $this->data; @@ -240,7 +251,7 @@ public function save() { public function delete() { $this->data = array(); $this->storage->delete($this->name); - Cache::invalidateTags($this->getCacheTags()); + $this->cacheTagsInvalidator->invalidateTags($this->getCacheTags()); $this->isNew = TRUE; $this->resetOverriddenData(); $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this)); diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index b1a8932..313f23e 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Config; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -71,6 +72,13 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface protected $configFactoryOverrides = array(); /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $cacheTagsInvalidator; + + /** * Constructs the Config factory. * * @param \Drupal\Core\Config\StorageInterface $storage @@ -79,11 +87,14 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface * An event dispatcher instance to use for configuration events. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config * The typed configuration manager. + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator + * The cache tags invalidator. */ - public function __construct(StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) { + public function __construct(StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config, CacheTagsInvalidatorInterface $cache_tags_invalidator) { $this->storage = $storage; $this->eventDispatcher = $event_dispatcher; $this->typedConfigManager = $typed_config; + $this->cacheTagsInvalidator = $cache_tags_invalidator; } /** @@ -112,7 +123,7 @@ public function get($name) { // If the configuration object does not exist in the configuration // storage, create a new object and add it to the static cache. $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->cacheTagsInvalidator); if ($this->useOverrides) { // Get and apply any overrides. @@ -157,7 +168,7 @@ public function loadMultiple(array $names) { foreach ($storage_data as $name => $data) { $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->cacheTagsInvalidator); $this->cache[$cache_key]->initWithData($data); if ($this->useOverrides) { if (isset($module_overrides[$name])) { diff --git a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php index 8ec44cb..f0d9d94 100644 --- a/core/lib/Drupal/Core/Menu/MenuTreeStorage.php +++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php @@ -12,6 +12,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\SelectInterface; @@ -42,6 +43,13 @@ class MenuTreeStorage implements MenuTreeStorageInterface { protected $menuCacheBackend; /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $cacheTagsInvalidator; + + /** * The database table name. * * @var string @@ -109,14 +117,17 @@ class MenuTreeStorage implements MenuTreeStorageInterface { * A Database connection to use for reading and writing configuration data. * @param \Drupal\Core\Cache\CacheBackendInterface $menu_cache_backend * Cache backend instance for the extracted tree data. + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator + * The cache tags invalidator. * @param string $table * A database table name to store configuration data in. * @param array $options * (optional) Any additional database connection options to use in queries. */ - public function __construct(Connection $connection, CacheBackendInterface $menu_cache_backend, $table, array $options = array()) { + public function __construct(Connection $connection, CacheBackendInterface $menu_cache_backend, CacheTagsInvalidatorInterface $cache_tags_invalidator, $table, array $options = array()) { $this->connection = $connection; $this->menuCacheBackend = $menu_cache_backend; + $this->cacheTagsInvalidator = $cache_tags_invalidator; $this->table = $table; $this->options = $options; } @@ -181,7 +192,7 @@ public function rebuild(array $definitions) { $affected_menus = $this->getMenuNames() + $before_menus; // Invalidate any cache tagged with any menu name. $cache_tags = Cache::buildTags('config:system.menu', $affected_menus, '.'); - Cache::invalidateTags($cache_tags); + $this->cacheTagsInvalidator->invalidateTags($cache_tags); $this->resetDefinitions(); // Every item in the cache bin should have one of the menu cache tags but it // is not guaranteed, so invalidate everything in the bin. @@ -242,7 +253,7 @@ public function save(array $link) { $affected_menus = $this->doSave($link); $this->resetDefinitions(); $cache_tags = Cache::buildTags('config:system.menu', $affected_menus, '.'); - Cache::invalidateTags($cache_tags); + $this->cacheTagsInvalidator->invalidateTags($cache_tags); return $affected_menus; } @@ -421,7 +432,7 @@ public function delete($id) { $this->updateParentalStatus($item); // Many children may have moved. $this->resetDefinitions(); - Cache::invalidateTags(array('config:system.menu.' . $item['menu_name'])); + $this->cacheTagsInvalidator->invalidateTags(['config:system.menu.' . $item['menu_name']]); } } diff --git a/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php b/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php index a6a6b7c..eb68d75 100644 --- a/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php +++ b/core/modules/system/src/EventSubscriber/ThemeSettingsCacheTag.php @@ -8,6 +8,7 @@ namespace Drupal\system\EventSubscriber; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Config\ConfigCrudEvent; use Drupal\Core\Config\ConfigEvents; use Drupal\Core\Extension\ThemeHandlerInterface; @@ -26,13 +27,23 @@ class ThemeSettingsCacheTag implements EventSubscriberInterface { protected $themeHandler; /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + protected $cacheTagsInvalidator; + + /** * Constructs a ThemeSettingsCacheTag object. * * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler * The theme handler. + * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_invalidator + * The cache tags invalidator. */ - public function __construct(ThemeHandlerInterface $theme_handler) { + public function __construct(ThemeHandlerInterface $theme_handler, CacheTagsInvalidatorInterface $cache_tags_invalidator) { $this->themeHandler = $theme_handler; + $this->cacheTagsInvalidator = $cache_tags_invalidator; } /** @@ -44,14 +55,14 @@ public function __construct(ThemeHandlerInterface $theme_handler) { public function onSave(ConfigCrudEvent $event) { // Global theme settings. if ($event->getConfig()->getName() === 'system.theme.global') { - Cache::invalidateTags(['rendered']); + $this->cacheTagsInvalidator->invalidateTags(['rendered']); } // Theme-specific settings, check if this matches a theme settings // configuration object, in that case, clear the rendered cache tag. foreach (array_keys($this->themeHandler->listInfo()) as $theme_name) { if ($theme_name == $event->getConfig()->getName()) { - Cache::invalidateTags(['rendered']); + $this->cacheTagsInvalidator->invalidateTags(['rendered']); break; } } diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index 97cd4b9..2a21015 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -44,6 +44,6 @@ services: - { name: event_subscriber } system.theme_settings_cache_tag: class: Drupal\system\EventSubscriber\ThemeSettingsCacheTag - arguments: ['@theme_handler'] + arguments: ['@theme_handler', '@cache_tags.invalidator'] tags: - { name: event_subscriber } diff --git a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php index 549e28a..7a872be 100644 --- a/core/tests/Drupal/Tests/Core/Config/ConfigTest.php +++ b/core/tests/Drupal/Tests/Core/Config/ConfigTest.php @@ -62,12 +62,8 @@ public function setUp() { $this->storage = $this->getMock('Drupal\Core\Config\StorageInterface'); $this->eventDispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); $this->typedConfig = $this->getMock('\Drupal\Core\Config\TypedConfigManagerInterface'); - $this->config = new Config('config.test', $this->storage, $this->eventDispatcher, $this->typedConfig); $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); - - $container = new ContainerBuilder(); - $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); - \Drupal::setContainer($container); + $this->config = new Config('config.test', $this->storage, $this->eventDispatcher, $this->typedConfig, $this->cacheTagsInvalidator); } /**