function DependencySerializationTrait::__sleep

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php \Drupal\Core\DependencyInjection\DependencySerializationTrait::__sleep()
  2. 8.9.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php \Drupal\Core\DependencyInjection\DependencySerializationTrait::__sleep()
  3. 10 core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php \Drupal\Core\DependencyInjection\DependencySerializationTrait::__sleep()
6 calls to DependencySerializationTrait::__sleep()
ConfigImporter::reInjectMe in core/lib/Drupal/Core/Config/ConfigImporter.php
Gets all the service dependencies from \Drupal.
DrupalDateTime::__sleep in core/lib/Drupal/Core/Datetime/DrupalDateTime.php
EntityBase::__sleep in core/lib/Drupal/Core/Entity/EntityBase.php
ForumManager::__sleep in core/modules/forum/src/ForumManager.php
StorageComparer::__sleep in core/lib/Drupal/Core/Config/StorageComparer.php

... See full list

1 method overrides DependencySerializationTrait::__sleep()
TermStorage::__sleep in core/modules/taxonomy/src/TermStorage.php

File

core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php, line 33

Class

DependencySerializationTrait
Provides dependency injection friendly methods for serialization.

Namespace

Drupal\Core\DependencyInjection

Code

public function __sleep() : array {
    $vars = get_object_vars($this);
    try {
        $container = \Drupal::getContainer();
        $reverse_container = $container->get(ReverseContainer::class);
        foreach ($vars as $key => $value) {
            if (!is_object($value) || $value instanceof TranslatableMarkup) {
                // Ignore properties that cannot be services.
                continue;
            }
            if ($value instanceof EntityStorageInterface) {
                // If a class member is an entity storage, only store the entity type
                // ID the storage is for, so it can be used to get a fresh object on
                // unserialization. By doing this we prevent possible memory leaks
                // when the storage is serialized and it contains a static cache of
                // entity objects. Additionally we ensure that we'll not have multiple
                // storage objects for the same entity type and therefore prevent
                // returning different references for the same entity.
                $this->_entityStorages[$key] = $value->getEntityTypeId();
                unset($vars[$key]);
            }
            elseif ($service_id = $reverse_container->getId($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]);
            }
        }
    } catch (ContainerNotInitializedException $e) {
        // No container, no problem.
    }
    return array_keys($vars);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.