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

8.0.x
Alex Pott 2014-02-24 12:00:34 +00:00
parent 228fdcbcc0
commit 65899f6bdd
6 changed files with 21 additions and 40 deletions

View File

@ -21,9 +21,9 @@ class BatchConfigImporter extends ConfigImporter {
// Ensure that the changes have been validated. // Ensure that the changes have been validated.
$this->validate(); $this->validate();
if (!$this->lock->acquire(static::ID)) { if (!$this->lock->acquire(static::LOCK_ID)) {
// Another process is synchronizing configuration. // 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; $this->totalToProcess = 0;
foreach(array('create', 'delete', 'update') as $op) { foreach(array('create', 'delete', 'update') as $op) {
@ -48,9 +48,9 @@ class BatchConfigImporter extends ConfigImporter {
$context['finished'] = 1; $context['finished'] = 1;
} }
if ($context['finished'] >= 1) { if ($context['finished'] >= 1) {
$this->notify('import'); $this->eventDispatcher->dispatch(ConfigEvents::IMPORT, new ConfigImporterEvent($this));
// The import is now complete. // The import is now complete.
$this->lock->release(static::ID); $this->lock->release(static::LOCK_ID);
$this->reset(); $this->reset();
} }
} }

View File

@ -47,7 +47,7 @@ final class ConfigEvents {
* @see \Drupal\Core\Config\ConfigImporter::validate(). * @see \Drupal\Core\Config\ConfigImporter::validate().
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber::onConfigImporterValidate(). * @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. * Name of event fired when when importing configuration to target storage.

View File

@ -7,6 +7,7 @@
namespace Drupal\Core\Config; namespace Drupal\Core\Config;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\DependencyInjection\DependencySerialization; use Drupal\Core\DependencyInjection\DependencySerialization;
use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -21,7 +22,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
* *
* 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:
* - ConfigEvents::VALIDATE: Events listening can throw a * - ConfigEvents::IMPORT_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
@ -33,9 +34,9 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ConfigImporter extends DependencySerialization { 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. * The storage comparer used to discover configuration changes.
@ -201,9 +202,9 @@ class ConfigImporter extends DependencySerialization {
// Ensure that the changes have been validated. // Ensure that the changes have been validated.
$this->validate(); $this->validate();
if (!$this->lock->acquire(static::ID)) { if (!$this->lock->acquire(static::LOCK_ID)) {
// Another process is synchronizing configuration. // 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 // First pass deleted, then new, and lastly changed configuration, in order
// to handle dependencies correctly. // to handle dependencies correctly.
@ -215,10 +216,11 @@ class ConfigImporter extends DependencySerialization {
} }
} }
// Allow modules to react to a import. // Allow modules to react to a import.
$this->notify('import'); $this->eventDispatcher->dispatch(ConfigEvents::IMPORT, new ConfigImporterEvent($this));
// The import is now complete. // The import is now complete.
$this->lock->release(static::ID); $this->lock->release(static::LOCK_ID);
$this->reset(); $this->reset();
} }
return $this; return $this;
@ -235,7 +237,7 @@ class ConfigImporter extends DependencySerialization {
if (!$this->storageComparer->validateSiteUuid()) { if (!$this->storageComparer->validateSiteUuid()) {
throw new ConfigImporterException('Site UUID in source storage does not match the target storage.'); 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; $this->validated = TRUE;
} }
return $this; return $this;
@ -322,16 +324,6 @@ class ConfigImporter extends DependencySerialization {
return FALSE; 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. * Determines if a import is already running.
* *
@ -339,17 +331,7 @@ class ConfigImporter extends DependencySerialization {
* TRUE if an import is already running, FALSE if not. * TRUE if an import is already running, FALSE if not.
*/ */
public function alreadyImporting() { public function alreadyImporting() {
return !$this->lock->lockMayBeAvailable(static::ID); return !$this->lock->lockMayBeAvailable(static::LOCK_ID);
}
/**
* Returns the identifier for events and locks.
*
* @return string
* The identifier for events and locks.
*/
public function getId() {
return static::ID;
} }
} }

View File

@ -41,7 +41,7 @@ class ConfigImportSubscriber implements EventSubscriberInterface {
* An array of event listener definitions. * An array of event listener definitions.
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 40); $events[ConfigEvents::IMPORT_VALIDATE][] = array('onConfigImporterValidate', 40);
return $events; return $events;
} }

View File

@ -101,15 +101,15 @@ class ConfigImportUITest extends WebTestBase {
$this->assertNoText(t('There are no configuration changes.')); $this->assertNoText(t('There are no configuration changes.'));
// Acquire a fake-lock on the import mechanism. // Acquire a fake-lock on the import mechanism.
$config_importer_lock = $this->configImporter()->getId(); $config_importer = $this->configImporter();
$this->container->get('lock')->acquire($config_importer_lock); $this->container->get('lock')->acquire($config_importer::LOCK_ID);
// Attempt to import configuration and verify that an error message appears. // Attempt to import configuration and verify that an error message appears.
$this->drupalPostForm(NULL, array(), t('Import all')); $this->drupalPostForm(NULL, array(), t('Import all'));
$this->assertText(t('Another request may be synchronizing configuration already.')); $this->assertText(t('Another request may be synchronizing configuration already.'));
// Release the lock, just to keep testing sane. // 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. // Verify site name has not changed.
$this->assertNotEqual($new_site_name, \Drupal::config('system.site')->get('name')); $this->assertNotEqual($new_site_name, \Drupal::config('system.site')->get('name'));

View File

@ -22,7 +22,7 @@ class SystemConfigSubscriber implements EventSubscriberInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
static function getSubscribedEvents() { static function getSubscribedEvents() {
$events[ConfigEvents::VALIDATE][] = array('onConfigImporterValidate', 20); $events[ConfigEvents::IMPORT_VALIDATE][] = array('onConfigImporterValidate', 20);
return $events; return $events;
} }
@ -42,4 +42,3 @@ class SystemConfigSubscriber implements EventSubscriberInterface {
} }
} }
} }