-
Notifications
You must be signed in to change notification settings - Fork 257
/
ConfigUpdater.php
276 lines (234 loc) · 8.67 KB
/
ConfigUpdater.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
namespace Drupal\commerce\Config;
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ExtensionInstallStorage;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* Default implementation of the ConfigUpdaterInterface.
*/
class ConfigUpdater implements ConfigUpdaterInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The active config storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $activeConfigStorage;
/**
* The extension config storage for config/install config items.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $extensionConfigStorage;
/**
* The extension config storage for config/optional config items.
*
* @var \Drupal\Core\Config\ExtensionInstallStorage
*/
protected $extensionOptionalConfigStorage;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* List of current config entity types, keyed by prefix.
*
* @var string[]
*/
protected $typesByPrefix = [];
/**
* Constructs a new ConfigUpdater object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Config\StorageInterface $active_config_storage
* The active config storage.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, StorageInterface $active_config_storage, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->activeConfigStorage = $active_config_storage;
$this->extensionConfigStorage = new ExtensionInstallStorage($active_config_storage, InstallStorage::CONFIG_INSTALL_DIRECTORY);
$this->extensionOptionalConfigStorage = new ExtensionInstallStorage($active_config_storage, InstallStorage::CONFIG_OPTIONAL_DIRECTORY);
$this->configFactory = $config_factory;
foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
/** @var \Drupal\Core\Config\Entity\ConfigEntityTypeInterface $definition */
$prefix = $definition->getConfigPrefix();
$this->typesByPrefix[$prefix] = $entity_type;
}
}
}
/**
* {@inheritdoc}
*/
public function import(array $config_names) {
$succeeded = [];
$failed = [];
foreach ($config_names as $config_name) {
// Read the config from the file.
$value = $this->loadFromExtension($config_name);
if (!$value) {
$failed[$config_name] = $this->t('@config did not exist in extension storage', ['@config' => $config_name]);
continue;
}
if ($this->loadFromActive($config_name)) {
$failed[$config_name] = $this->t('@config already exists, use revert to update', ['@config' => $config_name]);
continue;
}
$type = $this->getConfigType($config_name);
// Save it as a new config entity or simple config.
if ($type == 'system.simple') {
$this->configFactory->getEditable($config_name)->setData($value)->save();
}
else {
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
$entity_storage = $this->entityTypeManager->getStorage($type);
$entity = $entity_storage->createFromStorageRecord($value);
$entity->save();
}
$succeeded[$config_name] = $this->t('@config was successfully imported', ['@config' => $config_name]);
}
return new ConfigUpdateResult($failed, $succeeded);
}
/**
* {@inheritdoc}
*/
public function revert(array $config_names, $skip_modified = TRUE) {
$succeeded = [];
$failed = [];
foreach ($config_names as $config_name) {
// Read the config from the file.
$value = $this->loadFromExtension($config_name);
if (!$value) {
$failed[$config_name] = $this->t('@config did not exist in extension storage', ['@config' => $config_name]);
continue;
}
// Check the configuration object's hash and see if it has been modified.
if ($this->isModified($config_name) && $skip_modified) {
$failed[$config_name] = $this->t('@config has been modified and was not reverted', ['@config' => $config_name]);
continue;
}
$type = $this->getConfigType($config_name);
if ($type == 'system.simple') {
// Load the current config and replace the value.
$this->configFactory->getEditable($config_name)->setData($value)->save();
}
else {
// Load the current config entity and replace the value, with the
// old UUID.
$definition = $this->entityTypeManager->getDefinition($type);
$id_key = $definition->getKey('id');
$id = $value[$id_key];
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
$entity_storage = $this->entityTypeManager->getStorage($type);
/** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
$entity = $entity_storage->load($id);
$uuid = $entity->get('uuid');
$entity = $entity_storage->updateFromStorageRecord($entity, $value);
$entity->set('uuid', $uuid);
$entity->save();
}
$succeeded[$config_name] = $this->t('@config was successfully reverted', ['@config' => $config_name]);
}
return new ConfigUpdateResult($failed, $succeeded);
}
/**
* {@inheritdoc}
*/
public function delete(array $config_names) {
$succeeded = [];
$failed = [];
foreach ($config_names as $config_name) {
$value = $this->loadFromActive($config_name);
if (!$value) {
$failed[$config_name] = $this->t('@config did not exist in extension storage', ['@config' => $config_name]);
continue;
}
// Check the configuration object's hash and see if it has been modified.
if ($this->isModified($config_name)) {
$failed[$config_name] = $this->t('@config has been modified and was not deleted', ['@config' => $config_name]);
continue;
}
$type = $this->getConfigType($config_name);
if ($type == 'system.simple') {
$config = $this->configFactory->getEditable($config_name);
if (!$config) {
$failed[$config_name] = $this->t('@config did not exist in active storage', ['@config' => $config_name]);
continue;
}
$config->delete();
}
else {
$definition = $this->entityTypeManager->getDefinition($type);
$id_key = $definition->getKey('id');
$id = $value[$id_key];
/** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
$entity_storage = $this->entityTypeManager->getStorage($type);
$entity = $entity_storage->load($id);
$entity_storage->delete([$entity]);
}
$succeeded[$config_name] = $this->t('@config was successfully deleted', ['@config' => $config_name]);
}
return new ConfigUpdateResult($failed, $succeeded);
}
/**
* {@inheritdoc}
*/
public function loadFromActive($config_name) {
return $this->activeConfigStorage->read($config_name);
}
/**
* {@inheritdoc}
*/
public function loadFromExtension($config_name) {
$value = $this->extensionConfigStorage->read($config_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage->read($config_name);
}
return $value;
}
/**
* {@inheritdoc}
*/
public function isModified($config_name) {
$active = $this->activeConfigStorage->read($config_name);
// Get the hash created when the config was installed.
$original_hash = $active['_core']['default_config_hash'];
// Remove export keys not used to generate default config hash.
unset($active['uuid']);
unset($active['_core']);
$active_hash = Crypt::hashBase64(serialize($active));
return $original_hash !== $active_hash;
}
/**
* Gets the config type for a given config object.
*
* @param string $config_name
* Name of the config object.
*
* @return string
* Name of the config type.
*/
protected function getConfigType($config_name) {
foreach ($this->typesByPrefix as $prefix => $config_type) {
if (strpos($config_name, $prefix) === 0) {
return $config_type;
}
}
return NULL;
}
}