comparison core/lib/Drupal/Component/Version/Constraint.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\Component\Version;
4
5 /**
6 * A value object representing a Drupal version constraint.
7 */
8 class Constraint {
9
10 /**
11 * The constraint represented as a string. For example '>=8.x-5.x'.
12 *
13 * @var string
14 */
15 protected $constraint;
16
17 /**
18 * A list of associative arrays representing the constraint.
19 *
20 * Each containing the keys:
21 * - 'op': can be one of: '=', '==', '!=', '<>', '<', '<=', '>', or '>='.
22 * - 'version': A complete version, e.g. '4.5-beta3'.
23 *
24 * @var array[]
25 */
26 protected $constraintArray = [];
27
28 /**
29 * Constraint constructor.
30 *
31 * @param string $constraint
32 * The constraint string to create the object from. For example, '>8.x-1.1'.
33 * @param string $core_compatibility
34 * Core compatibility declared for the current version of Drupal core.
35 * Normally this is set to \Drupal::CORE_COMPATIBILITY by the caller.
36 */
37 public function __construct($constraint, $core_compatibility) {
38 $this->constraint = $constraint;
39 $this->parseConstraint($constraint, $core_compatibility);
40 }
41
42 /**
43 * Gets the constraint as a string.
44 *
45 * Can be used in the UI for reporting incompatibilities.
46 *
47 * @return string
48 * The constraint as a string.
49 */
50 public function __toString() {
51 return $this->constraint;
52 }
53
54 /**
55 * A list of associative arrays representing the constraint.
56 *
57 * Each containing the keys:
58 * - 'op': can be one of: '=', '==', '!=', '<>', '<', '<=', '>', or '>='.
59 * - 'version': A complete version, e.g. '4.5-beta3'.
60 *
61 * @return array[]
62 * The constraint represented as an array.
63 *
64 * @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
65 * Only exists to provide a backwards compatibility layer.
66 *
67 * @see https://www.drupal.org/node/2756875
68 */
69 public function toArray() {
70 @trigger_error(sprintf('%s() only exists to provide a backwards compatibility layer. See https://www.drupal.org/node/2756875', __METHOD__), E_USER_DEPRECATED);
71 return $this->constraintArray;
72 }
73
74 /**
75 * Determines if the provided version is satisfied by this constraint.
76 *
77 * @param string $version
78 * The version to check, for example '4.2'.
79 *
80 * @return bool
81 * TRUE if the provided version is satisfied by this constraint, FALSE if
82 * not.
83 */
84 public function isCompatible($version) {
85 foreach ($this->constraintArray as $constraint) {
86 if (!version_compare($version, $constraint['version'], $constraint['op'])) {
87 return FALSE;
88 }
89 }
90 return TRUE;
91 }
92
93 /**
94 * Parses a constraint string.
95 *
96 * @param string $constraint_string
97 * The constraint string to parse.
98 * @param string $core_compatibility
99 * Core compatibility declared for the current version of Drupal core.
100 * Normally this is set to \Drupal::CORE_COMPATIBILITY by the caller.
101 */
102 private function parseConstraint($constraint_string, $core_compatibility) {
103 // We use named subpatterns and support every op that version_compare
104 // supports. Also, op is optional and defaults to equals.
105 $p_op = '(?<operation>!=|==|=|<|<=|>|>=|<>)?';
106 // Core version is always optional: 8.x-2.x and 2.x is treated the same.
107 $p_core = '(?:' . preg_quote($core_compatibility) . '-)?';
108 $p_major = '(?<major>\d+)';
109 // By setting the minor version to x, branches can be matched.
110 $p_minor = '(?<minor>(?:\d+|x)(?:-[A-Za-z]+\d+)?)';
111 foreach (explode(',', $constraint_string) as $constraint) {
112 if (preg_match("/^\s*$p_op\s*$p_core$p_major\.$p_minor/", $constraint, $matches)) {
113 $op = !empty($matches['operation']) ? $matches['operation'] : '=';
114 if ($matches['minor'] == 'x') {
115 // Drupal considers "2.x" to mean any version that begins with
116 // "2" (e.g. 2.0, 2.9 are all "2.x"). PHP's version_compare(),
117 // on the other hand, treats "x" as a string; so to
118 // version_compare(), "2.x" is considered less than 2.0. This
119 // means that >=2.x and <2.x are handled by version_compare()
120 // as we need, but > and <= are not.
121 if ($op == '>' || $op == '<=') {
122 $matches['major']++;
123 }
124 // Equivalence can be checked by adding two restrictions.
125 if ($op == '=' || $op == '==') {
126 $this->constraintArray[] = ['op' => '<', 'version' => ($matches['major'] + 1) . '.x'];
127 $op = '>=';
128 }
129 }
130 $this->constraintArray[] = ['op' => $op, 'version' => $matches['major'] . '.' . $matches['minor']];
131 }
132 }
133 }
134
135 }