ConfigFactoryOverrideBase.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Drupal\Core\Config;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. /**
  5. * Defines a base event listener implementation configuration overrides.
  6. */
  7. abstract class ConfigFactoryOverrideBase implements EventSubscriberInterface {
  8. /**
  9. * Reacts to the ConfigEvents::COLLECTION_INFO event.
  10. *
  11. * @param \Drupal\Core\Config\ConfigCollectionInfo $collection_info
  12. * The configuration collection info event.
  13. */
  14. abstract public function addCollections(ConfigCollectionInfo $collection_info);
  15. /**
  16. * Actions to be performed to configuration override on configuration save.
  17. *
  18. * @param \Drupal\Core\Config\ConfigCrudEvent $event
  19. * The config CRUD event.
  20. */
  21. abstract public function onConfigSave(ConfigCrudEvent $event);
  22. /**
  23. * Actions to be performed to configuration override on configuration delete.
  24. *
  25. * @param \Drupal\Core\Config\ConfigCrudEvent $event
  26. * The config CRUD event.
  27. */
  28. abstract public function onConfigDelete(ConfigCrudEvent $event);
  29. /**
  30. * Actions to be performed to configuration override on configuration rename.
  31. *
  32. * @param \Drupal\Core\Config\ConfigRenameEvent $event
  33. * The config rename event.
  34. */
  35. abstract public function onConfigRename(ConfigRenameEvent $event);
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function getSubscribedEvents() {
  40. $events[ConfigEvents::COLLECTION_INFO][] = ['addCollections'];
  41. $events[ConfigEvents::SAVE][] = ['onConfigSave', 20];
  42. $events[ConfigEvents::DELETE][] = ['onConfigDelete', 20];
  43. $events[ConfigEvents::RENAME][] = ['onConfigRename', 20];
  44. return $events;
  45. }
  46. /**
  47. * Filters data in the override based on what is currently in configuration.
  48. *
  49. * @param \Drupal\Core\Config\Config $config
  50. * Current configuration object.
  51. * @param \Drupal\Core\Config\StorableConfigBase $override
  52. * Override object corresponding to the configuration to filter data in.
  53. */
  54. protected function filterOverride(Config $config, StorableConfigBase $override) {
  55. $override_data = $override->get();
  56. $changed = $this->filterNestedArray($config->get(), $override_data);
  57. if (empty($override_data)) {
  58. // If no override values are left that would apply, remove the override.
  59. $override->delete();
  60. }
  61. elseif ($changed) {
  62. // Otherwise set the filtered override values back.
  63. $override->setData($override_data)->save(TRUE);
  64. }
  65. }
  66. /**
  67. * Filters data in nested arrays.
  68. *
  69. * @param array $original_data
  70. * Original data array to filter against.
  71. * @param array $override_data
  72. * Override data to filter.
  73. *
  74. * @return bool
  75. * TRUE if $override_data was changed, FALSE otherwise.
  76. */
  77. protected function filterNestedArray(array $original_data, array &$override_data) {
  78. $changed = FALSE;
  79. foreach ($override_data as $key => $value) {
  80. if (!isset($original_data[$key])) {
  81. // The original data is not there anymore, remove the override.
  82. unset($override_data[$key]);
  83. $changed = TRUE;
  84. }
  85. elseif (is_array($override_data[$key])) {
  86. if (is_array($original_data[$key])) {
  87. // Do the filtering one level deeper.
  88. // Ensure that we track $changed along the way.
  89. if ($this->filterNestedArray($original_data[$key], $override_data[$key])) {
  90. $changed = TRUE;
  91. }
  92. // If no overrides are left under this level, remove the level.
  93. if (empty($override_data[$key])) {
  94. unset($override_data[$key]);
  95. $changed = TRUE;
  96. }
  97. }
  98. else {
  99. // The override is an array but the value is not, this will not go
  100. // well, remove the override.
  101. unset($override_data[$key]);
  102. $changed = TRUE;
  103. }
  104. }
  105. }
  106. return $changed;
  107. }
  108. }