ConfigEntityDependency.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace Drupal\Core\Config\Entity;
  3. use Drupal\Component\Utility\NestedArray;
  4. /**
  5. * Provides a value object to discover configuration dependencies.
  6. *
  7. * @see \Drupal\Core\Config\Entity\ConfigDependencyManager
  8. */
  9. class ConfigEntityDependency {
  10. /**
  11. * The configuration entity's configuration object name.
  12. *
  13. * @var string
  14. */
  15. protected $name;
  16. /**
  17. * The configuration entity's dependencies.
  18. *
  19. * @var array
  20. */
  21. protected $dependencies = [];
  22. /**
  23. * Constructs the configuration entity dependency from the entity values.
  24. *
  25. * @param string $name
  26. * The configuration entity's configuration object name.
  27. * @param array $values
  28. * (optional) The configuration entity's values.
  29. */
  30. public function __construct($name, $values = []) {
  31. $this->name = $name;
  32. if (isset($values['dependencies']) && isset($values['dependencies']['enforced'])) {
  33. // Merge the enforced dependencies into the list of dependencies.
  34. $enforced_dependencies = $values['dependencies']['enforced'];
  35. unset($values['dependencies']['enforced']);
  36. $this->dependencies = NestedArray::mergeDeep($values['dependencies'], $enforced_dependencies);
  37. }
  38. elseif (isset($values['dependencies'])) {
  39. $this->dependencies = $values['dependencies'];
  40. }
  41. }
  42. /**
  43. * Gets the configuration entity's dependencies of the supplied type.
  44. *
  45. * @param string $type
  46. * The type of dependency to return. Either 'module', 'theme', 'config' or
  47. * 'content'.
  48. *
  49. * @return array
  50. * The list of dependencies of the supplied type.
  51. */
  52. public function getDependencies($type) {
  53. $dependencies = [];
  54. if (isset($this->dependencies[$type])) {
  55. $dependencies = $this->dependencies[$type];
  56. }
  57. if ($type == 'module') {
  58. $dependencies[] = substr($this->name, 0, strpos($this->name, '.'));
  59. }
  60. return $dependencies;
  61. }
  62. /**
  63. * Determines if the entity is dependent on extensions or entities.
  64. *
  65. * @param string $type
  66. * The type of dependency being checked. Either 'module', 'theme', 'config'
  67. * or 'content'.
  68. * @param string $name
  69. * The specific name to check. If $type equals 'module' or 'theme' then it
  70. * should be a module name or theme name. In the case of entity it should be
  71. * the full configuration object name.
  72. *
  73. * @return bool
  74. */
  75. public function hasDependency($type, $name) {
  76. // A config entity is always dependent on its provider.
  77. if ($type == 'module' && strpos($this->name, $name . '.') === 0) {
  78. return TRUE;
  79. }
  80. return isset($this->dependencies[$type]) && array_search($name, $this->dependencies[$type]) !== FALSE;
  81. }
  82. /**
  83. * Gets the configuration entity's configuration dependency name.
  84. *
  85. * @see \Drupal\Core\Entity\EntityInterface::getConfigDependencyName()
  86. *
  87. * @return string
  88. * The configuration dependency name for the entity.
  89. */
  90. public function getConfigDependencyName() {
  91. return $this->name;
  92. }
  93. }