PluginProbe
Polylang / 3.8.10
Polylang v3.8.10
3.8.10 3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 All 234 releases
← All changes | vendor/composer/InstalledVersions.php +337 -479 2.9.1 → 3.8.10 View file →
@@ -1,538 +1,396 @@
1 1 <?php
2 2
3 +/*
4 + * This file is part of Composer.
5 + *
6 + * (c) Nils Adermann <[email protected]>
7 + * Jordi Boggiano <[email protected]>
8 + *
9 + * For the full copyright and license information, please view the LICENSE
10 + * file that was distributed with this source code.
11 + */
3 12
4 -
5 -
6 -
7 -
8 -
9 -
10 -
11 -
12 -
13 13 namespace Composer;
14 14
15 +use Composer\Autoload\ClassLoader;
15 16 use Composer\Semver\VersionParser;
16 17
17 -
18 -
19 -
20 -
21 -
18 +/**
19 + * This class is copied in every Composer installed project and available to all
20 + *
21 + * See also https://getcomposer.org/doc/07-runtime.md#installed-versions
22 + *
23 + * To require its presence, you can require `composer-runtime-api ^2.0`
24 + *
25 + * @final
26 + */
22 27 class InstalledVersions
23 28 {
24 -private static $installed = array (
25 - 'root' =>
26 - array (
27 - 'pretty_version' => 'dev-master',
28 - 'version' => 'dev-master',
29 - 'aliases' =>
30 - array (
31 - ),
32 - 'reference' => '421ec30d3ae8380eb66da357d813d2eabc1b8875',
33 - 'name' => 'wpsyntex/polylang',
34 - ),
35 - 'versions' =>
36 - array (
37 - 'automattic/vipwpcs' =>
38 - array (
39 - 'pretty_version' => '2.2.0',
40 - 'version' => '2.2.0.0',
41 - 'aliases' =>
42 - array (
43 - ),
44 - 'reference' => '4d0612461232b313d06321f1501c3989bd6aecf9',
45 - ),
46 - 'dealerdirect/phpcodesniffer-composer-installer' =>
47 - array (
48 - 'pretty_version' => 'v0.7.1',
49 - 'version' => '0.7.1.0',
50 - 'aliases' =>
51 - array (
52 - ),
53 - 'reference' => 'fe390591e0241955f22eb9ba327d137e501c771c',
54 - ),
55 - 'doctrine/instantiator' =>
56 - array (
57 - 'pretty_version' => '1.4.0',
58 - 'version' => '1.4.0.0',
59 - 'aliases' =>
60 - array (
61 - ),
62 - 'reference' => 'd56bf6102915de5702778fe20f2de3b2fe570b5b',
63 - ),
64 - 'myclabs/deep-copy' =>
65 - array (
66 - 'pretty_version' => '1.10.2',
67 - 'version' => '1.10.2.0',
68 - 'aliases' =>
69 - array (
70 - ),
71 - 'reference' => '776f831124e9c62e1a2c601ecc52e776d8bb7220',
72 - 'replaced' =>
73 - array (
74 - 0 => '1.10.2',
75 - ),
76 - ),
77 - 'phpcompatibility/php-compatibility' =>
78 - array (
79 - 'pretty_version' => '9.3.5',
80 - 'version' => '9.3.5.0',
81 - 'aliases' =>
82 - array (
83 - ),
84 - 'reference' => '9fb324479acf6f39452e0655d2429cc0d3914243',
85 - ),
86 - 'phpcompatibility/phpcompatibility-paragonie' =>
87 - array (
88 - 'pretty_version' => '1.3.0',
89 - 'version' => '1.3.0.0',
90 - 'aliases' =>
91 - array (
92 - ),
93 - 'reference' => 'b862bc32f7e860d0b164b199bd995e690b4b191c',
94 - ),
95 - 'phpcompatibility/phpcompatibility-wp' =>
96 - array (
97 - 'pretty_version' => '2.1.0',
98 - 'version' => '2.1.0.0',
99 - 'aliases' =>
100 - array (
101 - ),
102 - 'reference' => '41bef18ba688af638b7310666db28e1ea9158b2f',
103 - ),
104 - 'phpdocumentor/reflection-common' =>
105 - array (
106 - 'pretty_version' => '2.1.0',
107 - 'version' => '2.1.0.0',
108 - 'aliases' =>
109 - array (
110 - ),
111 - 'reference' => '6568f4687e5b41b054365f9ae03fcb1ed5f2069b',
112 - ),
113 - 'phpdocumentor/reflection-docblock' =>
114 - array (
115 - 'pretty_version' => '4.3.4',
116 - 'version' => '4.3.4.0',
117 - 'aliases' =>
118 - array (
119 - ),
120 - 'reference' => 'da3fd972d6bafd628114f7e7e036f45944b62e9c',
121 - ),
122 - 'phpdocumentor/type-resolver' =>
123 - array (
124 - 'pretty_version' => '1.0.1',
125 - 'version' => '1.0.1.0',
126 - 'aliases' =>
127 - array (
128 - ),
129 - 'reference' => '2e32a6d48972b2c1976ed5d8967145b6cec4a4a9',
130 - ),
131 - 'phpspec/prophecy' =>
132 - array (
133 - 'pretty_version' => 'v1.10.3',
134 - 'version' => '1.10.3.0',
135 - 'aliases' =>
136 - array (
137 - ),
138 - 'reference' => '451c3cd1418cf640de218914901e51b064abb093',
139 - ),
140 - 'phpunit/php-code-coverage' =>
141 - array (
142 - 'pretty_version' => '4.0.8',
143 - 'version' => '4.0.8.0',
144 - 'aliases' =>
145 - array (
146 - ),
147 - 'reference' => 'ef7b2f56815df854e66ceaee8ebe9393ae36a40d',
148 - ),
149 - 'phpunit/php-file-iterator' =>
150 - array (
151 - 'pretty_version' => '1.4.5',
152 - 'version' => '1.4.5.0',
153 - 'aliases' =>
154 - array (
155 - ),
156 - 'reference' => '730b01bc3e867237eaac355e06a36b85dd93a8b4',
157 - ),
158 - 'phpunit/php-text-template' =>
159 - array (
160 - 'pretty_version' => '1.2.1',
161 - 'version' => '1.2.1.0',
162 - 'aliases' =>
163 - array (
164 - ),
165 - 'reference' => '31f8b717e51d9a2afca6c9f046f5d69fc27c8686',
166 - ),
167 - 'phpunit/php-timer' =>
168 - array (
169 - 'pretty_version' => '1.0.9',
170 - 'version' => '1.0.9.0',
171 - 'aliases' =>
172 - array (
173 - ),
174 - 'reference' => '3dcf38ca72b158baf0bc245e9184d3fdffa9c46f',
175 - ),
176 - 'phpunit/php-token-stream' =>
177 - array (
178 - 'pretty_version' => '2.0.2',
179 - 'version' => '2.0.2.0',
180 - 'aliases' =>
181 - array (
182 - ),
183 - 'reference' => '791198a2c6254db10131eecfe8c06670700904db',
184 - ),
185 - 'phpunit/phpunit' =>
186 - array (
187 - 'pretty_version' => '5.7.27',
188 - 'version' => '5.7.27.0',
189 - 'aliases' =>
190 - array (
191 - ),
192 - 'reference' => 'b7803aeca3ccb99ad0a506fa80b64cd6a56bbc0c',
193 - ),
194 - 'phpunit/phpunit-mock-objects' =>
195 - array (
196 - 'pretty_version' => '3.4.4',
197 - 'version' => '3.4.4.0',
198 - 'aliases' =>
199 - array (
200 - ),
201 - 'reference' => 'a23b761686d50a560cc56233b9ecf49597cc9118',
202 - ),
203 - 'sebastian/code-unit-reverse-lookup' =>
204 - array (
205 - 'pretty_version' => '1.0.2',
206 - 'version' => '1.0.2.0',
207 - 'aliases' =>
208 - array (
209 - ),
210 - 'reference' => '1de8cd5c010cb153fcd68b8d0f64606f523f7619',
211 - ),
212 - 'sebastian/comparator' =>
213 - array (
214 - 'pretty_version' => '1.2.4',
215 - 'version' => '1.2.4.0',
216 - 'aliases' =>
217 - array (
218 - ),
219 - 'reference' => '2b7424b55f5047b47ac6e5ccb20b2aea4011d9be',
220 - ),
221 - 'sebastian/diff' =>
222 - array (
223 - 'pretty_version' => '1.4.3',
224 - 'version' => '1.4.3.0',
225 - 'aliases' =>
226 - array (
227 - ),
228 - 'reference' => '7f066a26a962dbe58ddea9f72a4e82874a3975a4',
229 - ),
230 - 'sebastian/environment' =>
231 - array (
232 - 'pretty_version' => '2.0.0',
233 - 'version' => '2.0.0.0',
234 - 'aliases' =>
235 - array (
236 - ),
237 - 'reference' => '5795ffe5dc5b02460c3e34222fee8cbe245d8fac',
238 - ),
239 - 'sebastian/exporter' =>
240 - array (
241 - 'pretty_version' => '2.0.0',
242 - 'version' => '2.0.0.0',
243 - 'aliases' =>
244 - array (
245 - ),
246 - 'reference' => 'ce474bdd1a34744d7ac5d6aad3a46d48d9bac4c4',
247 - ),
248 - 'sebastian/global-state' =>
249 - array (
250 - 'pretty_version' => '1.1.1',
251 - 'version' => '1.1.1.0',
252 - 'aliases' =>
253 - array (
254 - ),
255 - 'reference' => 'bc37d50fea7d017d3d340f230811c9f1d7280af4',
256 - ),
257 - 'sebastian/object-enumerator' =>
258 - array (
259 - 'pretty_version' => '2.0.1',
260 - 'version' => '2.0.1.0',
261 - 'aliases' =>
262 - array (
263 - ),
264 - 'reference' => '1311872ac850040a79c3c058bea3e22d0f09cbb7',
265 - ),
266 - 'sebastian/recursion-context' =>
267 - array (
268 - 'pretty_version' => '2.0.0',
269 - 'version' => '2.0.0.0',
270 - 'aliases' =>
271 - array (
272 - ),
273 - 'reference' => '2c3ba150cbec723aa057506e73a8d33bdb286c9a',
274 - ),
275 - 'sebastian/resource-operations' =>
276 - array (
277 - 'pretty_version' => '1.0.0',
278 - 'version' => '1.0.0.0',
279 - 'aliases' =>
280 - array (
281 - ),
282 - 'reference' => 'ce990bb21759f94aeafd30209e8cfcdfa8bc3f52',
283 - ),
284 - 'sebastian/version' =>
285 - array (
286 - 'pretty_version' => '2.0.1',
287 - 'version' => '2.0.1.0',
288 - 'aliases' =>
289 - array (
290 - ),
291 - 'reference' => '99732be0ddb3361e16ad77b68ba41efc8e979019',
292 - ),
293 - 'sirbrillig/phpcs-variable-analysis' =>
294 - array (
295 - 'pretty_version' => 'v2.10.1',
296 - 'version' => '2.10.1.0',
297 - 'aliases' =>
298 - array (
299 - ),
300 - 'reference' => 'c6716a98fe7bee25d31306e14fb62c3ffa16d70a',
301 - ),
302 - 'squizlabs/php_codesniffer' =>
303 - array (
304 - 'pretty_version' => '3.5.8',
305 - 'version' => '3.5.8.0',
306 - 'aliases' =>
307 - array (
308 - ),
309 - 'reference' => '9d583721a7157ee997f235f327de038e7ea6dac4',
310 - ),
311 - 'symfony/polyfill-ctype' =>
312 - array (
313 - 'pretty_version' => 'v1.20.0',
314 - 'version' => '1.20.0.0',
315 - 'aliases' =>
316 - array (
317 - ),
318 - 'reference' => 'f4ba089a5b6366e453971d3aad5fe8e897b37f41',
319 - ),
320 - 'symfony/yaml' =>
321 - array (
322 - 'pretty_version' => 'v4.4.17',
323 - 'version' => '4.4.17.0',
324 - 'aliases' =>
325 - array (
326 - ),
327 - 'reference' => '7531361cf38e4816821b4a12a42542b3c6143ad1',
328 - ),
329 - 'webmozart/assert' =>
330 - array (
331 - 'pretty_version' => '1.9.1',
332 - 'version' => '1.9.1.0',
333 - 'aliases' =>
334 - array (
335 - ),
336 - 'reference' => 'bafc69caeb4d49c39fd0779086c03a3738cbb389',
337 - ),
338 - 'wp-coding-standards/wpcs' =>
339 - array (
340 - 'pretty_version' => '2.3.0',
341 - 'version' => '2.3.0.0',
342 - 'aliases' =>
343 - array (
344 - ),
345 - 'reference' => '7da1894633f168fe244afc6de00d141f27517b62',
346 - ),
347 - 'wpsyntex/polylang' =>
348 - array (
349 - 'pretty_version' => 'dev-master',
350 - 'version' => 'dev-master',
351 - 'aliases' =>
352 - array (
353 - ),
354 - 'reference' => '421ec30d3ae8380eb66da357d813d2eabc1b8875',
355 - ),
356 - 'wpsyntex/wp-phpunit' =>
357 - array (
358 - 'pretty_version' => 'dev-branch-5.7',
359 - 'version' => 'dev-branch-5.7',
360 - 'aliases' =>
361 - array (
362 - ),
363 - 'reference' => '1928b00425f8393b80c5d603428cca7c4001b480',
364 - ),
365 - ),
366 -);
29 + /**
30 + * @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to
31 + * @internal
32 + */
33 + private static $selfDir = null;
367 34
35 + /**
36 + * @var mixed[]|null
37 + * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
38 + */
39 + private static $installed;
368 40
41 + /**
42 + * @var bool
43 + */
44 + private static $installedIsLocalDir;
369 45
46 + /**
47 + * @var bool|null
48 + */
49 + private static $canGetVendors;
370 50
51 + /**
52 + * @var array[]
53 + * @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
54 + */
55 + private static $installedByVendor = array();
371 56
57 + /**
58 + * Returns a list of all package names which are present, either by being installed, replaced or provided
59 + *
60 + * @return string[]
61 + * @psalm-return list<string>
62 + */
63 + public static function getInstalledPackages()
64 + {
65 + $packages = array();
66 + foreach (self::getInstalled() as $installed) {
67 + $packages[] = array_keys($installed['versions']);
68 + }
372 69
70 + if (1 === \count($packages)) {
71 + return $packages[0];
72 + }
373 73
374 -public static function getInstalledPackages()
375 -{
376 -return array_keys(self::$installed['versions']);
377 -}
74 + return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
75 + }
378 76
77 + /**
78 + * Returns a list of all package names with a specific type e.g. 'library'
79 + *
80 + * @param string $type
81 + * @return string[]
82 + * @psalm-return list<string>
83 + */
84 + public static function getInstalledPackagesByType($type)
85 + {
86 + $packagesByType = array();
379 87
88 + foreach (self::getInstalled() as $installed) {
89 + foreach ($installed['versions'] as $name => $package) {
90 + if (isset($package['type']) && $package['type'] === $type) {
91 + $packagesByType[] = $name;
92 + }
93 + }
94 + }
380 95
96 + return $packagesByType;
97 + }
381 98
99 + /**
100 + * Checks whether the given package is installed
101 + *
102 + * This also returns true if the package name is provided or replaced by another package
103 + *
104 + * @param string $packageName
105 + * @param bool $includeDevRequirements
106 + * @return bool
107 + */
108 + public static function isInstalled($packageName, $includeDevRequirements = true)
109 + {
110 + foreach (self::getInstalled() as $installed) {
111 + if (isset($installed['versions'][$packageName])) {
112 + return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false;
113 + }
114 + }
382 115
116 + return false;
117 + }
383 118
119 + /**
120 + * Checks whether the given package satisfies a version constraint
121 + *
122 + * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
123 + *
124 + * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
125 + *
126 + * @param VersionParser $parser Install composer/semver to have access to this class and functionality
127 + * @param string $packageName
128 + * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
129 + * @return bool
130 + */
131 + public static function satisfies(VersionParser $parser, $packageName, $constraint)
132 + {
133 + $constraint = $parser->parseConstraints((string) $constraint);
134 + $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
384 135
136 + return $provided->matches($constraint);
137 + }
385 138
139 + /**
140 + * Returns a version constraint representing all the range(s) which are installed for a given package
141 + *
142 + * It is easier to use this via isInstalled() with the $constraint argument if you need to check
143 + * whether a given version of a package is installed, and not just whether it exists
144 + *
145 + * @param string $packageName
146 + * @return string Version constraint usable with composer/semver
147 + */
148 + public static function getVersionRanges($packageName)
149 + {
150 + foreach (self::getInstalled() as $installed) {
151 + if (!isset($installed['versions'][$packageName])) {
152 + continue;
153 + }
386 154
387 -public static function isInstalled($packageName)
388 -{
389 -return isset(self::$installed['versions'][$packageName]);
390 -}
155 + $ranges = array();
156 + if (isset($installed['versions'][$packageName]['pretty_version'])) {
157 + $ranges[] = $installed['versions'][$packageName]['pretty_version'];
158 + }
159 + if (array_key_exists('aliases', $installed['versions'][$packageName])) {
160 + $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
161 + }
162 + if (array_key_exists('replaced', $installed['versions'][$packageName])) {
163 + $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
164 + }
165 + if (array_key_exists('provided', $installed['versions'][$packageName])) {
166 + $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
167 + }
391 168
169 + return implode(' || ', $ranges);
170 + }
392 171
172 + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
173 + }
393 174
175 + /**
176 + * @param string $packageName
177 + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
178 + */
179 + public static function getVersion($packageName)
180 + {
181 + foreach (self::getInstalled() as $installed) {
182 + if (!isset($installed['versions'][$packageName])) {
183 + continue;
184 + }
394 185
186 + if (!isset($installed['versions'][$packageName]['version'])) {
187 + return null;
188 + }
395 189
190 + return $installed['versions'][$packageName]['version'];
191 + }
396 192
193 + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
194 + }
397 195
196 + /**
197 + * @param string $packageName
198 + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
199 + */
200 + public static function getPrettyVersion($packageName)
201 + {
202 + foreach (self::getInstalled() as $installed) {
203 + if (!isset($installed['versions'][$packageName])) {
204 + continue;
205 + }
398 206
207 + if (!isset($installed['versions'][$packageName]['pretty_version'])) {
208 + return null;
209 + }
399 210
211 + return $installed['versions'][$packageName]['pretty_version'];
212 + }
400 213
214 + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
215 + }
401 216
217 + /**
218 + * @param string $packageName
219 + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
220 + */
221 + public static function getReference($packageName)
222 + {
223 + foreach (self::getInstalled() as $installed) {
224 + if (!isset($installed['versions'][$packageName])) {
225 + continue;
226 + }
402 227
228 + if (!isset($installed['versions'][$packageName]['reference'])) {
229 + return null;
230 + }
403 231
232 + return $installed['versions'][$packageName]['reference'];
233 + }
404 234
405 -public static function satisfies(VersionParser $parser, $packageName, $constraint)
406 -{
407 -$constraint = $parser->parseConstraints($constraint);
408 -$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
235 + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
236 + }
409 237
410 -return $provided->matches($constraint);
411 -}
238 + /**
239 + * @param string $packageName
240 + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
241 + */
242 + public static function getInstallPath($packageName)
243 + {
244 + foreach (self::getInstalled() as $installed) {
245 + if (!isset($installed['versions'][$packageName])) {
246 + continue;
247 + }
412 248
249 + return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
250 + }
413 251
252 + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
253 + }
414 254
255 + /**
256 + * @return array
257 + * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
258 + */
259 + public static function getRootPackage()
260 + {
261 + $installed = self::getInstalled();
415 262
263 + return $installed[0]['root'];
264 + }
416 265
266 + /**
267 + * Returns the raw installed.php data for custom implementations
268 + *
269 + * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
270 + * @return array[]
271 + * @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}
272 + */
273 + public static function getRawData()
274 + {
275 + @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
417 276
277 + if (null === self::$installed) {
278 + // only require the installed.php file if this file is loaded from its dumped location,
279 + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
280 + if (substr(__DIR__, -8, 1) !== 'C') {
281 + self::$installed = include __DIR__ . '/installed.php';
282 + } else {
283 + self::$installed = array();
284 + }
285 + }
418 286
287 + return self::$installed;
288 + }
419 289
290 + /**
291 + * Returns the raw data of all installed.php which are currently loaded for custom implementations
292 + *
293 + * @return array[]
294 + * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
295 + */
296 + public static function getAllRawData()
297 + {
298 + return self::getInstalled();
299 + }
420 300
301 + /**
302 + * Lets you reload the static array from another file
303 + *
304 + * This is only useful for complex integrations in which a project needs to use
305 + * this class but then also needs to execute another project's autoloader in process,
306 + * and wants to ensure both projects have access to their version of installed.php.
307 + *
308 + * A typical case would be PHPUnit, where it would need to make sure it reads all
309 + * the data it needs from this class, then call reload() with
310 + * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
311 + * the project in which it runs can then also use this class safely, without
312 + * interference between PHPUnit's dependencies and the project's dependencies.
313 + *
314 + * @param array[] $data A vendor/composer/installed.php data set
315 + * @return void
316 + *
317 + * @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data
318 + */
319 + public static function reload($data)
320 + {
321 + self::$installed = $data;
322 + self::$installedByVendor = array();
421 323
422 -public static function getVersionRanges($packageName)
423 -{
424 -if (!isset(self::$installed['versions'][$packageName])) {
425 -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
426 -}
324 + // when using reload, we disable the duplicate protection to ensure that self::$installed data is
325 + // always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not,
326 + // so we have to assume it does not, and that may result in duplicate data being returned when listing
327 + // all installed packages for example
328 + self::$installedIsLocalDir = false;
329 + }
427 330
428 -$ranges = array();
429 -if (isset(self::$installed['versions'][$packageName]['pretty_version'])) {
430 -$ranges[] = self::$installed['versions'][$packageName]['pretty_version'];
431 -}
432 -if (array_key_exists('aliases', self::$installed['versions'][$packageName])) {
433 -$ranges = array_merge($ranges, self::$installed['versions'][$packageName]['aliases']);
434 -}
435 -if (array_key_exists('replaced', self::$installed['versions'][$packageName])) {
436 -$ranges = array_merge($ranges, self::$installed['versions'][$packageName]['replaced']);
437 -}
438 -if (array_key_exists('provided', self::$installed['versions'][$packageName])) {
439 -$ranges = array_merge($ranges, self::$installed['versions'][$packageName]['provided']);
440 -}
331 + /**
332 + * @return string
333 + */
334 + private static function getSelfDir()
335 + {
336 + if (self::$selfDir === null) {
337 + self::$selfDir = strtr(__DIR__, '\\', '/');
338 + }
441 339
442 -return implode(' || ', $ranges);
443 -}
340 + return self::$selfDir;
341 + }
444 342
343 + /**
344 + * @return array[]
345 + * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
346 + */
347 + private static function getInstalled()
348 + {
349 + if (null === self::$canGetVendors) {
350 + self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
351 + }
445 352
353 + $installed = array();
354 + $copiedLocalDir = false;
446 355
356 + if (self::$canGetVendors) {
357 + $selfDir = self::getSelfDir();
358 + foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
359 + $vendorDir = strtr($vendorDir, '\\', '/');
360 + if (isset(self::$installedByVendor[$vendorDir])) {
361 + $installed[] = self::$installedByVendor[$vendorDir];
362 + } elseif (is_file($vendorDir.'/composer/installed.php')) {
363 + /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
364 + $required = require $vendorDir.'/composer/installed.php';
365 + self::$installedByVendor[$vendorDir] = $required;
366 + $installed[] = $required;
367 + if (self::$installed === null && $vendorDir.'/composer' === $selfDir) {
368 + self::$installed = $required;
369 + self::$installedIsLocalDir = true;
370 + }
371 + }
372 + if (self::$installedIsLocalDir && $vendorDir.'/composer' === $selfDir) {
373 + $copiedLocalDir = true;
374 + }
375 + }
376 + }
447 377
378 + if (null === self::$installed) {
379 + // only require the installed.php file if this file is loaded from its dumped location,
380 + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
381 + if (substr(__DIR__, -8, 1) !== 'C') {
382 + /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
383 + $required = require __DIR__ . '/installed.php';
384 + self::$installed = $required;
385 + } else {
386 + self::$installed = array();
387 + }
388 + }
448 389
449 -public static function getVersion($packageName)
450 -{
451 -if (!isset(self::$installed['versions'][$packageName])) {
452 -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
453 -}
390 + if (self::$installed !== array() && !$copiedLocalDir) {
391 + $installed[] = self::$installed;
392 + }
454 393
455 -if (!isset(self::$installed['versions'][$packageName]['version'])) {
456 -return null;
457 -}
458 -
459 -return self::$installed['versions'][$packageName]['version'];
460 -}
461 -
462 -
463 -
464 -
465 -
466 -public static function getPrettyVersion($packageName)
467 -{
468 -if (!isset(self::$installed['versions'][$packageName])) {
469 -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
470 -}
471 -
472 -if (!isset(self::$installed['versions'][$packageName]['pretty_version'])) {
473 -return null;
474 -}
475 -
476 -return self::$installed['versions'][$packageName]['pretty_version'];
477 -}
478 -
479 -
480 -
481 -
482 -
483 -public static function getReference($packageName)
484 -{
485 -if (!isset(self::$installed['versions'][$packageName])) {
486 -throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
487 -}
488 -
489 -if (!isset(self::$installed['versions'][$packageName]['reference'])) {
490 -return null;
491 -}
492 -
493 -return self::$installed['versions'][$packageName]['reference'];
494 -}
495 -
496 -
497 -
498 -
499 -
500 -public static function getRootPackage()
501 -{
502 -return self::$installed['root'];
503 -}
504 -
505 -
506 -
507 -
508 -
509 -
510 -
511 -public static function getRawData()
512 -{
513 -return self::$installed;
514 -}
515 -
516 -
517 -
518 -
519 -
520 -
521 -
522 -
523 -
524 -
525 -
526 -
527 -
528 -
529 -
530 -
531 -
532 -
533 -
534 -public static function reload($data)
535 -{
536 -self::$installed = $data;
537 -}
394 + return $installed;
395 + }
538 396 }