annotate core/lib/Drupal/Core/Config/PreExistingConfigException.php @ 19:fa3358dc1485 tip

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