1. 8.5.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait
  2. 8.0.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait
  3. 8.1.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait
  4. 8.2.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait
  5. 8.3.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait
  6. 8.4.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait
  7. 8.6.x core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php DependencySerializationTrait

Provides dependency injection friendly methods for serialization.

Hierarchy

38 files declare their use of DependencySerializationTrait
CachedStorage.php in core/lib/Drupal/Core/Config/CachedStorage.php
ConfigBase.php in core/lib/Drupal/Core/Config/ConfigBase.php
ConfigImporter.php in core/lib/Drupal/Core/Config/ConfigImporter.php
ContentTranslationHandler.php in core/modules/content_translation/src/ContentTranslationHandler.php
ContextAwarePluginBase.php in core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php

... See full list

File

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

Namespace

Drupal\Core\DependencyInjection
View source
trait DependencySerializationTrait {

  /**
   * An array of service IDs keyed by property name used for serialization.
   *
   * @var array
   */
  protected $_serviceIds = [];

  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    $this->_serviceIds = [];
    $vars = get_object_vars($this);
    foreach ($vars as $key => $value) {
      if (is_object($value) && isset($value->_serviceId)) {

        // 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] = $value->_serviceId;
        unset($vars[$key]);
      }
      elseif ($value instanceof ContainerInterface) {
        $this->_serviceIds[$key] = 'service_container';
        unset($vars[$key]);
      }
    }
    return array_keys($vars);
  }

  /**
   * {@inheritdoc}
   */
  public function __wakeup() {

    // Tests in isolation potentially unserialize in the parent process.
    if (isset($GLOBALS['__PHPUNIT_BOOTSTRAP']) && !\Drupal::hasContainer()) {
      return;
    }
    $container = \Drupal::getContainer();
    foreach ($this->_serviceIds as $key => $service_id) {
      $this->{$key} = $container
        ->get($service_id);
    }
    $this->_serviceIds = [];
  }

}

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function Prevents serializing any properties.
DependencySerializationTrait::__wakeup public function