PreExistingConfigException.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Drupal\Core\Config;
  3. use Drupal\Component\Render\FormattableMarkup;
  4. /**
  5. * An exception thrown if configuration with the same name already exists.
  6. */
  7. class PreExistingConfigException extends ConfigException {
  8. /**
  9. * A list of configuration objects that already exist in active configuration.
  10. *
  11. * @var array
  12. */
  13. protected $configObjects = [];
  14. /**
  15. * The name of the module that is being installed.
  16. *
  17. * @var string
  18. */
  19. protected $extension;
  20. /**
  21. * Gets the list of configuration objects that already exist.
  22. *
  23. * @return array
  24. * A list of configuration objects that already exist in active
  25. * configuration keyed by collection.
  26. */
  27. public function getConfigObjects() {
  28. return $this->configObjects;
  29. }
  30. /**
  31. * Gets the name of the extension that is being installed.
  32. *
  33. * @return string
  34. * The name of the extension that is being installed.
  35. */
  36. public function getExtension() {
  37. return $this->extension;
  38. }
  39. /**
  40. * Creates an exception for an extension and a list of configuration objects.
  41. *
  42. * @param $extension
  43. * The name of the extension that is being installed.
  44. * @param array $config_objects
  45. * A list of configuration objects that already exist in active
  46. * configuration, keyed by config collection.
  47. *
  48. * @return \Drupal\Core\Config\PreExistingConfigException
  49. */
  50. public static function create($extension, array $config_objects) {
  51. $message = new FormattableMarkup('Configuration objects (@config_names) provided by @extension already exist in active configuration',
  52. [
  53. '@config_names' => implode(', ', static::flattenConfigObjects($config_objects)),
  54. '@extension' => $extension,
  55. ]
  56. );
  57. $e = new static($message);
  58. $e->configObjects = $config_objects;
  59. $e->extension = $extension;
  60. return $e;
  61. }
  62. /**
  63. * Flattens the config object array to a single dimensional list.
  64. *
  65. * @param array $config_objects
  66. * A list of configuration objects that already exist in active
  67. * configuration, keyed by config collection.
  68. *
  69. * @return array
  70. * A list of configuration objects that have been prefixed with their
  71. * collection.
  72. */
  73. public static function flattenConfigObjects(array $config_objects) {
  74. $flat_config_objects = [];
  75. foreach ($config_objects as $collection => $config_names) {
  76. $config_names = array_map(function ($config_name) use ($collection) {
  77. if ($collection != StorageInterface::DEFAULT_COLLECTION) {
  78. $config_name = str_replace('.', DIRECTORY_SEPARATOR, $collection) . DIRECTORY_SEPARATOR . $config_name;
  79. }
  80. return $config_name;
  81. }, $config_names);
  82. $flat_config_objects = array_merge($flat_config_objects, $config_names);
  83. }
  84. return $flat_config_objects;
  85. }
  86. }