View source
<?php
namespace Drupal\config_distro_ignore\Plugin\ConfigFilter;
use Drupal\config_distro\Plugin\ConfigDistroFilterBase;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DistroIgnoreFilter extends ConfigDistroFilterBase implements ContainerFactoryPluginInterface {
const HASH_SEPARATOR = '::';
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$config = $container
->get('config.factory')
->get('config_distro_ignore.settings');
$configuration['all_collections'] = $config
->get('all_collections');
$configuration['default_collection'] = $config
->get('default_collection');
$configuration['custom_collections'] = $config
->get('custom_collections');
return new static($configuration, $plugin_id, $plugin_definition);
}
public static function hashConfig($config) {
return md5(serialize($config));
}
public function filterRead($name, $data) {
if ($this
->matchConfigName($name)) {
return $this
->activeRead($name, $data);
}
return $data;
}
public function filterExists($name, $exists) {
return $exists || $this
->matchConfigName($name) && $this
->getSourceStorage()
->exists($name);
}
public function filterReadMultiple(array $names, array $data) {
$names = array_filter($names, [
$this,
'matchConfigName',
]);
foreach ($names as $name) {
$data[$name] = $this
->activeRead($name, $data[$name]);
}
return $data;
}
public function filterListAll($prefix, array $data) {
$active_names = $this
->getSourceStorage()
->listAll($prefix);
$active_names = array_filter($active_names, [
$this,
'matchConfigName',
]);
return array_unique(array_merge($data, $active_names));
}
protected function matchConfigName($config_name) {
if (in_array($config_name, $this->configuration['all_collections'])) {
return TRUE;
}
if (count($this
->getIgnoredHash($config_name))) {
return TRUE;
}
return FALSE;
}
protected function getIgnoredHash($config_name) {
$hashes = [];
$collection = $this
->getSourceStorage()
->getCollectionName();
if ($collection == StorageInterface::DEFAULT_COLLECTION) {
foreach ($this->configuration['default_collection'] as $name) {
$ignore = explode(self::HASH_SEPARATOR, $name);
if (fnmatch($ignore[0], $config_name)) {
if (isset($ignore[1])) {
$hashes[] = $ignore[1];
}
else {
$hashes[] = '';
}
}
}
}
else {
$collection_parts = explode('.', $collection);
$custom_config = $this->configuration['custom_collections'];
foreach ($collection_parts as $part) {
$custom_config = $custom_config[$part];
}
foreach ($custom_config as $name) {
$ignore = explode(self::HASH_SEPARATOR, $name);
if (fnmatch($ignore[0], $config_name)) {
if (isset($ignore[1])) {
$hashes[] = $ignore[1];
}
else {
$hashes[] = '';
}
}
}
}
return $hashes;
}
protected function activeRead($name, $data) {
if (in_array($name, $this->configuration['all_collections'])) {
return $this
->getSourceStorage()
->read($name);
}
$hash = self::hashConfig($data);
$hashes = $this
->getIgnoredHash($name);
if (in_array('', $hashes) || in_array($hash, $hashes)) {
return $this
->getSourceStorage()
->read($name);
}
return $data;
}
}