UnmetDependenciesException.php 2.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Contains \Drupal\Core\Config\UnmetDependenciesException.
 */

namespace Drupal\Core\Config;

10
use Drupal\Component\Utility\SafeMarkup;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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.
   *
   * @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
   *   A list of configuration objects that have unmet dependencies.
   */
  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.
   *
   * @return string
   */
  public function getTranslatedMessage(TranslationInterface $string_translation, $extension) {
    return $string_translation->formatPlural(
      count($this->getConfigObjects()),
      'Unable to install @extension, %config_names has unmet dependencies.',
      'Unable to install @extension, %config_names have unmet dependencies.',
      [
        '%config_names' => implode(', ', $this->getConfigObjects()),
        '@extension' => $extension,
      ]
    );
  }

  /**
   * 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
   *   A list of configuration object names that have unmet dependencies
   *
   * @return \Drupal\Core\Config\PreExistingConfigException
   */
  public static function create($extension, array $config_objects) {
83
    $message = SafeMarkup::format('Configuration objects (@config_names) provided by @extension have unmet dependencies',
84
85
86
87
88
89
90
91
92
93
94
95
      array(
        '@config_names' => implode(', ', $config_objects),
        '@extension' => $extension
      )
    );
    $e = new static($message);
    $e->configObjects = $config_objects;
    $e->extension = $extension;
    return $e;
  }

}