Commit 65899f6b authored by alexpott's avatar alexpott

Issue #2195417 by Sutharsan: Clean up configuration import events.

parent 228fdcbc
......@@ -21,9 +21,9 @@ public function initialize() {
// Ensure that the changes have been validated.
$this->validate();
if (!$this->lock->acquire(static::ID)) {
if (!$this->lock->acquire(static::LOCK_ID)) {
// Another process is synchronizing configuration.
throw new ConfigImporterException(sprintf('%s is already importing', static::ID));
throw new ConfigImporterException(sprintf('%s is already importing', static::LOCK_ID));
}
$this->totalToProcess = 0;
foreach(array('create', 'delete', 'update') as $op) {
......@@ -48,9 +48,9 @@ public function processBatch(array &$context) {
$context['finished'] = 1;
}
if ($context['finished'] >= 1) {
$this->notify('import');
$this->eventDispatcher->dispatch(ConfigEvents::IMPORT, new ConfigImporterEvent($this));
// The import is now complete.
$this->lock->release(static::ID);
$this->lock->release(static::LOCK_ID);
$this->reset();
}
}
......
......@@ -47,7 +47,7 @@ final class ConfigEvents {
* @see \Drupal\Core\Config\ConfigImporter::validate().
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber::onConfigImporterValidate().
*/
const VALIDATE = 'config.importer.validate';
const IMPORT_VALIDATE = 'config.importer.validate';
/**
* Name of event fired when when importing configuration to target storage.
......
......@@ -7,6 +7,7 @@
namespace Drupal\Core\Config;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -21,7 +22,7 @@
*
* The ConfigImporter has a identifier which is used to construct event names.
* The events fired during an import are:
* - ConfigEvents::VALIDATE: Events listening can throw a
* - ConfigEvents::IMPORT_VALIDATE: Events listening can throw a
* \Drupal\Core\Config\ConfigImporterException to prevent an import from
* occurring.
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
......@@ -33,9 +34,9 @@
class ConfigImporter extends DependencySerialization {
/**
* The name used to identify events and the lock.
* The name used to identify the lock.
*/
const ID = 'config.importer';
const LOCK_ID = 'config_importer';
/**
* The storage comparer used to discover configuration changes.
......@@ -201,9 +202,9 @@ public function import() {
// Ensure that the changes have been validated.
$this->validate();
if (!$this->lock->acquire(static::ID)) {
if (!$this->lock->acquire(static::LOCK_ID)) {
// Another process is synchronizing configuration.
throw new ConfigImporterException(sprintf('%s is already importing', static::ID));
throw new ConfigImporterException(sprintf('%s is already importing', static::LOCK_ID));
}
// First pass deleted, then new, and lastly changed configuration, in order
// to handle dependencies correctly.
......@@ -215,10 +216,11 @@ public function import() {
}
}
// Allow modules to react to a import.
$this->notify('import');
$this->eventDispatcher->dispatch(ConfigEvents::IMPORT, new ConfigImporterEvent($this));
// The import is now complete.
$this->lock->release(static::ID);
$this->lock->release(static::LOCK_ID);
$this->reset();
}
return $this;
......@@ -235,7 +237,7 @@ public function validate() {
if (!$this->storageComparer->validateSiteUuid()) {
throw new ConfigImporterException('Site UUID in source storage does not match the target storage.');
}
$this->notify('validate');
$this->eventDispatcher->dispatch(ConfigEvents::IMPORT_VALIDATE, new ConfigImporterEvent($this));
$this->validated = TRUE;
}
return $this;
......@@ -322,16 +324,6 @@ protected function importInvokeOwner($op, $name) {
return FALSE;
}
/**
* Dispatches a config importer event.
*
* @param string $event_name
* The name of the config importer event to dispatch.
*/
protected function notify($event_name) {
$this->eventDispatcher->dispatch(static::ID . '.' . $event_name, new ConfigImporterEvent($this));
}
/**
* Determines if a import is already running.
*
......@@ -339,17 +331,7 @@ protected function notify($event_name) {
* TRUE if an import is already running, FALSE if not.
*/
public function alreadyImporting() {
return !$this->lock->lockMayBeAvailable(static::ID);
}
/**
* Returns the identifier for events and locks.
*
* @return string
* The identifier for events and locks.
*/
public function getId() {
return static::ID;
return !$this->lock->lockMayBeAvailable(static::LOCK_ID);
}
}
......@@ -41,7 +41,7 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) {
* An array of event listener definitions.
*/
static function getSubscribedEvents() {
$events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 40);
$events[ConfigEvents::IMPORT_VALIDATE][] = array('onConfigImporterValidate', 40);
return $events;
}
......
......@@ -101,15 +101,15 @@ function testImportLock() {
$this->assertNoText(t('There are no configuration changes.'));
// Acquire a fake-lock on the import mechanism.
$config_importer_lock = $this->configImporter()->getId();
$this->container->get('lock')->acquire($config_importer_lock);
$config_importer = $this->configImporter();
$this->container->get('lock')->acquire($config_importer::LOCK_ID);
// Attempt to import configuration and verify that an error message appears.
$this->drupalPostForm(NULL, array(), t('Import all'));
$this->assertText(t('Another request may be synchronizing configuration already.'));
// Release the lock, just to keep testing sane.
$this->container->get('lock')->release($config_importer_lock);
$this->container->get('lock')->release($config_importer::LOCK_ID);
// Verify site name has not changed.
$this->assertNotEqual($new_site_name, \Drupal::config('system.site')->get('name'));
......
......@@ -22,7 +22,7 @@ class SystemConfigSubscriber implements EventSubscriberInterface {
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 20);
$events[ConfigEvents::IMPORT_VALIDATE][] = array('onConfigImporterValidate', 20);
return $events;
}
......@@ -42,4 +42,3 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) {
}
}
}
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