diff --git a/src/Plugin/ConfigFilter/IgnoreFilter.php b/src/Plugin/ConfigFilter/IgnoreFilter.php new file mode 100644 index 0000000000000000000000000000000000000000..5cc4e3ee9621bd6f3b5fb3c185a4168a8d1ca9fb --- /dev/null +++ b/src/Plugin/ConfigFilter/IgnoreFilter.php @@ -0,0 +1,165 @@ +active = $active; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + // Get the list of ignored config. + $ignored = $container->get('config.factory')->get('config_ignore.settings')->get('ignored_config_entities'); + // Allow hooks to alter the list. + $container->get('module_handler')->invokeAll('config_ignore_settings_alter', [&$ignored]); + // Set the list in the plugin configuration. + $configuration['ignored'] = $ignored; + + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('config.storage') + ); + } + + /** + * Match a config entity name against the list of ignored config entities. + * + * @param string $config_name + * The name of the config entity to match against all ignored entities. + * + * @return bool + * True, if the config entity is to be ignored, false otherwise. + */ + protected function matchConfigName($config_name) { + if (Settings::get('config_ignore_deactivate')) { + // Allow deactivating config_ignore in settings.php. Do not match any name + // in that case and allow a normal configuration import to happen. + return FALSE; + } + + foreach ($this->configuration['ignored'] as $config_ignore_setting) { + // Check if the last character in the string is an asterisk. + // If so, it means that it is a wildcard. + if (Unicode::substr($config_ignore_setting, -1) == '*') { + // Remove the asterisk character from the end of the string. + $config_ignore_setting = rtrim($config_ignore_setting, '*'); + // Test if the start of the config, we are checking, are matching + // the $config_ignore_setting string. If it is a match, mark + // that config name to be ignored. + if (Unicode::substr($config_name, 0, strlen($config_ignore_setting)) == $config_ignore_setting) { + return TRUE; + } + } + // If string does not contain an asterisk in the end, just compare + // the two strings, and if they match, mark that config name to be + // ignored. + elseif ($config_name == $config_ignore_setting) { + return TRUE; + } + } + + return FALSE; + } + + /** + * {@inheritdoc} + */ + public function filterRead($name, $data) { + // Read from the active storage when the name is in the ignored list. + if ($this->matchConfigName($name)) { + return $this->active->read($name); + } + + return $data; + } + + /** + * {@inheritdoc} + */ + public function filterExists($name, $exists) { + // A name exists if it is ignored and exists in the active storage. + return $exists || ($this->matchConfigName($name) && $this->active->exists($name)); + } + + /** + * {@inheritdoc} + */ + public function filterReadMultiple(array $names, array $data) { + // Limit the names which are read from the active storage. + $names = array_filter($names, [$this, 'matchConfigName']); + $active_data = $this->active->readMultiple($names); + + // Return the data with merged in active data. + return array_merge($data, $active_data); + } + + /** + * {@inheritdoc} + */ + public function filterListAll($prefix, array $data) { + $active_names = $this->active->listAll($prefix); + // Filter out only ignored config names. + $active_names = array_filter($active_names, [$this, 'matchConfigName']); + + // Return the data with the active names which are ignored merged in. + return array_unique(array_merge($data, $active_names)); + } + + /** + * {@inheritdoc} + */ + public function filterCreateCollection($collection) { + return new static($this->configuration, $this->pluginId, $this->pluginDefinition, $this->active->createCollection($collection)); + } + + /** + * {@inheritdoc} + */ + public function filterGetAllCollectionNames(array $collections) { + // Add active collection names as there could be ignored config in them. + return array_merge($collections, $this->active->getAllCollectionNames()); + } + +} \ No newline at end of file