UnmetDependenciesException.php 3.3 KB

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