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