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