Dependency.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Drupal\Core\Extension;
  3. use Drupal\Component\Version\Constraint;
  4. /**
  5. * A value object representing dependency information.
  6. *
  7. * This class implements \ArrayAccess to provide a backwards compatibility layer
  8. * for Drupal 8.x. This will be removed before Drupal 9.x.
  9. *
  10. * @see https://www.drupal.org/node/2756875
  11. */
  12. class Dependency implements \ArrayAccess {
  13. /**
  14. * The name of the dependency.
  15. *
  16. * @var string
  17. */
  18. protected $name;
  19. /**
  20. * The project namespace for the dependency.
  21. *
  22. * @var string
  23. */
  24. protected $project;
  25. /**
  26. * The constraint string.
  27. *
  28. * @var \Drupal\Component\Version\Constraint
  29. */
  30. protected $constraintString;
  31. /**
  32. * The Constraint object from the constraint string.
  33. *
  34. * @var \Drupal\Component\Version\Constraint
  35. */
  36. protected $constraint;
  37. /**
  38. * Dependency constructor.
  39. *
  40. * @param string $name
  41. * The name of the dependency.
  42. * @param string $project
  43. * The project namespace for the dependency.
  44. * @param string $constraint
  45. * The constraint string. For example, '>8.x-1.1'.
  46. */
  47. public function __construct($name, $project, $constraint) {
  48. $this->name = $name;
  49. $this->project = $project;
  50. $this->constraintString = $constraint;
  51. }
  52. /**
  53. * Gets the dependency's name.
  54. *
  55. * @return string
  56. * The dependency's name.
  57. */
  58. public function getName() {
  59. return $this->name;
  60. }
  61. /**
  62. * Gets the dependency's project namespace.
  63. *
  64. * @return string
  65. * The dependency's project namespace.
  66. */
  67. public function getProject() {
  68. return $this->project;
  69. }
  70. /**
  71. * Gets constraint string from the dependency.
  72. *
  73. * @return string
  74. * The constraint string.
  75. */
  76. public function getConstraintString() {
  77. return $this->constraintString;
  78. }
  79. /**
  80. * Gets the Constraint object.
  81. *
  82. * @return \Drupal\Component\Version\Constraint
  83. * The Constraint object.
  84. */
  85. protected function getConstraint() {
  86. if (!$this->constraint) {
  87. $this->constraint = new Constraint($this->constraintString, \Drupal::CORE_COMPATIBILITY);
  88. }
  89. return $this->constraint;
  90. }
  91. /**
  92. * Determines if the provided version is compatible with this dependency.
  93. *
  94. * @param string $version
  95. * The version to check, for example '4.2'.
  96. *
  97. * @return bool
  98. * TRUE if compatible with the provided version, FALSE if not.
  99. */
  100. public function isCompatible($version) {
  101. return $this->getConstraint()->isCompatible($version);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function offsetExists($offset) {
  107. @trigger_error(sprintf('Array access to %s properties is deprecated. Use accessor methods instead. See https://www.drupal.org/node/2756875', __CLASS__), E_USER_DEPRECATED);
  108. return in_array($offset, ['name', 'project', 'original_version', 'versions'], TRUE);
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function offsetGet($offset) {
  114. switch ($offset) {
  115. case 'name':
  116. @trigger_error(sprintf('Array access to the %s name property is deprecated. Use %s::getName() instead. See https://www.drupal.org/node/2756875', __CLASS__, __CLASS__), E_USER_DEPRECATED);
  117. return $this->getName();
  118. case 'project':
  119. @trigger_error(sprintf('Array access to the %s project property is deprecated. Use %s::getProject() instead. See https://www.drupal.org/node/2756875', __CLASS__, __CLASS__), E_USER_DEPRECATED);
  120. return $this->getProject();
  121. case 'original_version':
  122. @trigger_error(sprintf('Array access to the %s original_version property is deprecated. Use %s::getConstraintString() instead. See https://www.drupal.org/node/2756875', __CLASS__, __CLASS__), E_USER_DEPRECATED);
  123. $constraint = $this->getConstraintString();
  124. if ($constraint) {
  125. $constraint = ' (' . $constraint . ')';
  126. }
  127. return $constraint;
  128. case 'versions':
  129. @trigger_error(sprintf('Array access to the %s versions property is deprecated. See https://www.drupal.org/node/2756875', __CLASS__), E_USER_DEPRECATED);
  130. return $this->getConstraint()->toArray();
  131. }
  132. throw new \InvalidArgumentException("The $offset key is not supported");
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function offsetSet($offset, $value) {
  138. throw new \BadMethodCallException(sprintf('%s() is not supported', __METHOD__));
  139. }
  140. /**
  141. * {@inheritdoc}
  142. */
  143. public function offsetUnset($offset) {
  144. throw new \BadMethodCallException(sprintf('%s() is not supported', __METHOD__));
  145. }
  146. /**
  147. * Creates a new instance of this class from a dependency string.
  148. *
  149. * @param string $dependency
  150. * A dependency string, which specifies a module or theme dependency, and
  151. * optionally the project it comes from and a constraint string that
  152. * determines the versions that are supported. Supported formats include:
  153. * - 'module'
  154. * - 'project:module'
  155. * - 'project:module (>=version, <=version)'.
  156. *
  157. * @return static
  158. */
  159. public static function createFromString($dependency) {
  160. if (strpos($dependency, ':') !== FALSE) {
  161. list($project, $dependency) = explode(':', $dependency);
  162. }
  163. else {
  164. $project = '';
  165. }
  166. $parts = explode('(', $dependency, 2);
  167. $name = trim($parts[0]);
  168. $version_string = isset($parts[1]) ? rtrim($parts[1], ") ") : '';
  169. return new static($name, $project, $version_string);
  170. }
  171. /**
  172. * Prevents unnecessary serialization of constraint objects.
  173. *
  174. * @return array
  175. * The properties to seriailize.
  176. */
  177. public function __sleep() {
  178. return ['name', 'project', 'constraintString'];
  179. }
  180. }