Unverified Commit 3f4d4031 authored by alexpott's avatar alexpott
Browse files

Issue #3083065 by Krzysztof Domański, bircher, wouter.adem, alexpott: Remove...

Issue #3083065 by Krzysztof Domański, bircher, wouter.adem, alexpott: Remove EventSubscriberInterface from ExportStorageManager and remove StorageRebuildNeededEvent
parent a19a3806
......@@ -9,7 +9,6 @@
// See the experimental modules policy https://www.drupal.org/core/experimental
// @todo: remove class aliases in #2991683
@class_alias('Drupal\config_environment\Core\Config\StorageTransformEvent', 'Drupal\Core\Config\StorageTransformEvent');
@class_alias('Drupal\config_environment\Core\Config\StorageRebuildNeededEvent', 'Drupal\Core\Config\StorageRebuildNeededEvent');
@class_alias('Drupal\config_environment\Core\Config\ManagedStorage', 'Drupal\Core\Config\ManagedStorage');
@class_alias('Drupal\config_environment\Core\Config\StorageManagerInterface', 'Drupal\Core\Config\StorageManagerInterface');
@class_alias('Drupal\config_environment\Core\Config\ExportStorageManager', 'Drupal\Core\Config\ExportStorageManager');
......
......@@ -8,12 +8,10 @@ services:
arguments: ['@config.storage.export.manager']
config.storage.export.manager:
class: Drupal\config_environment\Core\Config\ExportStorageManager
arguments: ['@config.storage', '@state', '@database', '@event_dispatcher']
tags:
- { name: event_subscriber }
arguments: ['@config.storage', '@database', '@event_dispatcher']
# config_environment services.
config_environment.excluded_modules.event_subscriber:
class: Drupal\config_environment\EventSubscriber\ExcludedModulesEventSubscriber
arguments: ['@config.storage', '@settings', '@config.manager', '@state']
arguments: ['@config.storage', '@settings', '@config.manager']
tags:
- { name: event_subscriber }
......@@ -71,38 +71,10 @@ final class ConfigEvents {
*
* @see \Drupal\Core\Config\StorageTransformEvent
* @see \Drupal\Core\Config\ConfigEvents::STORAGE_TRANSFORM_IMPORT
* @see \Drupal\Core\Config\ConfigEvents::STORAGE_EXPORT_REBUILD
* @see \Drupal\config_environment\Core\Config\ExportStorageManager::getStorage
*
* @var string
*/
const STORAGE_TRANSFORM_EXPORT = 'config.transform.export';
/**
* Name of the event fired when the export storage may need to be rebuilt.
*
* This event allows subscribers to indicate that the export storage should be
* rebuilt. The event listener method receives a
* \Drupal\Core\Config\StorageRebuildNeededEvent instance.
* When this event is set to be needing a rebuild by a subscriber then the
* \Drupal\Core\Config\ConfigEvents::STORAGE_TRANSFORM_EXPORT event will be
* dispatched.
*
* @code
* if ($exportStorageIsOutOfDateConditionIsMet) {
* $event->setRebuildNeeded();
* }
* // else, do nothing.
* @endcode
*
* @Event
*
* @see \Drupal\Core\Config\StorageRebuildNeededEvent
* @see \Drupal\Core\Config\ConfigEvents::STORAGE_TRANSFORM_EXPORT
* @see \Drupal\config_environment\Core\Config\ExportStorageManager::getStorage
*
* @var string
*/
const STORAGE_EXPORT_REBUILD = 'config.export.rebuild';
}
......@@ -6,30 +6,22 @@
// @codingStandardsIgnoreEnd
namespace Drupal\config_environment\Core\Config;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\DatabaseStorage;
use Drupal\Core\Config\ReadOnlyStorage;
use Drupal\Core\Config\StorageCopyTrait;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* The export storage manager dispatches an event for the export storage.
*
* @internal
*/
class ExportStorageManager implements StorageManagerInterface, EventSubscriberInterface {
class ExportStorageManager implements StorageManagerInterface {
use StorageCopyTrait;
/**
* The state key indicating that the export storage needs to be rebuilt.
*/
const NEEDS_REBUILD_KEY = 'config_export_needs_rebuild';
/**
* The active configuration storage.
*
......@@ -37,13 +29,6 @@ class ExportStorageManager implements StorageManagerInterface, EventSubscriberIn
*/
protected $active;
/**
* The drupal state.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* The database storage.
*
......@@ -63,16 +48,13 @@ class ExportStorageManager implements StorageManagerInterface, EventSubscriberIn
*
* @param \Drupal\Core\Config\StorageInterface $active
* The active config storage to prime the export storage.
* @param \Drupal\Core\State\StateInterface $state
* The drupal state.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
*/
public function __construct(StorageInterface $active, StateInterface $state, Connection $connection, EventDispatcherInterface $event_dispatcher) {
public function __construct(StorageInterface $active, Connection $connection, EventDispatcherInterface $event_dispatcher) {
$this->active = $active;
$this->state = $state;
$this->eventDispatcher = $event_dispatcher;
// The point of this service is to provide the storage and dispatch the
// event when needed, so the storage itself can not be a service.
......@@ -83,38 +65,11 @@ public function __construct(StorageInterface $active, StateInterface $state, Con
* {@inheritdoc}
*/
public function getStorage() {
$rebuild = $this->state->get(self::NEEDS_REBUILD_KEY, TRUE);
if (!$rebuild) {
// @todo: Use ConfigEvents::STORAGE_EXPORT_REBUILD in #2991683
$rebuild = $this->eventDispatcher->dispatch('config.export.rebuild', new StorageRebuildNeededEvent())->isRebuildNeeded();
}
if ($rebuild) {
self::replaceStorageContents($this->active, $this->storage);
// @todo: Use ConfigEvents::STORAGE_TRANSFORM_EXPORT in #2991683
$this->eventDispatcher->dispatch('config.transform.export', new StorageTransformEvent($this->storage));
$this->state->set(self::NEEDS_REBUILD_KEY, FALSE);
}
self::replaceStorageContents($this->active, $this->storage);
// @todo: Use ConfigEvents::STORAGE_TRANSFORM_EXPORT in #2991683
$this->eventDispatcher->dispatch('config.transform.export', new StorageTransformEvent($this->storage));
return new ReadOnlyStorage($this->storage);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = ['onConfigChange', 0];
$events[ConfigEvents::DELETE][] = ['onConfigChange', 0];
$events[ConfigEvents::RENAME][] = ['onConfigChange', 0];
return $events;
}
/**
* Set the flag in state that the export storage is out of date.
*/
public function onConfigChange() {
if (!$this->state->get(self::NEEDS_REBUILD_KEY, FALSE)) {
$this->state->set(self::NEEDS_REBUILD_KEY, TRUE);
}
}
}
<?php
namespace Drupal\config_environment\Core\Config;
use Symfony\Component\EventDispatcher\Event;
/**
* The dispatched by a storage manager to check if a rebuild is needed.
*/
class StorageRebuildNeededEvent extends Event {
/**
* The flag which keeps track of whether the storage needs to be rebuilt.
*
* @var bool
*/
private $rebuildNeeded = FALSE;
/**
* Flags to the config storage manager that a rebuild is needed.
*/
public function setRebuildNeeded() {
$this->rebuildNeeded = TRUE;
$this->stopPropagation();
}
/**
* Returns whether the storage needs to be rebuilt or not.
*
* @return bool
* Whether the rebuild is needed or not.
*/
public function isRebuildNeeded() {
return $this->rebuildNeeded;
}
}
......@@ -4,10 +4,8 @@
use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageRebuildNeededEvent;
use Drupal\Core\Config\StorageTransformEvent;
use Drupal\Core\Site\Settings;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
......@@ -37,11 +35,6 @@ final class ExcludedModulesEventSubscriber implements EventSubscriberInterface {
*/
private $manager;
/**
* @var \Drupal\Core\State\StateInterface
*/
private $state;
/**
* EnvironmentModulesEventSubscriber constructor.
*
......@@ -51,14 +44,11 @@ final class ExcludedModulesEventSubscriber implements EventSubscriberInterface {
* The Drupal settings.
* @param \Drupal\Core\Config\ConfigManagerInterface $manager
* The config manager.
* @param \Drupal\Core\State\StateInterface $state
* The Drupal state.
*/
public function __construct(StorageInterface $active_storage, Settings $settings, ConfigManagerInterface $manager, StateInterface $state) {
public function __construct(StorageInterface $active_storage, Settings $settings, ConfigManagerInterface $manager) {
$this->activeStorage = $active_storage;
$this->settings = $settings;
$this->manager = $manager;
$this->state = $state;
}
/**
......@@ -69,23 +59,9 @@ public static function getSubscribedEvents() {
return [
'config.transform.import' => ['onConfigTransformImport', -500],
'config.transform.export' => ['onConfigTransformExport', 500],
'config.export.rebuild' => ['onExportStorageNeedsRebuild', 0],
];
}
/**
* Mark the export storage as out of date when the settings changed.
*
* @param \Drupal\Core\Config\StorageRebuildNeededEvent $event
* The event to control the storage rebuild.
*/
public function onExportStorageNeedsRebuild(StorageRebuildNeededEvent $event) {
// If the excluded modules are not the same as last time, re-transform.
if ($this->state->get(self::EXCLUDED_MODULES_KEY) != $this->getExcludedModules()) {
$event->setRebuildNeeded();
}
}
/**
* Transform the storage which is used to import the configuration.
*
......@@ -141,9 +117,6 @@ public function onConfigTransformImport(StorageTransformEvent $event) {
* The transformation event.
*/
public function onConfigTransformExport(StorageTransformEvent $event) {
// Save which modules are excluded in state to know if it has changed.
$this->state->set(self::EXCLUDED_MODULES_KEY, $this->getExcludedModules());
$storage = $event->getStorage();
if (!$storage->exists('core.extension')) {
// If the core.extension config is not present there is nothing to do.
......
......@@ -3,7 +3,6 @@
namespace Drupal\config_transformer_test;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageRebuildNeededEvent;
use Drupal\Core\Config\StorageTransformEvent;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -102,18 +101,6 @@ public function onExportTransform(StorageTransformEvent $event) {
}
}
/**
* React to the rebuilding the config export storage.
*
* @param \Drupal\Core\Config\StorageRebuildNeededEvent $event
* The event we may stop.
*/
public function onExportRebuild(StorageRebuildNeededEvent $event) {
if ($this->state->get('config_transform_test_rebuild', FALSE)) {
$event->setRebuildNeeded();
}
}
/**
* {@inheritdoc}
*/
......@@ -121,7 +108,6 @@ public static function getSubscribedEvents() {
// @todo: use class constants when they get added in #2991683
$events['config.transform.import'][] = ['onImportTransform'];
$events['config.transform.export'][] = ['onExportTransform'];
$events['config.export.rebuild'][] = ['onExportRebuild'];
return $events;
}
......
......@@ -45,7 +45,7 @@ public function testGetStorage() {
$this->assertEquals($rawConfig['name'], $exported['name']);
$this->assertEquals($rawConfig['slogan'] . ' Arrr', $exported['slogan']);
// Save the config to trigger the rebuild.
// Save the config to active storage so that the transformer can alter it.
$this->config('system.site')
->set('name', 'New name')
->set('slogan', 'New slogan')
......@@ -58,15 +58,9 @@ public function testGetStorage() {
$this->assertEquals('New name', $exported['name']);
$this->assertEquals($rawConfig['slogan'] . ' Arrr', $exported['slogan']);
// Change the state which will not trigger a rebuild.
// Change what the transformer does without changing anything else to assert
// that the event is dispatched every time the storage is needed.
$this->container->get('state')->set('config_transform_test_mail', 'config@drupal.example');
$storage = $this->container->get('config.storage.export.manager')->getStorage();
$exported = $storage->read('system.site');
// The mail is still set to the empty value from last time.
$this->assertEquals('', $exported['mail']);
$this->container->get('state')->set('config_transform_test_rebuild', TRUE);
$storage = $this->container->get('config.storage.export.manager')->getStorage();
$exported = $storage->read('system.site');
// The mail is still set to the value from the beginning.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment