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