diff --git a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php index 26624a1..bb38d78 100644 --- a/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php +++ b/core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php @@ -29,27 +29,14 @@ public function __sleep() { try { $container = \Drupal::getContainer(); if ($container instanceof SleepHelperInterface) { - $hashes = $container->getHashes(); + $sleep_helper_storage = $container->getSleepHelperStorage(); 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) { - // 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]); - } + if (is_object($value) && $sleep_helper_storage->contains($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] = $sleep_helper_storage->offsetGet($value); + unset($vars[$key]); } } } diff --git a/core/lib/Drupal/Core/DependencyInjection/SleepHelperInterface.php b/core/lib/Drupal/Core/DependencyInjection/SleepHelperInterface.php index b7fab8c..c93a62e 100644 --- a/core/lib/Drupal/Core/DependencyInjection/SleepHelperInterface.php +++ b/core/lib/Drupal/Core/DependencyInjection/SleepHelperInterface.php @@ -11,11 +11,10 @@ interface SleepHelperInterface { /** - * Get the SPL hashes of the services. + * Get an object storage containing the services. * - * When sleeping an object it becomes possible to investigate whether its - * properties are services. + * @return \SplObjectStorage */ - public function getHashes(); + public function getSleepHelperStorage(); } diff --git a/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php b/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php index 02ec6dd..ad79132 100644 --- a/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php +++ b/core/lib/Drupal/Core/DependencyInjection/SleepHelperTrait.php @@ -13,18 +13,25 @@ */ trait SleepHelperTrait { - protected $hashes = []; + /** + * @var \SplObjectStorage + */ + protected $sleepHelperStorage; /** * {@inheritdoc} */ - public function getHashes() { + public function getSleepHelperStorage() { + if (empty($this->sleepHelperStorage)) { + $this->sleepHelperStorage = new \SplObjectStorage(); + $this->sleepHelperStorage->attach($this, 'service_container'); + } foreach ($this->services as $id => $service) { if (is_object($service)) { - $this->hashes[spl_object_hash($service)] = $id; + $this->sleepHelperStorage->attach($service, $id); } } - return $this->hashes; + return $this->sleepHelperStorage; } /** @@ -32,8 +39,8 @@ public function getHashes() { * * @param array $hashes */ - public function setHashes(array $hashes) { - $this->hashes = $hashes; + public function setSleepHelperStorage(\SplObjectStorage $storage) { + $this->sleepHelperStorage = $storage; } } diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index e673618..aa1de8c 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -756,7 +756,7 @@ protected function initializeContainer() { $current_user_id = $this->container->get('current_user')->id(); } // Save the current hashes. - $hashes = $this->container->getHashes(); + $sleep_helper_storage = $this->container->getSleepHelperStorage(); // If there is a session, close and save it. if ($this->container->initialized('session')) { @@ -808,8 +808,8 @@ protected function initializeContainer() { } } - if (!empty($hashes)) { - $container->setHashes($hashes); + if (!empty($sleep_helper_storage )) { + $container->setSleepHelperStorage($sleep_helper_storage); } if (!empty($current_user_id)) { $this->container->get('current_user')->setInitialAccountId($current_user_id); diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php index 3c88a0f..58a1492 100644 --- a/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/ContainerTest.php @@ -28,14 +28,13 @@ public function testSerialize() { } /** - * @covers ::getHashes + * @covers ::getSleepHelperStorage */ public function testGetHashes() { $container = new Container(); $service = new BarClass(); $container->set('bar', $service); - // Ensure the right hashes are returned. - $this->assertEquals([spl_object_hash($service) => 'bar'], $container->getHashes()); + $this->assertEquals('bar', $container->getSleepHelperStorage()->offsetGet($service)); } }