clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,47 @@
<?php
namespace Drupal\config_update;
/**
* Defines an interface for deleting config items.
*/
interface ConfigDeleteInterface {
/**
* Name of the event triggered before configuration delete.
*
* @see \Drupal\config_update\ConfigRevertEvent
* @see \Drupal\config_update\ConfigDeleteInterface::delete()
*/
const PRE_DELETE = 'config_update.pre_delete';
/**
* Name of the event triggered on configuration delete.
*
* @see \Drupal\config_update\ConfigRevertEvent
* @see \Drupal\config_update\ConfigDeleteInterface::delete()
*/
const DELETE = 'config_update.delete';
/**
* Deletes a configuration item.
*
* This action triggers the ConfigDeleteInterface::PRE_DELETE and
* ConfigDeleteInterface::DELETE events if the configuration could be deleted.
*
* @param string $type
* The type of configuration.
* @param string $name
* The name of the config item, without the prefix.
*
* @return bool
* TRUE if the operation succeeded; FALSE if the base configuration could
* not be found to delete. May also throw exceptions if there is a
* problem during deleting the configuration.
*
* @see \Drupal\config_update\ConfigDeleteInterface::PRE_DELETE
* @see \Drupal\config_update\ConfigDeleteInterface::DELETE
*/
public function delete($type, $name);
}
@@ -0,0 +1,46 @@
<?php
namespace Drupal\config_update;
/**
* Defines an interface for config differences.
*/
interface ConfigDiffInterface {
/**
* Decides if two configuration arrays are considered to be the same.
*
* The two arrays are considered to be the same if, after "normalizing", they
* have the same keys and values. It is up to the particular implementing
* class to decide what normalizing means.
*
* @param array|null $source
* Source config.
* @param array|null $target
* Target config.
*
* @return bool
* TRUE if the source and target are the same, and FALSE if they are
* different.
*/
public function same($source, $target);
/**
* Calculates differences between config.
*
* The two arrays are "normalized" and split into lines to compare
* differences. It is up to the particular implementing class to decide what
* normalizing means.
*
* @param array|null $source
* Source config.
* @param array|null $target
* Target config.
*
* @return \Drupal\Component\Diff\Diff
* Diff object for displaying line-by-line differences between source and
* target config.
*/
public function diff($source, $target);
}
@@ -0,0 +1,195 @@
<?php
namespace Drupal\config_update;
use Drupal\Component\Diff\Diff;
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
/**
* Provides methods related to config differences.
*/
class ConfigDiffer implements ConfigDiffInterface {
use StringTranslationTrait;
/**
* List of elements to ignore on top level when comparing config.
*
* @var string[]
*
* @see ConfigDiffer::format().
*/
protected $ignore;
/**
* Prefix to use to indicate config hierarchy.
*
* @var string
*
* @see ConfigDiffer::format().
*/
protected $hierarchyPrefix;
/**
* Prefix to use to indicate config values.
*
* @var string
*
* @see ConfigDiffer::format().
*/
protected $valuePrefix;
/**
* Constructs a ConfigDiffer.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* String translation service.
* @param string[] $ignore
* Config components to ignore at the top level.
* @param string $hierarchy_prefix
* Prefix to use in diffs for array hierarchy.
* @param string $value_prefix
* Prefix to use in diffs for array value.
*/
public function __construct(TranslationInterface $translation, array $ignore = ['uuid', '_core'], $hierarchy_prefix = '::', $value_prefix = ' : ') {
$this->stringTranslation = $translation;
$this->hierarchyPrefix = $hierarchy_prefix;
$this->valuePrefix = $value_prefix;
$this->ignore = $ignore;
}
/**
* Normalizes config for comparison.
*
* Removes elements in the ignore list from the top level of configuration,
* and at each level of the array, removes empty arrays and sorts by array
* key, so that config from different storage can be compared meaningfully.
*
* @param array|null $config
* Configuration array to normalize.
*
* @return array
* Normalized configuration array.
*
* @see ConfigDiffer::format()
* @see ConfigDiffer::$ignore
*/
protected function normalize($config) {
if (empty($config)) {
return [];
}
// Remove "ignore" elements, only at the top level.
foreach ($this->ignore as $element) {
unset($config[$element]);
}
// Recursively normalize and return.
return $this->normalizeArray($config);
}
/**
* Recursively sorts an array by key, and removes empty arrays.
*
* @param array $array
* An array to normalize.
*
* @return array
* An array that is sorted by key, at each level of the array, with empty
* arrays removed.
*/
protected function normalizeArray(array $array) {
foreach ($array as $key => $value) {
if (is_array($value)) {
$new = $this->normalizeArray($value);
if (count($new)) {
$array[$key] = $new;
}
else {
unset($array[$key]);
}
}
}
ksort($array);
return $array;
}
/**
* {@inheritdoc}
*/
public function same($source, $target) {
$source = $this->normalize($source);
$target = $this->normalize($target);
return $source === $target;
}
/**
* Formats config for showing differences.
*
* To compute differences, we need to separate the config into lines and use
* line-by-line differentiator. The obvious way to split into lines is:
* @code
* explode("\n", Yaml::encode($config))
* @endcode
* But this would highlight meaningless differences due to the often different
* order of config files, and also loses the indentation and context of the
* config hierarchy when differences are computed, making the difference
* difficult to interpret.
*
* So, what we do instead is to take the YAML hierarchy and format it so that
* the hierarchy is shown on each line. So, if you're in element
* $config['foo']['bar'] and the value is 'value', you will see
* 'foo::bar : value'.
*
* @param array $config
* Config array to format. Normalize it first if you want to do diffs.
* @param string $prefix
* (optional) When called recursively, the prefix to put on each line. Omit
* when initially calling this function.
*
* @return string[]
* Array of config lines formatted so that a line-by-line diff will show the
* context in each line, and meaningful differences will be computed.
*
* @see ConfigDiffer::normalize()
* @see ConfigDiffer::$hierarchyPrefix
* @see ConfigDiffer::$valuePrefix
*/
protected function format(array $config, $prefix = '') {
$lines = [];
foreach ($config as $key => $value) {
$section_prefix = ($prefix) ? $prefix . $this->hierarchyPrefix . $key : $key;
if (is_array($value)) {
$lines[] = $section_prefix;
$newlines = $this->format($value, $section_prefix);
foreach ($newlines as $line) {
$lines[] = $line;
}
}
else {
$lines[] = $section_prefix . $this->valuePrefix . Yaml::encode($value);
}
}
return $lines;
}
/**
* {@inheritdoc}
*/
public function diff($source, $target) {
$source = $this->normalize($source);
$target = $this->normalize($target);
$source_lines = $this->format($source);
$target_lines = $this->format($target);
return new Diff($source_lines, $target_lines);
}
}
@@ -0,0 +1,37 @@
<?php
namespace Drupal\config_update;
/**
* Defines an extended interface for listing configuration.
*/
interface ConfigListByProviderInterface extends ConfigListInterface {
/**
* Returns the provider of a given config object.
*
* @param string $name
* Name of the config object.
*
* @return string[]
* Array containing the type of extension providing this config object as
* its first element (module, theme, profile), and the name of the provider
* as its second element. NULL if unknown.
*/
public function getConfigProvider($name);
/**
* Lists the providers of a given type that actually have configuration.
*
* @param string $type
* Type of extension (module, theme, profile).
* @param string $name
* Machine name of extension.
*
* @return bool
* TRUE if the extension has either install or optional config, and FALSE
* if it does not.
*/
public function providerHasConfig($type, $name);
}
@@ -0,0 +1,74 @@
<?php
namespace Drupal\config_update;
/**
* Defines an interface for config listings.
*/
interface ConfigListInterface {
/**
* Lists the types of configuration available on the system.
*
* @return \Drupal\Core\Entity\EntityTypeInterface[]
* Array of entity type definitions, keyed by machine name of the type.
*/
public function listTypes();
/**
* Returns the entity type object for a given config type name.
*
* @param string $name
* Config entity type machine name.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* Entity type object with machine name $name.
*/
public function getType($name);
/**
* Returns the entity type object for a given config prefix.
*
* @param string $prefix
* Config prefix.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* Entity type object corresponding to $prefix.
*/
public function getTypeByPrefix($prefix);
/**
* Returns the config type name for a given config object.
*
* @param string $name
* Name of the config object.
*
* @return string
* Name of the config type that this is an instance of, determined by
* prefix. NULL for simple configuration.
*/
public function getTypeNameByConfigName($name);
/**
* Lists the config objects in active and extension storage.
*
* @param string $list_type
* Type of list to make: 'type', 'module', 'theme', or 'profile'.
* @param string $name
* Machine name of a configuration type, module, or theme to generate the
* list for. Ignored for profile, since that uses the active profile. Use
* type 'system.simple' for simple config, and 'system.all' to list all
* config items.
*
* @return array
* Array whose first element is the list of config objects in active
* storage, second is the list of config objects in extension storage,
* and third is the list of optional config objects in extension storage
* (the ones with dependencies from config/optional directories).
* Note that for everything except 'type' lists, the active storage list
* includes all configuration items in the system, not limited to ones from
* this module, theme, or profile.
*/
public function listConfig($list_type, $name);
}
@@ -0,0 +1,254 @@
<?php
namespace Drupal\config_update;
use Drupal\Core\Config\ExtensionInstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Site\Settings;
/**
* Provides methods related to config listing.
*/
class ConfigLister implements ConfigListInterface {
/**
* List of current config entity types, keyed by prefix.
*
* This is not set up until ConfigLister::listTypes() has been called.
*
* @var string[]
*/
protected $typesByPrefix = [];
/**
* List of current config entity type definitions, keyed by entity type.
*
* This is not set up until ConfigLister::listTypes() has been called.
*
* @var \Drupal\Core\Entity\EntityTypeInterface[]
*/
protected $definitions = [];
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;
/**
* The active config storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $activeConfigStorage;
/**
* The extension config storage.
*
* @var \Drupal\Core\Config\ExtensionInstallStorage
*/
protected $extensionConfigStorage;
/**
* The extension config storage for optional config.
*
* @var \Drupal\Core\Config\ExtensionInstallStorage
*/
protected $extensionOptionalConfigStorage;
/**
* Constructs a ConfigLister.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Config\StorageInterface $active_config_storage
* The active config storage.
* @param \Drupal\Core\Config\ExtensionInstallStorage $extension_config_storage
* The extension config storage.
* @param \Drupal\Core\Config\ExtensionInstallStorage $extension_optional_config_storage
* The extension config storage for optional config items.
*/
public function __construct(EntityTypeManagerInterface $entity_manager, StorageInterface $active_config_storage, ExtensionInstallStorage $extension_config_storage, ExtensionInstallStorage $extension_optional_config_storage) {
$this->entityManager = $entity_manager;
$this->activeConfigStorage = $active_config_storage;
$this->extensionConfigStorage = $extension_config_storage;
$this->extensionOptionalConfigStorage = $extension_optional_config_storage;
}
/**
* Sets up and returns the entity definitions list.
*/
public function listTypes() {
// Return list if it has already been calculated.
if (count($this->definitions)) {
return $this->definitions;
}
// Calculate and return the list.
foreach ($this->entityManager->getDefinitions() as $entity_type => $definition) {
if ($definition->entityClassImplements('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
$this->definitions[$entity_type] = $definition;
$prefix = $definition->getConfigPrefix();
$this->typesByPrefix[$prefix] = $entity_type;
}
}
ksort($this->definitions);
return $this->definitions;
}
/**
* {@inheritdoc}
*/
public function getType($name) {
$definitions = $this->listTypes();
return $definitions[$name] ?? NULL;
}
/**
* {@inheritdoc}
*/
public function getTypeByPrefix($prefix) {
$definitions = $this->listTypes();
return isset($this->typesByPrefix[$prefix]) ? $definitions[$this->typesByPrefix[$prefix]] : NULL;
}
/**
* {@inheritdoc}
*/
public function getTypeNameByConfigName($name) {
$definitions = $this->listTypes();
foreach ($this->typesByPrefix as $prefix => $entity_type) {
if (strpos($name, $prefix . '.') === 0) {
return $entity_type;
}
}
return NULL;
}
/**
* {@inheritdoc}
*/
public function listConfig($list_type, $name) {
$active_list = [];
$install_list = [];
$optional_list = [];
$definitions = $this->listTypes();
switch ($list_type) {
case 'type':
if ($name == 'system.all') {
$active_list = $this->activeConfigStorage->listAll();
$install_list = $this->extensionConfigStorage->listAll();
$optional_list = $this->extensionOptionalConfigStorage->listAll();
}
elseif ($name == 'system.simple') {
// Listing is done by prefixes, and simple config doesn't have one.
// So list all and filter out all known prefixes.
$active_list = $this->omitKnownPrefixes($this->activeConfigStorage->listAll());
$install_list = $this->omitKnownPrefixes($this->extensionConfigStorage->listAll());
$optional_list = $this->omitKnownPrefixes($this->extensionOptionalConfigStorage->listAll());
}
elseif (isset($this->definitions[$name])) {
$definition = $this->definitions[$name];
$prefix = $definition->getConfigPrefix();
$active_list = $this->activeConfigStorage->listAll($prefix);
$install_list = $this->extensionConfigStorage->listAll($prefix);
$optional_list = $this->extensionOptionalConfigStorage->listAll($prefix);
}
break;
case 'profile':
$name = $this->getProfileName();
// Intentional fall-through here to the 'module' or 'theme' case.
case 'module':
case 'theme':
$active_list = $this->activeConfigStorage->listAll();
$install_list = $this->listProvidedItems($list_type, $name);
$optional_list = $this->listProvidedItems($list_type, $name, TRUE);
break;
}
// This only seems to be a problem in unit tests, where a mock object
// is returning NULL instead of an empty array for some reason.
if (!is_array($optional_list)) {
$optional_list = [];
}
return [$active_list, $install_list, $optional_list];
}
/**
* Returns a list of the install storage items for an extension.
*
* @param string $type
* Type of extension ('module', etc.).
* @param string $name
* Machine name of extension.
* @param bool $do_optional
* FALSE (default) to list config/install items, TRUE to list
* config/optional items.
*
* @return string[]
* List of config items provided by this extension.
*/
protected function listProvidedItems($type, $name, $do_optional = FALSE) {
// @todo Inject this dependency in the constructor.
// @phpstan-ignore-next-line as it's a dynamic service call.
$pathname = \Drupal::service("extension.list.$type")->getPathname($name);
// @phpstan-ignore-next-line as $this->root failing on previous major.
$component = new Extension(\Drupal::root(), $type, $pathname);
if ($do_optional) {
$names = $this->extensionOptionalConfigStorage->getComponentNames([$component]);
}
else {
$names = $this->extensionConfigStorage->getComponentNames([$component]);
}
return array_keys($names);
}
/**
* Omits config with known prefixes from a list of config names.
*/
protected function omitKnownPrefixes($list) {
$prefixes = array_keys($this->typesByPrefix);
$list = array_combine($list, $list);
foreach ($list as $name) {
foreach ($prefixes as $prefix) {
if (strpos($name, $prefix . '.') === 0) {
unset($list[$name]);
}
}
}
return array_values($list);
}
/**
* Returns the name of the install profile.
*
* For backwards compatibility with pre/post 8.3.x, tries to get it from
* either configuration or settings.
*
* @return string
* The name of the install profile.
*/
protected function getProfileName() {
// Code adapted from DrupalKernel::getInstalProfile() in Core.
// In Core 8.3.x or later, read from config.
$config = $this->activeConfigStorage->read('core.extension');
if (!empty($config['profile'])) {
return $config['profile'];
}
else {
// If system_update_8300() has not yet run, use settings.
return Settings::get('install_profile');
}
}
}
@@ -0,0 +1,151 @@
<?php
namespace Drupal\config_update;
use Drupal\Core\Config\ExtensionInstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
/**
* Provides methods related to config listing, including provider calculation.
*/
class ConfigListerWithProviders extends ConfigLister implements ConfigListByProviderInterface {
/**
* List of providers of config, keyed by config item name.
*
* The array elements are each arrays, with the type of extension providing
* the config object as the first element (module, theme, profile), and the
* name of the provider as the second element.
*
* This is not set up until ConfigListerWithProviders::listProviders() has
* been called.
*
* @var array
*/
protected $providers = [];
/**
* List of extensions that provide configuration, keyed by type.
*
* This is not set up until ConfigListerWithProviders::listProviders() has
* been called.
*
* @var array
*/
protected $extensionsWithConfig = [];
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* Constructs a ConfigListerWithProviders.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Config\StorageInterface $active_config_storage
* The active config storage.
* @param \Drupal\Core\Config\ExtensionInstallStorage $extension_config_storage
* The extension config storage.
* @param \Drupal\Core\Config\ExtensionInstallStorage $extension_optional_config_storage
* The extension config storage for optional config items.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* The theme handler.
*/
public function __construct(EntityTypeManagerInterface $entity_manager, StorageInterface $active_config_storage, ExtensionInstallStorage $extension_config_storage, ExtensionInstallStorage $extension_optional_config_storage, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler) {
$this->entityManager = $entity_manager;
$this->activeConfigStorage = $active_config_storage;
$this->extensionConfigStorage = $extension_config_storage;
$this->extensionOptionalConfigStorage = $extension_optional_config_storage;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
}
/**
* Sets up and returns the config providers list.
*/
public function listProviders() {
// Return the list if it has already been set up.
if (count($this->providers)) {
return $this->providers;
}
// Calculate the list of who provides which config, if it hasn't been set
// up yet. List all of the profile, modules, and themes. Profile needs to
// come last, so it will override config in modules and themes in our list,
// matching what the config system does.
$extensionsToDo = [];
$profile = $this->getProfileName();
$modules = $this->moduleHandler->getModuleList();
foreach ($modules as $machine_name => $module) {
if ($machine_name != $profile) {
$extensionsToDo[] = ['module', $machine_name];
}
}
$themes = $this->themeHandler->listInfo();
foreach ($themes as $machine_name => $theme) {
$extensionsToDo[] = ['theme', $machine_name];
}
$extensionsToDo[] = ['profile', $profile];
// For each extension, figure out if it has config, and make an index of
// config item => provider.
$this->extensionsWithConfig = [
'profile' => [],
'module' => [],
'theme' => [],
];
foreach ($extensionsToDo as $item) {
$type = $item[0];
$name = $item[1];
$configs = array_merge($this->listProvidedItems($type, $name),
$this->listProvidedItems($type, $name, TRUE));
if (!empty($configs)) {
$this->extensionsWithConfig[$type][$name] = $name;
foreach ($configs as $config) {
$this->providers[$config] = [$type, $name];
}
}
}
return $this->providers;
}
/**
* {@inheritdoc}
*/
public function getConfigProvider($name) {
$providers = $this->listProviders();
return (isset($providers[$name])) ? $providers[$name] : NULL;
}
/**
* {@inheritdoc}
*/
public function providerHasConfig($type, $name) {
// Make sure the list of extensions with configuration has been generated.
$this->listProviders();
return isset($this->extensionsWithConfig[$type][$name]);
}
}
@@ -0,0 +1,122 @@
<?php
namespace Drupal\config_update;
use Drupal\Component\EventDispatcher\Event;
/**
* Event context class for configuration pre-revert/pre-import events.
*
* This class is passed in as the event when the
* \Drupal\config_update\ConfigRevertInterface::PRE_IMPORT and
* \Drupal\config_update\ConfigRevertInterface::PRE_REVERT events are triggered.
*/
class ConfigPreRevertEvent extends Event {
/**
* The type of configuration that is being imported or reverted.
*
* @var string
*/
protected $type;
/**
* The name of the config item being imported or reverted, without prefix.
*
* @var string
*/
protected $name;
/**
* The current configuration to be applied.
*
* @var array
*/
protected $value;
/**
* The active configuration.
*
* Only available on \Drupal\config_update\ConfigRevertInterface::PRE_REVERT.
*
* @var array|null
*/
protected $active;
/**
* Constructs a new ConfigPreRevertEvent.
*
* @param string $type
* The type of configuration being imported or reverted.
* @param string $name
* The name of the config item being imported/reverted, without prefix.
* @param array $value
* The current configuration.
* @param array|null $active
* The active configuration.
*/
public function __construct($type, $name, array $value, $active) {
$this->type = $type;
$this->name = $name;
$this->active = $active;
$this->value = $value;
}
/**
* Returns the type of configuration being imported or reverted.
*
* @return string
* The type of configuration, either 'system.simple' or a config entity
* type machine name.
*/
public function getType() {
return $this->type;
}
/**
* Returns the name of the config item, without prefix.
*
* @return string
* The name of the config item being imported/reverted/deleted, with the
* prefix.
*/
public function getName() {
return $this->name;
}
/**
* Returns the current configuration value to be applied.
*
* @return array
* The configuration value.
*/
public function getValue() {
return $this->value;
}
/**
* Set the configuration value to import or revert to after the event.
*
* This new value will be imported in place of the configuration value
* originally read from the filesystem.
*
* @param array $value
* The configuration value.
*/
public function setValue(array $value) {
$this->value = $value;
}
/**
* Returns the active configuration.
*
* Only available on \Drupal\config_update\ConfigRevertInterface::PRE_REVERT.
*
* @return array|null
* The active configuration.
*/
public function getActive() {
return $this->active;
}
}
@@ -0,0 +1,67 @@
<?php
namespace Drupal\config_update;
use Drupal\Component\EventDispatcher\Event;
/**
* Event context class for configuration revert/import events.
*
* This class is passed in as the event when the
* \Drupal\config_update\ConfigRevertInterface::IMPORT,
* \Drupal\config_update\ConfigDeleteInterface::PRE_DELETE,
* \Drupal\config_update\ConfigDeleteInterface::DELETE, and
* \Drupal\config_update\ConfigRevertInterface::REVERT events are triggered.
*/
class ConfigRevertEvent extends Event {
/**
* The type of configuration that is being imported or reverted.
*
* @var string
*/
protected $type;
/**
* The name of the config item being imported or reverted, without prefix.
*
* @var string
*/
protected $name;
/**
* Constructs a new ConfigRevertEvent.
*
* @param string $type
* The type of configuration being imported or reverted.
* @param string $name
* The name of the config item being imported/reverted, without prefix.
*/
public function __construct($type, $name) {
$this->type = $type;
$this->name = $name;
}
/**
* Returns the type of configuration being imported or reverted.
*
* @return string
* The type of configuration, either 'system.simple' or a config entity
* type machine name.
*/
public function getType() {
return $this->type;
}
/**
* Returns the name of the config item, without prefix.
*
* @return string
* The name of the config item being imported/reverted/deleted, with the
* prefix.
*/
public function getName() {
return $this->name;
}
}
@@ -0,0 +1,117 @@
<?php
namespace Drupal\config_update;
/**
* Defines an interface for config import and revert operations.
*/
interface ConfigRevertInterface {
/**
* Name of the event triggered before configuration import.
*
* @see \Drupal\config_update\ConfigPreRevertEvent
* @see \Drupal\config_update\ConfigRevertInterface::import()
*/
const PRE_IMPORT = 'config_update.pre_import';
/**
* Name of the event triggered on configuration import.
*
* @see \Drupal\config_update\ConfigRevertEvent
* @see \Drupal\config_update\ConfigRevertInterface::import()
*/
const IMPORT = 'config_update.import';
/**
* Name of the event triggered before configuration revert.
*
* @see \Drupal\config_update\ConfigPreRevertEvent
* @see \Drupal\config_update\ConfigRevertInterface::revert()
*/
const PRE_REVERT = 'config_update.pre_revert';
/**
* Name of the event triggered on configuration revert.
*
* @see \Drupal\config_update\ConfigRevertEvent
* @see \Drupal\config_update\ConfigRevertInterface::revert()
*/
const REVERT = 'config_update.revert';
/**
* Imports configuration from extension storage to active storage.
*
* This action triggers the ConfigRevertInterface::PRE_IMPORT and
* ConfigRevertInterface::IMPORT events if the configuration could be
* imported.
*
* @param string $type
* The type of configuration.
* @param string $name
* The name of the config item, without the prefix.
*
* @return bool
* TRUE if the operation succeeded; FALSE if the configuration could not
* be found to import. May also throw exceptions if there is a problem
* during saving the configuration.
*
* @see \Drupal\config_update\ConfigRevertInterface::PRE_IMPORT
* @see \Drupal\config_update\ConfigRevertInterface::IMPORT
*/
public function import($type, $name);
/**
* Reverts configuration to the value from extension storage.
*
* This action triggers the ConfigRevertInterface::PRE_REVERT and
* ConfigRevertInterface::REVERT events if the configuration could be
* reverted.
*
* @param string $type
* The type of configuration.
* @param string $name
* The name of the config item, without the prefix.
*
* @return bool
* TRUE if the operation succeeded; FALSE if the base configuration could
* not be found to revert to. May also throw exceptions if there is a
* problem during saving the configuration.
*
* @see \Drupal\config_update\ConfigRevertInterface::PRE_REVERT
* @see \Drupal\config_update\ConfigRevertInterface::REVERT
*/
public function revert($type, $name);
/**
* Gets the current active value of configuration.
*
* @param string $type
* The type of configuration. Or pass '' to indicate that $name is the full
* name.
* @param string $name
* The name of the config item, without the prefix.
*
* @return array
* The configuration value, or FALSE if it is not found.
*/
public function getFromActive($type, $name);
/**
* Gets the extension storage value of configuration.
*
* This is the value from a file in the config/install or config/optional
* directory of a module, theme, or install profile.
*
* @param string $type
* The type of configuration. Or pass '' to indicate that $name is the full
* name.
* @param string $name
* The name of the config item, without the prefix.
*
* @return array|false
* The configuration value, or FALSE if it could not be located.
*/
public function getFromExtension($type, $name);
}
@@ -0,0 +1,278 @@
<?php
namespace Drupal\config_update;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Provides methods related to config reverting, deleting, and importing.
*
* In this class, when any import or revert operation is requested, the
* configuration that is being reverted or imported is searched for in both the
* config/install repository and config/optional. This happens automatically.
*/
class ConfigReverter implements ConfigRevertInterface, ConfigDeleteInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityManager;
/**
* The active config storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $activeConfigStorage;
/**
* The extension config storage for config/install config items.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $extensionConfigStorage;
/**
* The extension config storage for config/optional config items.
*
* @var \Drupal\Core\Config\ExtensionInstallStorage
*/
protected $extensionOptionalConfigStorage;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
/**
* Constructs a ConfigReverter.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Config\StorageInterface $active_config_storage
* The active config storage.
* @param \Drupal\Core\Config\StorageInterface $extension_config_storage
* The extension config storage.
* @param \Drupal\Core\Config\StorageInterface $extension_optional_config_storage
* The extension config storage for optional config items.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
* The event dispatcher.
*/
public function __construct(EntityTypeManagerInterface $entity_manager, StorageInterface $active_config_storage, StorageInterface $extension_config_storage, StorageInterface $extension_optional_config_storage, ConfigFactoryInterface $config_factory, EventDispatcherInterface $dispatcher) {
$this->entityManager = $entity_manager;
$this->activeConfigStorage = $active_config_storage;
$this->extensionConfigStorage = $extension_config_storage;
$this->extensionOptionalConfigStorage = $extension_optional_config_storage;
$this->configFactory = $config_factory;
$this->dispatcher = $dispatcher;
}
/**
* {@inheritdoc}
*/
public function import($type, $name) {
// Read the config from the file. Note: Do not call getFromExtension() here
// because we need $full_name below.
$full_name = $this->getFullName($type, $name);
$value = FALSE;
if ($full_name) {
$value = $this->extensionConfigStorage->read($full_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage->read($full_name);
}
}
if (!$value) {
return FALSE;
}
// Trigger an event to modify the configuration value.
$event = new ConfigPreRevertEvent($type, $name, $value, NULL);
$this->dispatcher->dispatch($event, ConfigRevertInterface::PRE_IMPORT);
$value = $event->getValue();
// Save it as a new config entity or simple config.
if ($type === 'system.simple') {
$this->configFactory->getEditable($full_name)->setData($value)->save();
}
else {
$entity_storage = $this->entityManager->getStorage($type);
$entity = $entity_storage->createFromStorageRecord($value);
$entity->save();
}
// Trigger an event notifying of this change.
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher->dispatch($event, ConfigRevertInterface::IMPORT);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function revert($type, $name) {
// Read the config from the file. Note: Do not call getFromExtension() here
// because we need $full_name below.
$value = FALSE;
$full_name = $this->getFullName($type, $name);
if ($full_name) {
$value = $this->extensionConfigStorage->read($full_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage->read($full_name);
}
}
if (!$value) {
return FALSE;
}
// Make sure the configuration exists currently in active storage.
$active_value = $this->activeConfigStorage->read($full_name);
if (!$active_value) {
return FALSE;
}
// Trigger an event to modify the active configuration value.
$event = new ConfigPreRevertEvent($type, $name, $value, $active_value);
$this->dispatcher->dispatch($event, ConfigRevertInterface::PRE_REVERT);
$value = $event->getValue();
// Load the current config and replace the value, retaining the config
// hash (which is part of the _core config key's value).
if ($type === 'system.simple') {
$config = $this->configFactory->getEditable($full_name);
$core = $config->get('_core');
$config
->setData($value)
->set('_core', $core)
->save();
}
else {
$definition = $this->entityManager->getDefinition($type);
$id_key = $definition->getKey('id');
$id = $value[$id_key];
$entity_storage = $this->entityManager->getStorage($type);
$entity = $entity_storage->load($id);
$core = $entity->get('_core');
$entity = $entity_storage->updateFromStorageRecord($entity, $value);
$entity->set('_core', $core);
$entity->save();
}
// Trigger an event notifying of this change.
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher->dispatch($event, ConfigRevertInterface::REVERT);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function delete($type, $name) {
$config = FALSE;
$full_name = $this->getFullName($type, $name);
if ($full_name) {
// Make sure the configuration exists currently in active storage.
if (!$this->activeConfigStorage->read($full_name)) {
return FALSE;
}
$config = $this->configFactory->getEditable($full_name);
}
if (!$config) {
return FALSE;
}
// Trigger an event notifying of this change.
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher->dispatch($event, ConfigDeleteInterface::PRE_DELETE);
if ($type === 'system.simple') {
$config->delete();
}
else {
// Delete a config entity instance, updating all its dependents.
$definition = $this->entityManager->getDefinition($type);
$id = $config->get($definition->getKey('id'));
$entity_storage = $this->entityManager->getStorage($type);
$entity = $entity_storage->load($id);
if (!$entity) {
return FALSE;
}
$entity->delete();
}
// Trigger an event notifying of this change.
$event = new ConfigRevertEvent($type, $name);
$this->dispatcher->dispatch($event, ConfigDeleteInterface::DELETE);
return TRUE;
}
/**
* {@inheritdoc}
*/
public function getFromActive($type, $name) {
$full_name = $this->getFullName($type, $name);
if ($full_name) {
return $this->activeConfigStorage->read($full_name);
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getFromExtension($type, $name) {
$value = FALSE;
$full_name = $this->getFullName($type, $name);
if ($full_name) {
$value = $this->extensionConfigStorage->read($full_name);
if (!$value) {
$value = $this->extensionOptionalConfigStorage->read($full_name);
}
}
return $value;
}
/**
* Returns the full name of a config item.
*
* @param string $type
* The config type, or '' to indicate $name is already prefixed.
* @param string $name
* The config name, without prefix.
*
* @return string
* The config item's full name, or FALSE if there is an error.
*/
protected function getFullName($type, $name) {
if ($type === 'system.simple' || !$type) {
return $name;
}
$definition = $this->entityManager->getDefinition($type);
if ($definition) {
$prefix = $definition->getConfigPrefix() . '.';
return $prefix . $name;
}
else {
return FALSE;
}
}
}