You are here

trait DependencySerializationTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php \Drupal\Core\DependencyInjection\DependencySerializationTrait

Provides dependency injection friendly methods for serialization.

Hierarchy

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

... See full list

File

core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php, line 15
Contains \Drupal\Core\DependencyInjection\DependencySerializationTrait.

Namespace

Drupal\Core\DependencyInjection
View source
trait DependencySerializationTrait {

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

  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    $this->_serviceIds = array();
    $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() {
    $container = \Drupal::getContainer();
    foreach ($this->_serviceIds as $key => $service_id) {
      $this->{$key} = $container
        ->get($service_id);
    }
    $this->_serviceIds = array();
  }

}

Members

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 1
DependencySerializationTrait::__wakeup public function 2