Debian Codegraph @

vendor/wikimedia/composer-merge-plugin/src/StabilityFlags.php

raw
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\V2;
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 explicit stability flag (eg '@dev')
35 */
36 protected $explicitStabilityRe;
37
38 /**
39 * @param array $stabilityFlags Current package name => stability mappings
40 * @param int|string $minimumStability Current default minimum stability
41 */
42 public function __construct(
43 array $stabilityFlags = [],
44 $minimumStability = BasePackage::STABILITY_STABLE
45 ) {
46 $this->stabilityFlags = $stabilityFlags;
47 $this->minimumStability = $this->getStabilityInt((string)$minimumStability);
48 $this->explicitStabilityRe = '/^[^@]*?@(' .
49 implode('|', array_keys(BasePackage::$stabilities)) .
50 ')$/i';
51 }
52
53 /**
54 * Get the stability value for a given string.
55 *
56 * @param string $name Stability name
57 * @return int Stability value
58 */
59 protected function getStabilityInt($name)
60 {
61 $name = VersionParser::normalizeStability($name);
62 return BasePackage::$stabilities[$name] ?? BasePackage::STABILITY_STABLE;
63 }
64
65 /**
66 * Extract and merge stability flags from the given collection of
67 * requires with another collection of stability flags.
68 *
69 * @param array $requires New package name => link mappings
70 * @return array Unified package name => stability mappings
71 */
72 public function extractAll(array $requires)
73 {
74 $flags = [];
75
76 foreach ($requires as $name => $link) {
77 $name = strtolower($name);
78 $version = $link->getPrettyConstraint();
79
80 $stability = $this->getExplicitStability($version);
81
82 if ($stability === null) {
83 $stability = $this->getParsedStability($version);
84 }
85
86 $flags[$name] = max($stability, $this->getCurrentStability($name));
87 }
88
89 // Filter out null stability values
90 return array_filter($flags, function ($v) {
91 return $v !== null;
92 });
93 }
94
95 /**
96 * Extract the most unstable explicit stability (eg '@dev') from a version
97 * specification.
98 *
99 * @param string $version
100 * @return int|null Stability or null if no explict stability found
101 */
102 protected function getExplicitStability($version)
103 {
104 $found = null;
105 $constraints = $this->splitConstraints($version);
106 foreach ($constraints as $constraint) {
107 if (preg_match($this->explicitStabilityRe, $constraint, $match)) {
108 $stability = $this->getStabilityInt($match[1]);
109 $found = max($stability, $found);
110 }
111 }
112 return $found;
113 }
114
115 /**
116 * Split a version specification into a list of version constraints.
117 *
118 * @param string $version
119 * @return array
120 */
121 protected function splitConstraints($version)
122 {
123 $found = [];
124 $orConstraints = preg_split('/\s*\|\|?\s*/', trim($version));
125 foreach ($orConstraints as $constraints) {
126 $andConstraints = preg_split(
127 '/(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)/',
128 $constraints
129 );
130 foreach ($andConstraints as $constraint) {
131 $found[] = $constraint;
132 }
133 }
134 return $found;
135 }
136
137 /**
138 * Get the stability of a version
139 *
140 * @param string $version
141 * @return int|null Stability or null if STABLE or less than minimum
142 */
143 protected function getParsedStability($version)
144 {
145 // Drop aliasing if used
146 $version = preg_replace('/^([^,\s@]+) as .+$/', '$1', $version);
147 $stability = $this->getStabilityInt(
148 VersionParser::parseStability($version)
149 );
150
151 if ($stability === BasePackage::STABILITY_STABLE ||
152 $this->minimumStability > $stability
153 ) {
154 // Ignore if 'stable' or more stable than the global
155 // minimum
156 $stability = null;
157 }
158
159 return $stability;
160 }
161
162 /**
163 * Get the current stability of a given package.
164 *
165 * @param string $name
166 * @return int|null Stability of null if not set
167 */
168 protected function getCurrentStability($name)
169 {
170 return $this->stabilityFlags[$name] ?? null;
171 }
172 }
173 // vim:sw=4:ts=4:sts=4:et: