diff --git a/core/lib/Drupal/Core/Config/ConfigImporter.php b/core/lib/Drupal/Core/Config/ConfigImporter.php index bbb5215..943e68d 100644 --- a/core/lib/Drupal/Core/Config/ConfigImporter.php +++ b/core/lib/Drupal/Core/Config/ConfigImporter.php @@ -761,10 +761,11 @@ protected function processExtension($type, $op, $name) { ->setSyncing(TRUE) ->setSourceStorage($this->storageComparer->getSourceStorage()); if ($type == 'module') { + $service_names = $this->getInstanceServiceNames(); $this->moduleInstaller->$op(array($name), FALSE); // Installing a module can cause a kernel boot therefore reinject all the // services. - $this->reInjectMe(); + $this->restoreInstanceServices($service_names); // During a module install or uninstall the container is rebuilt and the // module handler is called from drupal_get_complete_schema(). This causes // the container's instance of the module handler not to have loaded all @@ -1010,20 +1011,4 @@ public function alreadyImporting() { return !$this->lock->lockMayBeAvailable(static::LOCK_NAME); } - /** - * Gets all the service dependencies from \Drupal. - * - * Since the ConfigImporter handles module installation the kernel and the - * container can be rebuilt and altered during processing. It is necessary to - * keep the services used by the importer in sync. - */ - protected function reInjectMe() { - $resolver = $this->getReverseServiceResolver(); - foreach (get_object_vars($this) as $key => $value) { - if ($service_id = $resolver->lookup($value)) { - $this->$key = \Drupal::service($service_id); - } - } - } - } diff --git a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php index d044c58..5c4f290 100644 --- a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php +++ b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php @@ -32,30 +32,24 @@ * {@inheritdoc} */ public function __sleep() { - $this->_serviceIds = array(); - $vars = get_object_vars($this); - $resolver = $this->getReverseServiceResolver(); - foreach ($vars as $key => $value) { - if ($service_id = $resolver->lookup($value)) { - // If a class member was instantiated by the dependency injection - // container, only store its ID so it can be used to get a fresh - // object on unserialization. - $this->_serviceIds[$key] = $service_id; - unset($vars[$key]); - } - } + $service_names = $this->getInstanceServiceNames(); + + // If a class member was instantiated by the dependency injection container, + // only store its ID so it can be used to get a fresh object on + // unserialization. + $this->_serviceIds = $service_names; - return array_keys($vars); + $all_vars = get_object_vars($this); + $serializable_vars = array_diff_key($all_vars, $service_names); + + return array_keys($serializable_vars); } /** * {@inheritdoc} */ public function __wakeup() { - $container = \Drupal::getContainer(); - foreach ($this->_serviceIds as $key => $service_id) { - $this->$key = $container->get($service_id); - } + $this->restoreInstanceServices($this->_serviceIds); $this->_serviceIds = array(); } @@ -86,4 +80,38 @@ protected function getReverseServiceResolver() { return $this->reverseServiceResolver; } + /** + * Returns the service ids for all instance variables. + * + * @return array + * An associative array where keys are instance variable names and values + * are service ids. + */ + protected function getInstanceServiceNames() { + $service_ids = array(); + $resolver = $this->getReverseServiceResolver(); + + foreach (get_object_vars($this) as $key => $value) { + if ($service_id = $resolver->lookup($value)) { + $service_ids[$key] = $service_id; + } + } + + return $service_ids; + } + + /** + * Restores services given a map of instance variable names to service ids. + * + * @param array $service_names + * An associative array where keys are instance variable names and values + * are service ids as produced by static::getInstanceServiceNames(). + */ + protected function restoreInstanceServices(array $service_names) { + $container = \Drupal::getContainer(); + foreach ($this->_serviceIds as $key => $service_id) { + $this->$key = $container->get($service_id); + } + } + } diff --git a/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php b/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php index 3d26d37..14f3016 100644 --- a/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php +++ b/core/modules/locale/src/Tests/LocaleConfigTranslationTest.php @@ -127,7 +127,10 @@ public function testConfigTranslation() { // Enable the image module. $this->drupalPostForm('admin/modules', array('modules[Field types][image][enable]' => "1"), t('Save configuration')); + + // Rebuild the container and reinject the locale storage service. $this->rebuildContainer(); + $this->storage = $this->container->get('locale.storage'); $string = $this->storage->findString(array('source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration')); $this->assertTrue($string, 'Configuration strings have been created upon installation.');