Mercurial > hg > cmmr2012-drupal-site
comparison core/lib/Drupal/Core/Config/ConfigManager.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | c75dbcec494b |
children |
comparison
equal
deleted
inserted
replaced
4:a9cd425dd02b | 5:12f9dff5fda9 |
---|---|
4 | 4 |
5 use Drupal\Component\Diff\Diff; | 5 use Drupal\Component\Diff\Diff; |
6 use Drupal\Core\Config\Entity\ConfigDependencyManager; | 6 use Drupal\Core\Config\Entity\ConfigDependencyManager; |
7 use Drupal\Core\Config\Entity\ConfigEntityInterface; | 7 use Drupal\Core\Config\Entity\ConfigEntityInterface; |
8 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface; | 8 use Drupal\Core\Config\Entity\ConfigEntityTypeInterface; |
9 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; | |
9 use Drupal\Core\Entity\EntityManagerInterface; | 10 use Drupal\Core\Entity\EntityManagerInterface; |
11 use Drupal\Core\Entity\EntityRepositoryInterface; | |
10 use Drupal\Core\Entity\EntityTypeInterface; | 12 use Drupal\Core\Entity\EntityTypeInterface; |
13 use Drupal\Core\Entity\EntityTypeManagerInterface; | |
11 use Drupal\Core\Serialization\Yaml; | 14 use Drupal\Core\Serialization\Yaml; |
12 use Drupal\Core\StringTranslation\StringTranslationTrait; | 15 use Drupal\Core\StringTranslation\StringTranslationTrait; |
13 use Drupal\Core\StringTranslation\TranslationInterface; | 16 use Drupal\Core\StringTranslation\TranslationInterface; |
14 use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 17 use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15 | 18 |
16 /** | 19 /** |
17 * The ConfigManager provides helper functions for the configuration system. | 20 * The ConfigManager provides helper functions for the configuration system. |
18 */ | 21 */ |
19 class ConfigManager implements ConfigManagerInterface { | 22 class ConfigManager implements ConfigManagerInterface { |
20 use StringTranslationTrait; | 23 use StringTranslationTrait; |
21 | 24 use DeprecatedServicePropertyTrait; |
22 /** | 25 use StorageCopyTrait; |
23 * The entity manager. | 26 |
24 * | 27 /** |
25 * @var \Drupal\Core\Entity\EntityManagerInterface | 28 * {@inheritdoc} |
26 */ | 29 */ |
27 protected $entityManager; | 30 protected $deprecatedProperties = ['entityManager' => 'entity.manager']; |
31 | |
32 /** | |
33 * The entity type manager. | |
34 * | |
35 * @var \Drupal\Core\Entity\EntityTypeManagerInterface | |
36 */ | |
37 protected $entityTypeManager; | |
38 | |
39 /** | |
40 * The entity repository. | |
41 * | |
42 * @var \Drupal\Core\Entity\EntityRepositoryInterface | |
43 */ | |
44 protected $entityRepository; | |
28 | 45 |
29 /** | 46 /** |
30 * The configuration factory. | 47 * The configuration factory. |
31 * | 48 * |
32 * @var \Drupal\Core\Config\ConfigFactoryInterface | 49 * @var \Drupal\Core\Config\ConfigFactoryInterface |
69 protected $storages; | 86 protected $storages; |
70 | 87 |
71 /** | 88 /** |
72 * Creates ConfigManager objects. | 89 * Creates ConfigManager objects. |
73 * | 90 * |
74 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager | 91 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
75 * The entity manager. | 92 * The entity type manager. |
76 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory | 93 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
77 * The configuration factory. | 94 * The configuration factory. |
78 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager | 95 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config_manager |
79 * The typed config manager. | 96 * The typed config manager. |
80 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation | 97 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation |
81 * The string translation service. | 98 * The string translation service. |
82 * @param \Drupal\Core\Config\StorageInterface $active_storage | 99 * @param \Drupal\Core\Config\StorageInterface $active_storage |
83 * The active configuration storage. | 100 * The active configuration storage. |
84 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher | 101 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher |
85 * The event dispatcher. | 102 * The event dispatcher. |
86 */ | 103 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository |
87 public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, TranslationInterface $string_translation, StorageInterface $active_storage, EventDispatcherInterface $event_dispatcher) { | 104 * The entity repository. |
88 $this->entityManager = $entity_manager; | 105 */ |
106 public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager, TranslationInterface $string_translation, StorageInterface $active_storage, EventDispatcherInterface $event_dispatcher, EntityRepositoryInterface $entity_repository = NULL) { | |
107 if ($entity_type_manager instanceof EntityManagerInterface) { | |
108 @trigger_error('Passing the entity.manager service to ConfigManager::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); | |
109 $this->entityTypeManager = \Drupal::entityTypeManager(); | |
110 } | |
111 else { | |
112 $this->entityTypeManager = $entity_type_manager; | |
113 } | |
89 $this->configFactory = $config_factory; | 114 $this->configFactory = $config_factory; |
90 $this->typedConfigManager = $typed_config_manager; | 115 $this->typedConfigManager = $typed_config_manager; |
91 $this->stringTranslation = $string_translation; | 116 $this->stringTranslation = $string_translation; |
92 $this->activeStorage = $active_storage; | 117 $this->activeStorage = $active_storage; |
93 $this->eventDispatcher = $event_dispatcher; | 118 $this->eventDispatcher = $event_dispatcher; |
119 if ($entity_repository) { | |
120 $this->entityRepository = $entity_repository; | |
121 } | |
122 else { | |
123 @trigger_error('The entity.repository service must be passed to ConfigManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); | |
124 $this->entityRepository = \Drupal::service('entity.repository'); | |
125 } | |
94 } | 126 } |
95 | 127 |
96 /** | 128 /** |
97 * {@inheritdoc} | 129 * {@inheritdoc} |
98 */ | 130 */ |
99 public function getEntityTypeIdByName($name) { | 131 public function getEntityTypeIdByName($name) { |
100 $entities = array_filter($this->entityManager->getDefinitions(), function (EntityTypeInterface $entity_type) use ($name) { | 132 $entities = array_filter($this->entityTypeManager->getDefinitions(), function (EntityTypeInterface $entity_type) use ($name) { |
101 return ($entity_type instanceof ConfigEntityTypeInterface && $config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0; | 133 return ($entity_type instanceof ConfigEntityTypeInterface && $config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0; |
102 }); | 134 }); |
103 return key($entities); | 135 return key($entities); |
104 } | 136 } |
105 | 137 |
107 * {@inheritdoc} | 139 * {@inheritdoc} |
108 */ | 140 */ |
109 public function loadConfigEntityByName($name) { | 141 public function loadConfigEntityByName($name) { |
110 $entity_type_id = $this->getEntityTypeIdByName($name); | 142 $entity_type_id = $this->getEntityTypeIdByName($name); |
111 if ($entity_type_id) { | 143 if ($entity_type_id) { |
112 $entity_type = $this->entityManager->getDefinition($entity_type_id); | 144 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); |
113 $id = substr($name, strlen($entity_type->getConfigPrefix()) + 1); | 145 $id = substr($name, strlen($entity_type->getConfigPrefix()) + 1); |
114 return $this->entityManager->getStorage($entity_type_id)->load($id); | 146 return $this->entityTypeManager->getStorage($entity_type_id)->load($id); |
115 } | 147 } |
116 return NULL; | 148 return NULL; |
117 } | 149 } |
118 | 150 |
119 /** | 151 /** |
120 * {@inheritdoc} | 152 * {@inheritdoc} |
121 */ | 153 */ |
122 public function getEntityManager() { | 154 public function getEntityManager() { |
123 return $this->entityManager; | 155 @trigger_error('ConfigManagerInterface::getEntityManager() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use ::getEntityTypeManager() instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); |
156 return \Drupal::service('entity.manager'); | |
157 } | |
158 | |
159 /** | |
160 * {@inheritdoc} | |
161 */ | |
162 public function getEntityTypeManager() { | |
163 return $this->entityTypeManager; | |
124 } | 164 } |
125 | 165 |
126 /** | 166 /** |
127 * {@inheritdoc} | 167 * {@inheritdoc} |
128 */ | 168 */ |
166 | 206 |
167 /** | 207 /** |
168 * {@inheritdoc} | 208 * {@inheritdoc} |
169 */ | 209 */ |
170 public function createSnapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) { | 210 public function createSnapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) { |
171 // Empty the snapshot of all configuration. | 211 self::replaceStorageContents($source_storage, $snapshot_storage); |
172 $snapshot_storage->deleteAll(); | |
173 foreach ($snapshot_storage->getAllCollectionNames() as $collection) { | |
174 $snapshot_collection = $snapshot_storage->createCollection($collection); | |
175 $snapshot_collection->deleteAll(); | |
176 } | |
177 foreach ($source_storage->listAll() as $name) { | |
178 $snapshot_storage->write($name, $source_storage->read($name)); | |
179 } | |
180 // Copy collections as well. | |
181 foreach ($source_storage->getAllCollectionNames() as $collection) { | |
182 $source_collection = $source_storage->createCollection($collection); | |
183 $snapshot_collection = $snapshot_storage->createCollection($collection); | |
184 foreach ($source_collection->listAll() as $name) { | |
185 $snapshot_collection->write($name, $source_collection->read($name)); | |
186 } | |
187 } | |
188 } | 212 } |
189 | 213 |
190 /** | 214 /** |
191 * {@inheritdoc} | 215 * {@inheritdoc} |
192 */ | 216 */ |
262 * {@inheritdoc} | 286 * {@inheritdoc} |
263 */ | 287 */ |
264 public function findConfigEntityDependentsAsEntities($type, array $names, ConfigDependencyManager $dependency_manager = NULL) { | 288 public function findConfigEntityDependentsAsEntities($type, array $names, ConfigDependencyManager $dependency_manager = NULL) { |
265 $dependencies = $this->findConfigEntityDependents($type, $names, $dependency_manager); | 289 $dependencies = $this->findConfigEntityDependents($type, $names, $dependency_manager); |
266 $entities = []; | 290 $entities = []; |
267 $definitions = $this->entityManager->getDefinitions(); | 291 $definitions = $this->entityTypeManager->getDefinitions(); |
268 foreach ($dependencies as $config_name => $dependency) { | 292 foreach ($dependencies as $config_name => $dependency) { |
269 // Group by entity type to efficient load entities using | 293 // Group by entity type to efficient load entities using |
270 // \Drupal\Core\Entity\EntityStorageInterface::loadMultiple(). | 294 // \Drupal\Core\Entity\EntityStorageInterface::loadMultiple(). |
271 $entity_type_id = $this->getEntityTypeIdByName($config_name); | 295 $entity_type_id = $this->getEntityTypeIdByName($config_name); |
272 // It is possible that a non-configuration entity will be returned if a | 296 // It is possible that a non-configuration entity will be returned if a |
278 $entities[$entity_type_id][] = $id; | 302 $entities[$entity_type_id][] = $id; |
279 } | 303 } |
280 } | 304 } |
281 $entities_to_return = []; | 305 $entities_to_return = []; |
282 foreach ($entities as $entity_type_id => $entities_to_load) { | 306 foreach ($entities as $entity_type_id => $entities_to_load) { |
283 $storage = $this->entityManager->getStorage($entity_type_id); | 307 $storage = $this->entityTypeManager->getStorage($entity_type_id); |
284 // Remove the keys since there are potential ID clashes from different | 308 // Remove the keys since there are potential ID clashes from different |
285 // configuration entity types. | 309 // configuration entity types. |
286 $entities_to_return = array_merge($entities_to_return, array_values($storage->loadMultiple($entities_to_load))); | 310 $entities_to_return = array_merge($entities_to_return, array_values($storage->loadMultiple($entities_to_load))); |
287 } | 311 } |
288 return $entities_to_return; | 312 return $entities_to_return; |
438 return $this->loadConfigEntityByName($name); | 462 return $this->loadConfigEntityByName($name); |
439 } | 463 } |
440 else { | 464 else { |
441 // Ignore the bundle. | 465 // Ignore the bundle. |
442 list($entity_type_id,, $uuid) = explode(':', $name); | 466 list($entity_type_id,, $uuid) = explode(':', $name); |
443 return $this->entityManager->loadEntityByConfigTarget($entity_type_id, $uuid); | 467 return $this->entityRepository->loadEntityByConfigTarget($entity_type_id, $uuid); |
444 } | 468 } |
445 }, $affected_dependencies[$type]); | 469 }, $affected_dependencies[$type]); |
446 } | 470 } |
447 } | 471 } |
448 | 472 |
485 } | 509 } |
486 } | 510 } |
487 foreach (array_unique($content_dependencies) as $content_dependency) { | 511 foreach (array_unique($content_dependencies) as $content_dependency) { |
488 // Format of the dependency is entity_type:bundle:uuid. | 512 // Format of the dependency is entity_type:bundle:uuid. |
489 list($entity_type, $bundle, $uuid) = explode(':', $content_dependency, 3); | 513 list($entity_type, $bundle, $uuid) = explode(':', $content_dependency, 3); |
490 if (!$this->entityManager->loadEntityByUuid($entity_type, $uuid)) { | 514 if (!$this->entityRepository->loadEntityByUuid($entity_type, $uuid)) { |
491 $missing_dependencies[$uuid] = [ | 515 $missing_dependencies[$uuid] = [ |
492 'entity_type' => $entity_type, | 516 'entity_type' => $entity_type, |
493 'bundle' => $bundle, | 517 'bundle' => $bundle, |
494 'uuid' => $uuid, | 518 'uuid' => $uuid, |
495 ]; | 519 ]; |