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