-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigTransformationEventSubscriber.php
More file actions
107 lines (92 loc) · 3.02 KB
/
Copy pathConfigTransformationEventSubscriber.php
File metadata and controls
107 lines (92 loc) · 3.02 KB
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
<?php
namespace Drupal\islandora_spreadsheet_ingest\EventSubscriber;
use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageTransformEvent;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Config transformation event subscriber.
*
* Inspired by https://www.drupal.org/sandbox/ekes/3187856, which deals instead
* with "webform" entities.
*/
class ConfigTransformationEventSubscriber implements EventSubscriberInterface, ContainerInjectionInterface {
protected const PRIORITY = 250;
protected const PREFIXES_TO_IGNORE = [
'migrate_plus.migration.isi__',
'migrate_plus.migration_group.isi__',
'islandora_spreadsheet_ingest.request.',
];
/**
* Constructor.
*/
public function __construct(
protected StorageInterface $activeStorage,
) {
// No-op.
}
/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.storage'),
);
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() : array {
return [
ConfigEvents::STORAGE_TRANSFORM_EXPORT => [['onExportTransform', static::PRIORITY]],
ConfigEvents::STORAGE_TRANSFORM_IMPORT => [['onImportTransform', -static::PRIORITY]],
];
}
/**
* Config export event handler.
*
* @param \Drupal\Core\Config\StorageTransformEvent $event
* The event to which to respond.
*/
public function onExportTransform(StorageTransformEvent $event) : void {
$storage = $event->getStorage();
foreach (static::PREFIXES_TO_IGNORE as $prefix) {
$storage->deleteAll($prefix);
}
}
/**
* Config import event handler.
*
* @param \Drupal\Core\Config\StorageTransformEvent $event
* The event to which to respond.
*/
public function onImportTransform(StorageTransformEvent $event) : void {
$storage = $event->getStorage();
$inbound = iterator_to_array(static::toIgnore($storage), FALSE);
$current = iterator_to_array(static::toIgnore($this->activeStorage), FALSE);
// In case a config object escaped let's deal with it.
foreach (array_diff($inbound, $current) as $to_delete) {
$storage->delete($to_delete);
}
// Keep the current config as the current config.
foreach ($current as $to_maintain) {
$storage->write($to_maintain, $this->activeStorage->read($to_maintain));
}
}
/**
* Helper; yield all the configs that should not change on imports/exports.
*
* @param \Drupal\Core\Config\StorageInterface $storage
* The storage from which to enumerate configs.
*
* @return \Generator
* The names of the configs that should never be changed on imports/exports.
*/
protected static function toIgnore(StorageInterface $storage) : \Generator {
foreach (static::PREFIXES_TO_IGNORE as $prefix) {
yield from $storage->listAll($prefix);
}
}
}