Skip to content
Snippets Groups Projects
Commit 6c70c013 authored by catch's avatar catch
Browse files

Issue #2175917 by Gábor Hojtsy, alexpott, piyuesh23, jibran: Clean up configuration system events.

parent 435de27c
No related branches found
No related tags found
No related merge requests found
Showing
with 359 additions and 36 deletions
...@@ -75,7 +75,7 @@ class Config extends DependencySerialization { ...@@ -75,7 +75,7 @@ class Config extends DependencySerialization {
* *
* @var array * @var array
*/ */
protected $originalData; protected $originalData = array();
/** /**
* The current runtime data. * The current runtime data.
...@@ -458,7 +458,7 @@ public function save() { ...@@ -458,7 +458,7 @@ public function save() {
$this->storage->write($this->name, $this->data); $this->storage->write($this->name, $this->data);
$this->isNew = FALSE; $this->isNew = FALSE;
$this->notify('save'); $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this));
$this->originalData = $this->data; $this->originalData = $this->data;
return $this; return $this;
} }
...@@ -475,7 +475,7 @@ public function delete() { ...@@ -475,7 +475,7 @@ public function delete() {
$this->storage->delete($this->name); $this->storage->delete($this->name);
$this->isNew = TRUE; $this->isNew = TRUE;
$this->resetOverriddenData(); $this->resetOverriddenData();
$this->notify('delete'); $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this));
$this->originalData = $this->data; $this->originalData = $this->data;
return $this; return $this;
} }
...@@ -490,16 +490,6 @@ public function getStorage() { ...@@ -490,16 +490,6 @@ public function getStorage() {
return $this->storage; return $this->storage;
} }
/**
* Dispatches a configuration event.
*
* @param string $config_event_name
* The configuration event name.
*/
protected function notify($config_event_name) {
$this->eventDispatcher->dispatch('config.' . $config_event_name, new ConfigEvent($this));
}
/** /**
* Merges data into a configuration object. * Merges data into a configuration object.
* *
......
<?php <?php
/**
* @file
* Contains \Drupal\Core\Config\ConfigCrudEvent.
*/
namespace Drupal\Core\Config; namespace Drupal\Core\Config;
use Symfony\Component\EventDispatcher\Event; use Symfony\Component\EventDispatcher\Event;
class ConfigEvent extends Event { /**
* Wraps a configuration event for event listeners.
*/
class ConfigCrudEvent extends Event {
/** /**
* Configuration object. * Configuration object.
...@@ -24,10 +32,26 @@ public function __construct(Config $config) { ...@@ -24,10 +32,26 @@ public function __construct(Config $config) {
} }
/** /**
* Get configuration object. * Gets configuration object.
*
* @return \Drupal\Core\Config\Config
* The configuration object that caused the event to fire.
*/ */
public function getConfig() { public function getConfig() {
return $this->config; return $this->config;
} }
/**
* Checks to see if the provided configuration key's value has changed.
*
* @param string $key
* The configuration key to check if it has changed.
*
* @return bool
*/
public function isChanged($key) {
return $this->config->get($key) !== $this->config->getOriginal($key);
}
} }
<?php
/**
* @file
* Contains Drupal\Core\Config\Config\ConfigEvents.
*/
namespace Drupal\Core\Config;
/**
* Defines events for the configuration system.
*/
class ConfigEvents {
/**
* Name of event fired when saving the configuration object.
*
* @see \Drupal\Core\Config\Config::save()
* @see \Drupal\Core\Config\ConfigFactory::onConfigSave()
*/
const SAVE = 'config.save';
/**
* Name of event fired when deleting the configuration object.
*
* @see \Drupal\Core\Config\Config::delete()
*/
const DELETE = 'config.delete';
/**
* Name of event fired when renaming a configuration object.
*
* @see \Drupal\Core\Config\ConfigFactory::rename().
*/
const RENAME = 'config.rename';
/**
* Name of event fired when collecting overrides for configuration objects.
*
* @see \Drupal\Core\Config\ConfigFactory::loadModuleOverrides().
*/
const MODULE_OVERRIDES = 'config.module.overrides';
/**
* Name of event fired when validating in the configuration import process.
*
* @see \Drupal\Core\Config\ConfigImporter::validate().
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber::onConfigImporterValidate().
*/
const VALIDATE = 'config.importer.validate';
/**
* Name of event fired when when importing configuration to target storage.
*
* @see \Drupal\Core\Config\ConfigImporter::import().
* @see \Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber::onConfigImporterImport().
*/
const IMPORT = 'config.importer.import';
}
...@@ -223,7 +223,7 @@ public function loadMultiple(array $names) { ...@@ -223,7 +223,7 @@ public function loadMultiple(array $names) {
*/ */
protected function loadModuleOverrides(array $names) { protected function loadModuleOverrides(array $names) {
$configOverridesEvent = new ConfigModuleOverridesEvent($names, $this->language); $configOverridesEvent = new ConfigModuleOverridesEvent($names, $this->language);
$this->eventDispatcher->dispatch('config.module.overrides', $configOverridesEvent); $this->eventDispatcher->dispatch(ConfigEvents::MODULE_OVERRIDES, $configOverridesEvent);
return $configOverridesEvent->getOverrides(); return $configOverridesEvent->getOverrides();
} }
...@@ -258,12 +258,10 @@ public function rename($old_name, $new_name) { ...@@ -258,12 +258,10 @@ public function rename($old_name, $new_name) {
unset($this->cache[$old_cache_key]); unset($this->cache[$old_cache_key]);
} }
$new_cache_key = $this->getCacheKey($new_name); // Prime the cache and load the configuration with the correct overrides.
$this->cache[$new_cache_key] = new Config($new_name, $this->storage, $this->eventDispatcher, $this->typedConfigManager, $this->language); $config = $this->get($new_name);
if ($data = $this->storage->read($new_name)) { $this->eventDispatcher->dispatch(ConfigEvents::RENAME, new ConfigRenameEvent($config, $old_name));
$this->cache[$new_cache_key]->initWithData($data); return $config;
}
return $this->cache[$new_cache_key];
} }
/** /**
...@@ -370,10 +368,10 @@ protected function canOverride($name) { ...@@ -370,10 +368,10 @@ protected function canOverride($name) {
/** /**
* Removes stale static cache entries when configuration is saved. * Removes stale static cache entries when configuration is saved.
* *
* @param ConfigEvent $event * @param ConfigCrudEvent $event
* The configuration event. * The configuration event.
*/ */
public function onConfigSave(ConfigEvent $event) { public function onConfigSave(ConfigCrudEvent $event) {
// Ensure that the static cache contains up to date configuration objects by // Ensure that the static cache contains up to date configuration objects by
// 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.
...@@ -390,7 +388,7 @@ public function onConfigSave(ConfigEvent $event) { ...@@ -390,7 +388,7 @@ public function onConfigSave(ConfigEvent $event) {
* {@inheritdoc} * {@inheritdoc}
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.save'][] = array('onConfigSave', 255); $events[ConfigEvents::SAVE][] = array('onConfigSave', 255);
return $events; return $events;
} }
......
...@@ -20,11 +20,11 @@ ...@@ -20,11 +20,11 @@
* *
* The ConfigImporter has a identifier which is used to construct event names. * The ConfigImporter has a identifier which is used to construct event names.
* The events fired during an import are: * The events fired during an import are:
* - 'config.importer.validate': Events listening can throw a * - ConfigEvents::VALIDATE: Events listening can throw a
* \Drupal\Core\Config\ConfigImporterException to prevent an import from * \Drupal\Core\Config\ConfigImporterException to prevent an import from
* occurring. * occurring.
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber * @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
* - 'config.importer.import': Events listening can react to a successful import. * - ConfigEvents::IMPORT: Events listening can react to a successful import.
* @see \Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber * @see \Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber
* *
* @see \Drupal\Core\Config\ConfigImporterEvent * @see \Drupal\Core\Config\ConfigImporterEvent
......
<?php
/**
* @file
* Contains \Drupal\Core\Config\ConfigRenameEvent.
*/
namespace Drupal\Core\Config;
/**
* Configuration event fired when renaming a configuration object.
*/
class ConfigRenameEvent extends ConfigCrudEvent {
/**
* The old configuration object name.
*
* @var string
*/
protected $oldName;
/**
* Constructs the config rename event.
*
* @param \Drupal\Core\Config\Config $config
* The configuration that has been renamed.
* @param string $old_name
* The old configuration object name.
*/
public function __construct(Config $config, $old_name) {
$this->config = $config;
$this->oldName = $old_name;
}
/**
* Gets the old configuration object name.
*
* @return string
* The old configuration object name.
*/
public function getOldName() {
return $this->oldName;
}
}
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\Core\EventSubscriber; namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Config\Config; use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigImporterEvent; use Drupal\Core\Config\ConfigImporterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...@@ -40,7 +41,7 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) { ...@@ -40,7 +41,7 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) {
* An array of event listener definitions. * An array of event listener definitions.
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.importer.validate'][] = array('onConfigImporterValidate', 40); $events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 40);
return $events; return $events;
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
namespace Drupal\Core\EventSubscriber; namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Config\Config; use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigManagerInterface; use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\StorageInterface; use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\ConfigImporterEvent; use Drupal\Core\Config\ConfigImporterEvent;
...@@ -70,7 +70,7 @@ public function onConfigImporterImport(ConfigImporterEvent $event) { ...@@ -70,7 +70,7 @@ public function onConfigImporterImport(ConfigImporterEvent $event) {
* An array of event listener definitions. * An array of event listener definitions.
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.importer.import'][] = array('onConfigImporterImport', 40); $events[ConfigEvents::IMPORT][] = array('onConfigImporterImport', 40);
return $events; return $events;
} }
......
<?php
/**
* @file
* Contains \Drupal\config\Tests\ConfigEventsTest.
*/
namespace Drupal\config\Tests;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigEvents;
use Drupal\simpletest\DrupalUnitTestBase;
/**
* Tests CRUD operations on configuration objects.
*/
class ConfigEventsTest extends DrupalUnitTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('config_events_test');
public static function getInfo() {
return array(
'name' => 'Config events',
'description' => 'Tests events fired on configuration objects.',
'group' => 'Configuration',
);
}
/**
* Tests configuration events.
*/
function testConfigEvents() {
$name = 'config_events_test.test';
$config = new Config($name, \Drupal::service('config.storage'), \Drupal::service('event_dispatcher'), \Drupal::service('config.typed'));
$config->set('key', 'initial');
\Drupal::state()->get('config_events_test.event', FALSE);
$this->assertIdentical(\Drupal::state()->get('config_events_test.event', array()), array(), 'No events fired by creating a new configuration object');
$config->save();
$event = \Drupal::state()->get('config_events_test.event', array());
$this->assertIdentical($event['event_name'], ConfigEvents::SAVE);
$this->assertIdentical($event['current_config_data'], array('key' => 'initial'));
$this->assertIdentical($event['raw_config_data'], array('key' => 'initial'));
$this->assertIdentical($event['original_config_data'], array());
$config->set('key', 'updated')->save();
$event = \Drupal::state()->get('config_events_test.event', array());
$this->assertIdentical($event['event_name'], ConfigEvents::SAVE);
$this->assertIdentical($event['current_config_data'], array('key' => 'updated'));
$this->assertIdentical($event['raw_config_data'], array('key' => 'updated'));
$this->assertIdentical($event['original_config_data'], array('key' => 'initial'));
$config->delete();
$event = \Drupal::state()->get('config_events_test.event', array());
$this->assertIdentical($event['event_name'], ConfigEvents::DELETE);
$this->assertIdentical($event['current_config_data'], array());
$this->assertIdentical($event['raw_config_data'], array());
$this->assertIdentical($event['original_config_data'], array('key' => 'updated'));
}
/**
* Tests configuration rename event that is fired from the ConfigFactory.
*/
function testConfigRenameEvent() {
$name = 'config_events_test.test';
$new_name = 'config_events_test.test_rename';
$GLOBALS['config'][$name] = array('key' => 'overridden');
$GLOBALS['config'][$new_name] = array('key' => 'new overridden');
$config = \Drupal::config($name);
$config->set('key', 'initial')->save();
$event = \Drupal::state()->get('config_events_test.event', array());
$this->assertIdentical($event['event_name'], ConfigEvents::SAVE);
$this->assertIdentical($event['current_config_data'], array('key' => 'overridden'));
\Drupal::configFactory()->rename($name, $new_name);
$event = \Drupal::state()->get('config_events_test.event', array());
$this->assertIdentical($event['event_name'], ConfigEvents::RENAME);
$this->assertIdentical($event['current_config_data'], array('key' => 'new overridden'));
$this->assertIdentical($event['raw_config_data'], array('key' => 'initial'));
$this->assertIdentical($event['original_config_data'], array('key' => 'new overridden'));
}
}
name: 'Configuration events test'
type: module
package: Testing
version: VERSION
core: 8.x
hidden: true
<?php
/**
* @file
* Provides Config event listeners for testing purposes.
*/
services:
config_events_test.event_subscriber:
class: Drupal\config_events_test\EventSubscriber
arguments: ['@state']
tags:
- { name: event_subscriber }
<?php
/**
* @file
* Contains \Drupal\config_events_test\EventSubscriber.
*/
namespace Drupal\config_events_test;
use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\KeyValueStore\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EventSubscriber implements EventSubscriberInterface {
/**
* The state key value store.
*
* @var \Drupal\Core\KeyValueStore\StateInterface
*/
protected $state;
/**
* Constructs the Event Subscriber object.
*
* @param \Drupal\Core\KeyValueStore\StateInterface $state
* The state key value store.
*/
public function __construct(StateInterface $state) {
$this->state = $state;
}
/**
* Reacts to config event.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The configuration event.
*/
public function configEventRecorder(ConfigCrudEvent $event) {
$config = $event->getConfig();
$this->state->set('config_events_test.event', array(
'event_name' => $event->getName(),
'current_config_data' => $config->get(),
'original_config_data' => $config->getOriginal(),
'raw_config_data' => $config->getRawData()
));
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[ConfigEvents::SAVE][] = array('configEventRecorder');
$events[ConfigEvents::DELETE][] = array('configEventRecorder');
$events[ConfigEvents::RENAME][] = array('configEventRecorder');
return $events;
}
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\config_override\EventSubscriber; namespace Drupal\config_override\EventSubscriber;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigModuleOverridesEvent; use Drupal\Core\Config\ConfigModuleOverridesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...@@ -36,7 +37,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) { ...@@ -36,7 +37,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) {
* An array of event listener definitions. * An array of event listener definitions.
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.module.overrides'][] = array('onConfigModuleOverride', 35); $events[ConfigEvents::MODULE_OVERRIDES][] = array('onConfigModuleOverride', 35);
return $events; return $events;
} }
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\config_override\EventSubscriber; namespace Drupal\config_override\EventSubscriber;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigModuleOverridesEvent; use Drupal\Core\Config\ConfigModuleOverridesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
...@@ -34,7 +35,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) { ...@@ -34,7 +35,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) {
* An array of event listener definitions. * An array of event listener definitions.
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.module.overrides'][] = array('onConfigModuleOverride', 40); $events[ConfigEvents::MODULE_OVERRIDES][] = array('onConfigModuleOverride', 40);
return $events; return $events;
} }
} }
......
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
namespace Drupal\language\EventSubscriber; namespace Drupal\language\EventSubscriber;
use Drupal\Component\PhpStorage\PhpStorageFactory; use Drupal\Component\PhpStorage\PhpStorageFactory;
use Drupal\Core\Config\ConfigEvent; use Drupal\Core\Config\ConfigCrudEvent;
use Drupal\Core\Config\ConfigEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
...@@ -19,12 +20,12 @@ class ConfigSubscriber implements EventSubscriberInterface { ...@@ -19,12 +20,12 @@ class ConfigSubscriber implements EventSubscriberInterface {
/** /**
* Causes the container to be rebuilt on the next request. * Causes the container to be rebuilt on the next request.
* *
* @param ConfigEvent $event * @param ConfigCrudEvent $event
* The configuration event. * The configuration event.
*/ */
public function onConfigSave(ConfigEvent $event) { public function onConfigSave(ConfigCrudEvent $event) {
$saved_config = $event->getConfig(); $saved_config = $event->getConfig();
if ($saved_config->getName() == 'system.site' && $saved_config->get('langcode') != $saved_config->getOriginal('langcode')) { if ($saved_config->getName() == 'system.site' && $event->isChanged('langcode')) {
// Trigger a container rebuild on the next request by deleting compiled // Trigger a container rebuild on the next request by deleting compiled
// from PHP storage. // from PHP storage.
PhpStorageFactory::get('service_container')->deleteAll(); PhpStorageFactory::get('service_container')->deleteAll();
...@@ -35,7 +36,7 @@ public function onConfigSave(ConfigEvent $event) { ...@@ -35,7 +36,7 @@ public function onConfigSave(ConfigEvent $event) {
* {@inheritdoc} * {@inheritdoc}
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.save'][] = array('onConfigSave', 0); $events[ConfigEvents::SAVE][] = array('onConfigSave', 0);
return $events; return $events;
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\system; namespace Drupal\system;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\ConfigImporterEvent; use Drupal\Core\Config\ConfigImporterEvent;
use Drupal\Core\Config\ConfigImporterException; use Drupal\Core\Config\ConfigImporterException;
use Drupal\Core\Config\StorageDispatcher; use Drupal\Core\Config\StorageDispatcher;
...@@ -21,7 +22,7 @@ class SystemConfigSubscriber implements EventSubscriberInterface { ...@@ -21,7 +22,7 @@ class SystemConfigSubscriber implements EventSubscriberInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events['config.importer.validate'][] = array('onConfigImporterValidate', 20); $events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 20);
return $events; return $events;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment