annotate core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents c2387f117808
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\DependencyInjection;
Chris@0 4
Chris@16 5 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 6 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Provides dependency injection friendly methods for serialization.
Chris@0 10 */
Chris@0 11 trait DependencySerializationTrait {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * An array of service IDs keyed by property name used for serialization.
Chris@0 15 *
Chris@0 16 * @var array
Chris@0 17 */
Chris@0 18 protected $_serviceIds = [];
Chris@0 19
Chris@0 20 /**
Chris@16 21 * An array of entity type IDs keyed by the property name of their storages.
Chris@16 22 *
Chris@16 23 * @var array
Chris@16 24 */
Chris@16 25 protected $_entityStorages = [];
Chris@16 26
Chris@16 27 /**
Chris@0 28 * {@inheritdoc}
Chris@0 29 */
Chris@0 30 public function __sleep() {
Chris@0 31 $this->_serviceIds = [];
Chris@0 32 $vars = get_object_vars($this);
Chris@0 33 foreach ($vars as $key => $value) {
Chris@0 34 if (is_object($value) && isset($value->_serviceId)) {
Chris@0 35 // If a class member was instantiated by the dependency injection
Chris@0 36 // container, only store its ID so it can be used to get a fresh object
Chris@0 37 // on unserialization.
Chris@0 38 $this->_serviceIds[$key] = $value->_serviceId;
Chris@0 39 unset($vars[$key]);
Chris@0 40 }
Chris@0 41 // Special case the container, which might not have a service ID.
Chris@0 42 elseif ($value instanceof ContainerInterface) {
Chris@0 43 $this->_serviceIds[$key] = 'service_container';
Chris@0 44 unset($vars[$key]);
Chris@0 45 }
Chris@16 46 elseif ($value instanceof EntityStorageInterface) {
Chris@16 47 // If a class member is an entity storage, only store the entity type ID
Chris@16 48 // the storage is for so it can be used to get a fresh object on
Chris@16 49 // unserialization. By doing this we prevent possible memory leaks when
Chris@16 50 // the storage is serialized when it contains a static cache of entity
Chris@16 51 // objects and additionally we ensure that we'll not have multiple
Chris@16 52 // storage objects for the same entity type and therefore prevent
Chris@16 53 // returning different references for the same entity.
Chris@16 54 $this->_entityStorages[$key] = $value->getEntityTypeId();
Chris@16 55 unset($vars[$key]);
Chris@16 56 }
Chris@0 57 }
Chris@0 58
Chris@0 59 return array_keys($vars);
Chris@0 60 }
Chris@0 61
Chris@0 62 /**
Chris@0 63 * {@inheritdoc}
Chris@0 64 */
Chris@0 65 public function __wakeup() {
Chris@0 66 // Tests in isolation potentially unserialize in the parent process.
Chris@0 67 $phpunit_bootstrap = isset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
Chris@0 68 if ($phpunit_bootstrap && !\Drupal::hasContainer()) {
Chris@0 69 return;
Chris@0 70 }
Chris@0 71 $container = \Drupal::getContainer();
Chris@0 72 foreach ($this->_serviceIds as $key => $service_id) {
Chris@0 73 // In rare cases, when test data is serialized in the parent process,
Chris@0 74 // there is a service container but it doesn't contain all expected
Chris@0 75 // services. To avoid fatal errors during the wrap-up of failing tests, we
Chris@0 76 // check for this case, too.
Chris@0 77 if ($phpunit_bootstrap && !$container->has($service_id)) {
Chris@0 78 continue;
Chris@0 79 }
Chris@0 80 $this->$key = $container->get($service_id);
Chris@0 81 }
Chris@0 82 $this->_serviceIds = [];
Chris@16 83
Chris@16 84 // In rare cases, when test data is serialized in the parent process, there
Chris@16 85 // is a service container but it doesn't contain all expected services. To
Chris@16 86 // avoid fatal errors during the wrap-up of failing tests, we check for this
Chris@16 87 // case, too.
Chris@16 88 if ($this->_entityStorages && (!$phpunit_bootstrap || $container->has('entity_type.manager'))) {
Chris@16 89 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
Chris@16 90 $entity_type_manager = $container->get('entity_type.manager');
Chris@16 91 foreach ($this->_entityStorages as $key => $entity_type_id) {
Chris@16 92 $this->$key = $entity_type_manager->getStorage($entity_type_id);
Chris@16 93 }
Chris@16 94 }
Chris@16 95 $this->_entityStorages = [];
Chris@0 96 }
Chris@0 97
Chris@0 98 }