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