diff --git a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php index 26624a1..92f1441 100644 --- a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php +++ b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php @@ -32,22 +32,12 @@ public function __sleep() { $hashes = $container->getHashes(); foreach ($vars as $key => $value) { if (is_object($value)) { - $service_id = FALSE; - // Special case the container. - if ($value instanceof ContainerInterface) { - $service_id = 'service_container'; - } - else { - $hash = spl_object_hash($value); - if (isset($hashes[$hash])) { - $service_id = $hashes[$hash]; - } - } - if ($service_id) { + $hash = spl_object_hash($value); + if (isset($hashes[$hash])) { // 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; + $this->_serviceIds[$key] = $hashes[$hash]; unset($vars[$key]); } } diff --git a/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php b/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php index 02ec6dd..062e071 100644 --- a/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php +++ b/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php @@ -19,6 +19,7 @@ * {@inheritdoc} */ public function getHashes() { + $this->hashes[spl_object_hash($this)] = 'service_container'; foreach ($this->services as $id => $service) { if (is_object($service)) { $this->hashes[spl_object_hash($service)] = $id; diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php index 3c88a0f..b2771b6 100644 --- a/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php @@ -35,7 +35,11 @@ public function testGetHashes() { $service = new BarClass(); $container->set('bar', $service); // Ensure the right hashes are returned. - $this->assertEquals([spl_object_hash($service) => 'bar'], $container->getHashes()); + $expected = [ + spl_object_hash($container) => 'service_container', + spl_object_hash($service) => 'bar', + ]; + $this->assertEquals($expected, $container->getHashes()); } }