From 65899f6bddf30817f93d324e0f407be188a38839 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 24 Feb 2014 12:00:34 +0000
Subject: [PATCH] Issue #2195417 by Sutharsan: Clean up configuration import
 events.

---
 .../Core/Config/BatchConfigImporter.php       |  8 ++--
 core/lib/Drupal/Core/Config/ConfigEvents.php  |  2 +-
 .../lib/Drupal/Core/Config/ConfigImporter.php | 40 +++++--------------
 .../ConfigImportSubscriber.php                |  2 +-
 .../config/Tests/ConfigImportUITest.php       |  6 +--
 .../Drupal/system/SystemConfigSubscriber.php  |  3 +-
 6 files changed, 21 insertions(+), 40 deletions(-)

diff --git a/core/lib/Drupal/Core/Config/BatchConfigImporter.php b/core/lib/Drupal/Core/Config/BatchConfigImporter.php
index b5cf1dca8a4f..0ab6cc153446 100644
--- a/core/lib/Drupal/Core/Config/BatchConfigImporter.php
+++ b/core/lib/Drupal/Core/Config/BatchConfigImporter.php
@@ -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();
     }
   }
diff --git a/core/lib/Drupal/Core/Config/ConfigEvents.php b/core/lib/Drupal/Core/Config/ConfigEvents.php
index 839b189f2571..0b0bb3ac4863 100644
--- a/core/lib/Drupal/Core/Config/ConfigEvents.php
+++ b/core/lib/Drupal/Core/Config/ConfigEvents.php
@@ -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.
diff --git a/core/lib/Drupal/Core/Config/ConfigImporter.php b/core/lib/Drupal/Core/Config/ConfigImporter.php
index b91964e3dcbc..5cc1a95a4575 100644
--- a/core/lib/Drupal/Core/Config/ConfigImporter.php
+++ b/core/lib/Drupal/Core/Config/ConfigImporter.php
@@ -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);
   }
 
 }
diff --git a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php
index 16267f406e11..75f06d4d675b 100644
--- a/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php
@@ -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;
   }
 
diff --git a/core/modules/config/lib/Drupal/config/Tests/ConfigImportUITest.php b/core/modules/config/lib/Drupal/config/Tests/ConfigImportUITest.php
index e49560258262..24be35e0eb1d 100644
--- a/core/modules/config/lib/Drupal/config/Tests/ConfigImportUITest.php
+++ b/core/modules/config/lib/Drupal/config/Tests/ConfigImportUITest.php
@@ -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'));
diff --git a/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php b/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php
index 4abc7144c420..39f7b525fd8b 100644
--- a/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php
+++ b/core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php
@@ -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) {
     }
   }
 }
-
-- 
GitLab