]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/composer-merge-plugin/src/Merge/StabilityFlags.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / composer-merge-plugin / src / Merge / StabilityFlags.php
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 <bd808@bd808.com>
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     public function __construct(
44         array $stabilityFlags = array(),
45         $minimumStability = BasePackage::STABILITY_STABLE
46     ) {
47         $this->stabilityFlags = $stabilityFlags;
48         $this->minimumStability = $this->getStabilityInt($minimumStability);
49         $this->explicitStabilityRe = '/^[^@]*?@(' .
50             implode('|', array_keys(BasePackage::$stabilities)) .
51             ')$/i';
52     }
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     protected function getStabilityInt($name)
61     {
62         $name = VersionParser::normalizeStability($name);
63         return isset(BasePackage::$stabilities[$name]) ?
64             BasePackage::$stabilities[$name] :
65             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     public function extractAll(array $requires)
76     {
77         $flags = array();
78
79         foreach ($requires as $name => $link) {
80             $name = strtolower($name);
81             $version = $link->getPrettyConstraint();
82
83             $stability = $this->getExplicitStability($version);
84
85             if ($stability === null) {
86                 $stability = $this->getParsedStability($version);
87             }
88
89             $flags[$name] = max($stability, $this->getCurrentStability($name));
90         }
91
92         // Filter out null stability values
93         return array_filter($flags, function ($v) {
94             return $v !== null;
95         });
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     protected function getExplicitStability($version)
107     {
108         $found = null;
109         $constraints = $this->splitConstraints($version);
110         foreach ($constraints as $constraint) {
111             if (preg_match($this->explicitStabilityRe, $constraint, $match)) {
112                 $stability = $this->getStabilityInt($match[1]);
113                 $found = max($stability, $found);
114             }
115         }
116         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     protected function splitConstraints($version)
127     {
128         $found = array();
129         $orConstraints = preg_split('/\s*\|\|?\s*/', trim($version));
130         foreach ($orConstraints as $constraints) {
131             $andConstraints = preg_split(
132                 '/(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)/',
133                 $constraints
134             );
135             foreach ($andConstraints as $constraint) {
136                 $found[] = $constraint;
137             }
138         }
139         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     protected function getParsedStability($version)
150     {
151         // Drop aliasing if used
152         $version = preg_replace('/^([^,\s@]+) as .+$/', '$1', $version);
153         $stability = $this->getStabilityInt(
154             VersionParser::parseStability($version)
155         );
156
157         if ($stability === BasePackage::STABILITY_STABLE ||
158             $this->minimumStability > $stability
159         ) {
160             // Ignore if 'stable' or more stable than the global
161             // minimum
162             $stability = null;
163         }
164
165         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     protected function getCurrentStability($name)
176     {
177         return isset($this->stabilityFlags[$name]) ?
178             $this->stabilityFlags[$name] : null;
179     }
180 }
181 // vim:sw=4:ts=4:sts=4:et: