PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.64
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.64
1.2.74 1.2.73 1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 All 174 releases
userswp / vendor / composer / InstalledVersions.php

InstalledVersions.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.64, at vendor/composer/InstalledVersions.php

381 lines 16.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Composer.
5 *
6 * (c) Nils Adermann <naderman@naderman.de>
7 * Jordi Boggiano <j.boggiano@seld.be>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Composer;
14
15 use Composer\Autoload\ClassLoader;
16 use Composer\Semver\VersionParser;
17
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 */
27 class InstalledVersions
28 {
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;
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;
40
41 /**
42 * @var bool|null
43 */
44 private static $canGetVendors;
45
46 /**
47 * @var array[]
48 * @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[]}>}>
49 */
50 private static $installedByVendor = array();
51
52 /**
53 * Returns a list of all package names which are present, either by being installed, replaced or provided
54 *
55 * @return string[]
56 * @psalm-return list<string>
57 */
58 public static function getInstalledPackages()
59 {
60 $packages = array();
61 foreach (self::getInstalled() as $installed) {
62 $packages[] = array_keys($installed['versions']);
63 }
64
65 if (1 === \count($packages)) {
66 return $packages[0];
67 }
68
69 return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
70 }
71
72 /**
73 * Returns a list of all package names with a specific type e.g. 'library'
74 *
75 * @param string $type
76 * @return string[]
77 * @psalm-return list<string>
78 */
79 public static function getInstalledPackagesByType($type)
80 {
81 $packagesByType = array();
82
83 foreach (self::getInstalled() as $installed) {
84 foreach ($installed['versions'] as $name => $package) {
85 if (isset($package['type']) && $package['type'] === $type) {
86 $packagesByType[] = $name;
87 }
88 }
89 }
90
91 return $packagesByType;
92 }
93
94 /**
95 * Checks whether the given package is installed
96 *
97 * This also returns true if the package name is provided or replaced by another package
98 *
99 * @param string $packageName
100 * @param bool $includeDevRequirements
101 * @return bool
102 */
103 public static function isInstalled($packageName, $includeDevRequirements = true)
104 {
105 foreach (self::getInstalled() as $installed) {
106 if (isset($installed['versions'][$packageName])) {
107 return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false;
108 }
109 }
110
111 return false;
112 }
113
114 /**
115 * Checks whether the given package satisfies a version constraint
116 *
117 * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
118 *
119 * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
120 *
121 * @param VersionParser $parser Install composer/semver to have access to this class and functionality
122 * @param string $packageName
123 * @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
124 * @return bool
125 */
126 public static function satisfies(VersionParser $parser, $packageName, $constraint)
127 {
128 $constraint = $parser->parseConstraints((string) $constraint);
129 $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
130
131 return $provided->matches($constraint);
132 }
133
134 /**
135 * Returns a version constraint representing all the range(s) which are installed for a given package
136 *
137 * It is easier to use this via isInstalled() with the $constraint argument if you need to check
138 * whether a given version of a package is installed, and not just whether it exists
139 *
140 * @param string $packageName
141 * @return string Version constraint usable with composer/semver
142 */
143 public static function getVersionRanges($packageName)
144 {
145 foreach (self::getInstalled() as $installed) {
146 if (!isset($installed['versions'][$packageName])) {
147 continue;
148 }
149
150 $ranges = array();
151 if (isset($installed['versions'][$packageName]['pretty_version'])) {
152 $ranges[] = $installed['versions'][$packageName]['pretty_version'];
153 }
154 if (array_key_exists('aliases', $installed['versions'][$packageName])) {
155 $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
156 }
157 if (array_key_exists('replaced', $installed['versions'][$packageName])) {
158 $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
159 }
160 if (array_key_exists('provided', $installed['versions'][$packageName])) {
161 $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
162 }
163
164 return implode(' || ', $ranges);
165 }
166
167 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
168 }
169
170 /**
171 * @param string $packageName
172 * @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
173 */
174 public static function getVersion($packageName)
175 {
176 foreach (self::getInstalled() as $installed) {
177 if (!isset($installed['versions'][$packageName])) {
178 continue;
179 }
180
181 if (!isset($installed['versions'][$packageName]['version'])) {
182 return null;
183 }
184
185 return $installed['versions'][$packageName]['version'];
186 }
187
188 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
189 }
190
191 /**
192 * @param string $packageName
193 * @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
194 */
195 public static function getPrettyVersion($packageName)
196 {
197 foreach (self::getInstalled() as $installed) {
198 if (!isset($installed['versions'][$packageName])) {
199 continue;
200 }
201
202 if (!isset($installed['versions'][$packageName]['pretty_version'])) {
203 return null;
204 }
205
206 return $installed['versions'][$packageName]['pretty_version'];
207 }
208
209 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
210 }
211
212 /**
213 * @param string $packageName
214 * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
215 */
216 public static function getReference($packageName)
217 {
218 foreach (self::getInstalled() as $installed) {
219 if (!isset($installed['versions'][$packageName])) {
220 continue;
221 }
222
223 if (!isset($installed['versions'][$packageName]['reference'])) {
224 return null;
225 }
226
227 return $installed['versions'][$packageName]['reference'];
228 }
229
230 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
231 }
232
233 /**
234 * @param string $packageName
235 * @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.
236 */
237 public static function getInstallPath($packageName)
238 {
239 foreach (self::getInstalled() as $installed) {
240 if (!isset($installed['versions'][$packageName])) {
241 continue;
242 }
243
244 return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
245 }
246
247 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
248 }
249
250 /**
251 * @return array
252 * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
253 */
254 public static function getRootPackage()
255 {
256 $installed = self::getInstalled();
257
258 return $installed[0]['root'];
259 }
260
261 /**
262 * Returns the raw installed.php data for custom implementations
263 *
264 * @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.
265 * @return array[]
266 * @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[]}>}
267 */
268 public static function getRawData()
269 {
270 @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);
271
272 if (null === self::$installed) {
273 // only require the installed.php file if this file is loaded from its dumped location,
274 // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
275 if (substr(__DIR__, -8, 1) !== 'C') {
276 self::$installed = include __DIR__ . '/installed.php';
277 } else {
278 self::$installed = array();
279 }
280 }
281
282 return self::$installed;
283 }
284
285 /**
286 * Returns the raw data of all installed.php which are currently loaded for custom implementations
287 *
288 * @return array[]
289 * @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[]}>}>
290 */
291 public static function getAllRawData()
292 {
293 return self::getInstalled();
294 }
295
296 /**
297 * Lets you reload the static array from another file
298 *
299 * This is only useful for complex integrations in which a project needs to use
300 * this class but then also needs to execute another project's autoloader in process,
301 * and wants to ensure both projects have access to their version of installed.php.
302 *
303 * A typical case would be PHPUnit, where it would need to make sure it reads all
304 * the data it needs from this class, then call reload() with
305 * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
306 * the project in which it runs can then also use this class safely, without
307 * interference between PHPUnit's dependencies and the project's dependencies.
308 *
309 * @param array[] $data A vendor/composer/installed.php data set
310 * @return void
311 *
312 * @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
313 */
314 public static function reload($data)
315 {
316 self::$installed = $data;
317 self::$installedByVendor = array();
318 }
319
320 /**
321 * @return string
322 */
323 private static function getSelfDir()
324 {
325 if (self::$selfDir === null) {
326 self::$selfDir = strtr(__DIR__, '\\', '/');
327 }
328
329 return self::$selfDir;
330 }
331
332 /**
333 * @return array[]
334 * @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[]}>}>
335 */
336 private static function getInstalled()
337 {
338 if (null === self::$canGetVendors) {
339 self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
340 }
341
342 $installed = array();
343 $copiedLocalDir = false;
344
345 if (self::$canGetVendors) {
346 foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
347 if (isset(self::$installedByVendor[$vendorDir])) {
348 $installed[] = self::$installedByVendor[$vendorDir];
349 } elseif (is_file($vendorDir.'/composer/installed.php')) {
350 /** @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 */
351 $required = require $vendorDir.'/composer/installed.php';
352 self::$installedByVendor[$vendorDir] = $required;
353 $installed[] = $required;
354 if (strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
355 self::$installed = $required;
356 $copiedLocalDir = true;
357 }
358 }
359 }
360 }
361
362 if (null === self::$installed) {
363 // only require the installed.php file if this file is loaded from its dumped location,
364 // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
365 if (substr(__DIR__, -8, 1) !== 'C') {
366 /** @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 */
367 $required = require __DIR__ . '/installed.php';
368 self::$installed = $required;
369 } else {
370 self::$installed = array();
371 }
372 }
373
374 if (self::$installed !== array() && !$copiedLocalDir) {
375 $installed[] = self::$installed;
376 }
377
378 return $installed;
379 }
380 }
381