FormOverrides.php in Configuration Override Warn 8
File
src/FormOverrides.php
View source
<?php
namespace Drupal\config_override_warn;
use Drupal\Component\Utility\DiffArray;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormInterface;
class FormOverrides {
protected $configFactory;
protected $typedConfigManager;
public function __construct(ConfigFactoryInterface $config_factory, TypedConfigManagerInterface $typed_config_manager) {
$this->configFactory = $config_factory;
$this->typedConfigManager = $typed_config_manager;
}
public function getFormOverrides(FormInterface $form) {
$names = $this
->getFormConfigNames($form);
$overrides = [];
foreach ($names as $name) {
$overrides = NestedArray::mergeDeep($overrides, $this
->getConfigOverrideDiffs($name));
}
return $overrides;
}
public function getFormConfigNames(FormInterface $form) {
$names = [];
if ($form instanceof EntityForm) {
$entity = $form
->getEntity();
if ($entity instanceof ConfigEntityInterface && !$entity
->isNew()) {
$names = [
$entity
->getConfigDependencyName(),
];
}
}
elseif (method_exists($form, 'getEditableConfigNames')) {
$method = new \ReflectionMethod($form, 'getEditableConfigNames');
$method
->setAccessible(TRUE);
$names = $method
->invoke($form);
}
return $names;
}
public function getConfigOverrideDiffs($name) {
$overrides = [];
$config = $this->configFactory
->get($name);
if ($config_overrides = $this
->getConfigOverrides($config)) {
$definition = $this->typedConfigManager
->getDefinition($name);
$keys = $this
->getConfigKeys($config_overrides, $definition);
foreach ($keys as $key) {
$original_value = $config
->getOriginal($key, FALSE);
$override_value = $config
->get($key);
if (is_array($original_value) && is_array($override_value)) {
$original_original_value = $original_value;
$original_value = DiffArray::diffAssocRecursive($original_value, $override_value);
$override_value = DiffArray::diffAssocRecursive($override_value, $original_original_value);
}
if ($original_value === $override_value) {
continue;
}
if ($this->configFactory
->get('config_override_warn.settings')
->get('show_values')) {
$overrides[$name][$key] = [
'original' => var_export($original_value, TRUE),
'override' => var_export($override_value, TRUE),
];
}
else {
$overrides[$name][$key] = NULL;
}
}
}
return $overrides;
}
public function getConfigOverrides(Config $config) {
$overrides = [];
if ($config
->hasOverrides()) {
$properties = [
'moduleOverrides',
'settingsOverrides',
];
foreach ($properties as $property) {
$reflection = new \ReflectionProperty($config, $property);
$reflection
->setAccessible(TRUE);
$property_overrides = $reflection
->getValue($config);
if (isset($property_overrides) && is_array($property_overrides)) {
$overrides = NestedArray::mergeDeepArray([
$overrides,
$property_overrides,
], TRUE);
}
}
}
return $overrides;
}
protected function getConfigKeys(array $values, array $definition, $prefix = NULL) {
$keys = [];
foreach ($values as $key => $value) {
if (is_array($value) && isset($definition['mapping'])) {
$value_definition = NestedArray::getValue($definition['mapping'], explode('.', $key));
if (isset($value_definition['type']) && $value_definition['type'] === 'mapping') {
$keys = array_merge($keys, $this
->getConfigKeys($value, $value_definition, $prefix . $key . '.'));
continue;
}
}
$keys[] = $prefix . $key;
}
return $keys;
}
}
Classes
Name |
Description |
FormOverrides |
Contains logic for inspecting config forms and their overridden values. |