UnmetDependenciesException.php 3.37 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Config;

5
use Drupal\Component\Render\FormattableMarkup;
6 7 8 9 10 11 12 13 14 15
use Drupal\Core\StringTranslation\TranslationInterface;

/**
 * An exception thrown if configuration has unmet dependencies.
 */
class UnmetDependenciesException extends ConfigException {

  /**
   * A list of configuration objects that have unmet dependencies.
   *
16 17 18 19 20 21 22 23 24 25 26 27 28 29
   * The list is keyed by the config object name, and the value is an array of
   * the missing dependencies:
   *
   * @code
   *
   * self::configObjects = [
   *   config_object_name => [
   *     'missing_dependency_1',
   *     'missing_dependency_2',
   *   ]
   * ];
   *
   * @endcode
   *
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   * @var array
   */
  protected $configObjects = [];

  /**
   * The name of the extension that is being installed.
   *
   * @var string
   */
  protected $extension;

  /**
   * Gets the list of configuration objects that have unmet dependencies.
   *
   * @return array
45 46
   *   A list of configuration objects that have unmet dependencies, keyed by
   *   object name, with the value being a list of the unmet dependencies.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
   */
  public function getConfigObjects() {
    return $this->configObjects;
  }

  /**
   * Gets the name of the extension that is being installed.
   *
   * @return string
   *   The name of the extension that is being installed.
   */
  public function getExtension() {
    return $this->extension;
  }

  /**
   * Gets a translated message from the exception.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
67 68
   * @param string $extension
   *   The name of the extension that is being installed.
69 70 71 72
   *
   * @return string
   */
  public function getTranslatedMessage(TranslationInterface $string_translation, $extension) {
73 74
    return $string_translation->translate(
      'Unable to install %extension due to unmet dependencies: %config_names',
75
      [
76 77
        '%config_names' => static::formatConfigObjectList($this->configObjects),
        '%extension' => $extension,
78 79 80 81 82 83 84 85 86 87
      ]
    );
  }

  /**
   * Creates an exception for an extension and a list of configuration objects.
   *
   * @param $extension
   *   The name of the extension that is being installed.
   * @param array $config_objects
88 89
   *   A list of configuration keyed by configuration name, with unmet
   *   dependencies as the value.
90 91 92 93
   *
   * @return \Drupal\Core\Config\PreExistingConfigException
   */
  public static function create($extension, array $config_objects) {
94
    $message = new FormattableMarkup('Configuration objects provided by %extension have unmet dependencies: %config_names',
95
      [
96
        '%config_names' => static::formatConfigObjectList($config_objects),
97
        '%extension' => $extension,
98
      ]
99 100 101 102 103 104 105
    );
    $e = new static($message);
    $e->configObjects = $config_objects;
    $e->extension = $extension;
    return $e;
  }

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  /**
   * Formats a list of configuration objects.
   *
   * @param array $config_objects
   *   A list of configuration object names that have unmet dependencies.
   *
   * @return string
   *   The imploded config_objects, formatted in an easy to read string.
   */
  protected static function formatConfigObjectList(array $config_objects) {
    $list = [];
    foreach ($config_objects as $config_object => $missing_dependencies) {
      $list[] = $config_object . ' (' . implode(', ', $missing_dependencies) . ')';
    }
    return implode(', ', $list);
  }

123
}