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