comparison core/lib/Drupal/Core/DependencyInjection/DependencySerializationTrait.php @ 16:c2387f117808

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