|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composer Merge plugin. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors |
|
6
|
|
|
* |
|
7
|
|
|
* This software may be modified and distributed under the terms of the MIT |
|
8
|
|
|
* license. See the LICENSE file for details. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Wikimedia\Composer\Merge; |
|
12
|
|
|
|
|
13
|
|
|
use Composer\Package\BasePackage; |
|
14
|
|
|
use Composer\Package\Version\VersionParser; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Adapted from Composer's RootPackageLoader::extractStabilityFlags |
|
18
|
|
|
* @author Bryan Davis <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class StabilityFlags |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array Current package name => stability mappings |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $stabilityFlags; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var int Current default minimum stability |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $minimumStability; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string Regex to extract an explict stability flag (eg '@dev') |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $explicitStabilityRe; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $stabilityFlags Current package name => stability mappings |
|
41
|
|
|
* @param int $minimumStability Current default minimum stability |
|
42
|
|
|
*/ |
|
43
|
150 |
|
public function __construct( |
|
44
|
|
|
array $stabilityFlags = array(), |
|
45
|
|
|
$minimumStability = BasePackage::STABILITY_STABLE |
|
46
|
|
|
) { |
|
47
|
150 |
|
$this->stabilityFlags = $stabilityFlags; |
|
48
|
150 |
|
$this->minimumStability = $this->getStabilityInt($minimumStability); |
|
49
|
150 |
|
$this->explicitStabilityRe = '/^[^@]*?@(' . |
|
50
|
150 |
|
implode('|', array_keys(BasePackage::$stabilities)) . |
|
51
|
150 |
|
')$/i'; |
|
52
|
150 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the stability value for a given string. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $name Stability name |
|
58
|
|
|
* @return int Stability value |
|
59
|
|
|
*/ |
|
60
|
150 |
|
protected function getStabilityInt($name) |
|
61
|
|
|
{ |
|
62
|
150 |
|
$name = VersionParser::normalizeStability($name); |
|
63
|
150 |
|
return isset(BasePackage::$stabilities[$name]) ? |
|
64
|
150 |
|
BasePackage::$stabilities[$name] : |
|
65
|
150 |
|
BasePackage::STABILITY_STABLE; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Extract and merge stability flags from the given collection of |
|
70
|
|
|
* requires with another collection of stability flags. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $requires New package name => link mappings |
|
73
|
|
|
* @return array Unified package name => stability mappings |
|
74
|
|
|
*/ |
|
75
|
150 |
|
public function extractAll(array $requires) |
|
76
|
|
|
{ |
|
77
|
150 |
|
$flags = array(); |
|
78
|
|
|
|
|
79
|
150 |
|
foreach ($requires as $name => $link) { |
|
80
|
150 |
|
$name = strtolower($name); |
|
81
|
150 |
|
$version = $link->getPrettyConstraint(); |
|
82
|
|
|
|
|
83
|
150 |
|
$stability = $this->getExplicitStability($version); |
|
84
|
|
|
|
|
85
|
150 |
|
if ($stability === null) { |
|
86
|
110 |
|
$stability = $this->getParsedStability($version); |
|
87
|
22 |
|
} |
|
88
|
|
|
|
|
89
|
150 |
|
$flags[$name] = max($stability, $this->getCurrentStability($name)); |
|
90
|
30 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Filter out null stability values |
|
93
|
150 |
|
return array_filter($flags, function ($v) { |
|
94
|
150 |
|
return $v !== null; |
|
95
|
150 |
|
}); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Extract the most unstable explicit stability (eg '@dev') from a version |
|
101
|
|
|
* specification. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $version |
|
104
|
|
|
* @return int|null Stability or null if no explict stability found |
|
105
|
|
|
*/ |
|
106
|
150 |
|
protected function getExplicitStability($version) |
|
107
|
|
|
{ |
|
108
|
150 |
|
$found = null; |
|
109
|
150 |
|
$constraints = $this->splitConstraints($version); |
|
110
|
150 |
|
foreach ($constraints as $constraint) { |
|
111
|
150 |
|
if (preg_match($this->explicitStabilityRe, $constraint, $match)) { |
|
112
|
65 |
|
$stability = $this->getStabilityInt($match[1]); |
|
113
|
82 |
|
$found = max($stability, $found); |
|
114
|
13 |
|
} |
|
115
|
30 |
|
} |
|
116
|
150 |
|
return $found; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Split a version specification into a list of version constraints. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $version |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
150 |
|
protected function splitConstraints($version) |
|
127
|
|
|
{ |
|
128
|
150 |
|
$found = array(); |
|
129
|
150 |
|
$orConstraints = preg_split('/\s*\|\|?\s*/', trim($version)); |
|
130
|
150 |
|
foreach ($orConstraints as $constraints) { |
|
131
|
150 |
|
$andConstraints = preg_split( |
|
132
|
150 |
|
'/(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)/', |
|
133
|
60 |
|
$constraints |
|
134
|
30 |
|
); |
|
135
|
150 |
|
foreach ($andConstraints as $constraint) { |
|
136
|
150 |
|
$found[] = $constraint; |
|
137
|
30 |
|
} |
|
138
|
30 |
|
} |
|
139
|
150 |
|
return $found; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the stability of a version |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $version |
|
147
|
|
|
* @return int|null Stability or null if STABLE or less than minimum |
|
148
|
|
|
*/ |
|
149
|
110 |
|
protected function getParsedStability($version) |
|
150
|
|
|
{ |
|
151
|
|
|
// Drop aliasing if used |
|
152
|
110 |
|
$version = preg_replace('/^([^,\s@]+) as .+$/', '$1', $version); |
|
153
|
110 |
|
$stability = $this->getStabilityInt( |
|
154
|
110 |
|
VersionParser::parseStability($version) |
|
155
|
22 |
|
); |
|
156
|
|
|
|
|
157
|
110 |
|
if ($stability === BasePackage::STABILITY_STABLE || |
|
158
|
101 |
|
$this->minimumStability > $stability |
|
159
|
22 |
|
) { |
|
160
|
|
|
// Ignore if 'stable' or more stable than the global |
|
161
|
|
|
// minimum |
|
162
|
75 |
|
$stability = null; |
|
163
|
15 |
|
} |
|
164
|
|
|
|
|
165
|
110 |
|
return $stability; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Get the current stability of a given package. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $name |
|
173
|
|
|
* @return int|null Stability of null if not set |
|
174
|
|
|
*/ |
|
175
|
150 |
|
protected function getCurrentStability($name) |
|
176
|
|
|
{ |
|
177
|
150 |
|
return isset($this->stabilityFlags[$name]) ? |
|
178
|
150 |
|
$this->stabilityFlags[$name] : null; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
// vim:sw=4:ts=4:sts=4:et: |
|
182
|
|
|
|