6c37c76372058bb4ce20c89c32db919535da63b4
[yaffs-website] / web / core / lib / Drupal / Core / Config / UnmetDependenciesException.php
1 <?php
2
3 namespace Drupal\Core\Config;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\StringTranslation\TranslationInterface;
7
8 /**
9  * An exception thrown if configuration has unmet dependencies.
10  */
11 class UnmetDependenciesException extends ConfigException {
12
13   /**
14    * A list of configuration objects that have unmet dependencies.
15    *
16    * The list is keyed by the config object name, and the value is an array of
17    * the missing dependencies:
18    *
19    * @code
20    *
21    * self::configObjects = [
22    *   config_object_name => [
23    *     'missing_dependency_1',
24    *     'missing_dependency_2',
25    *   ]
26    * ];
27    *
28    * @endcode
29    *
30    * @var array
31    */
32   protected $configObjects = [];
33
34   /**
35    * The name of the extension that is being installed.
36    *
37    * @var string
38    */
39   protected $extension;
40
41   /**
42    * Gets the list of configuration objects that have unmet dependencies.
43    *
44    * @return array
45    *   A list of configuration objects that have unmet dependencies, keyed by
46    *   object name, with the value being a list of the unmet dependencies.
47    */
48   public function getConfigObjects() {
49     return $this->configObjects;
50   }
51
52   /**
53    * Gets the name of the extension that is being installed.
54    *
55    * @return string
56    *   The name of the extension that is being installed.
57    */
58   public function getExtension() {
59     return $this->extension;
60   }
61
62   /**
63    * Gets a translated message from the exception.
64    *
65    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
66    *   The string translation service.
67    *
68    * @return string
69    */
70   public function getTranslatedMessage(TranslationInterface $string_translation, $extension) {
71     return $string_translation->translate(
72       'Unable to install %extension due to unmet dependencies: %config_names',
73       [
74         '%config_names' => static::formatConfigObjectList($this->configObjects),
75         '%extension' => $extension,
76       ]
77     );
78   }
79
80   /**
81    * Creates an exception for an extension and a list of configuration objects.
82    *
83    * @param $extension
84    *   The name of the extension that is being installed.
85    * @param array $config_objects
86    *   A list of configuration keyed by configuration name, with unmet
87    *   dependencies as the value.
88    *
89    * @return \Drupal\Core\Config\PreExistingConfigException
90    */
91   public static function create($extension, array $config_objects) {
92     $message = new FormattableMarkup('Configuration objects provided by %extension have unmet dependencies: %config_names',
93       [
94         '%config_names' => static::formatConfigObjectList($config_objects),
95         '%extension' => $extension
96       ]
97     );
98     $e = new static($message);
99     $e->configObjects = $config_objects;
100     $e->extension = $extension;
101     return $e;
102   }
103
104   /**
105    * Formats a list of configuration objects.
106    *
107    * @param array $config_objects
108    *   A list of configuration object names that have unmet dependencies.
109    *
110    * @return string
111    *   The imploded config_objects, formatted in an easy to read string.
112    */
113   protected static function formatConfigObjectList(array $config_objects) {
114     $list = [];
115     foreach ($config_objects as $config_object => $missing_dependencies) {
116       $list[] = $config_object . ' (' . implode(', ', $missing_dependencies) . ')';
117     }
118     return implode(', ', $list);
119   }
120
121 }