source: trunk/client/inc/hpdf5/composer/InstalledVersions.php@ 400

Last change on this file since 400 was 347, checked in by roby, 4 years ago

Aggiornamento per compatibilità con php7.4

File size: 10.2 KB
Line 
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
13namespace Composer;
14
15use Composer\Autoload\ClassLoader;
16use Composer\Semver\VersionParser;
17
18/**
19 * This class is copied in every Composer installed project and available to all
20 *
21 * To require it's presence, you can require `composer-runtime-api ^2.0`
22 */
23class InstalledVersions
24{
25 private static $installed = array (
26 'root' =>
27 array (
28 'pretty_version' => '1.0.0+no-version-set',
29 'version' => '1.0.0.0',
30 'aliases' =>
31 array (
32 ),
33 'reference' => NULL,
34 'name' => '__root__',
35 ),
36 'versions' =>
37 array (
38 '__root__' =>
39 array (
40 'pretty_version' => '1.0.0+no-version-set',
41 'version' => '1.0.0.0',
42 'aliases' =>
43 array (
44 ),
45 'reference' => NULL,
46 ),
47 'spipu/html2pdf' =>
48 array (
49 'pretty_version' => 'v5.2.2',
50 'version' => '5.2.2.0',
51 'aliases' =>
52 array (
53 ),
54 'reference' => 'e6d8ca22347b6691bb8c2652212b1be2c89b3eff',
55 ),
56 'tecnickcom/tcpdf' =>
57 array (
58 'pretty_version' => '6.4.1',
59 'version' => '6.4.1.0',
60 'aliases' =>
61 array (
62 ),
63 'reference' => '5ba838befdb37ef06a16d9f716f35eb03cb1b329',
64 ),
65 ),
66);
67 private static $canGetVendors;
68 private static $installedByVendor = array();
69
70 /**
71 * Returns a list of all package names which are present, either by being installed, replaced or provided
72 *
73 * @return string[]
74 * @psalm-return list<string>
75 */
76 public static function getInstalledPackages()
77 {
78 $packages = array();
79 foreach (self::getInstalled() as $installed) {
80 $packages[] = array_keys($installed['versions']);
81 }
82
83
84 if (1 === \count($packages)) {
85 return $packages[0];
86 }
87
88 return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
89 }
90
91 /**
92 * Checks whether the given package is installed
93 *
94 * This also returns true if the package name is provided or replaced by another package
95 *
96 * @param string $packageName
97 * @return bool
98 */
99 public static function isInstalled($packageName)
100 {
101 foreach (self::getInstalled() as $installed) {
102 if (isset($installed['versions'][$packageName])) {
103 return true;
104 }
105 }
106
107 return false;
108 }
109
110 /**
111 * Checks whether the given package satisfies a version constraint
112 *
113 * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
114 *
115 * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
116 *
117 * @param VersionParser $parser Install composer/semver to have access to this class and functionality
118 * @param string $packageName
119 * @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
120 *
121 * @return bool
122 */
123 public static function satisfies(VersionParser $parser, $packageName, $constraint)
124 {
125 $constraint = $parser->parseConstraints($constraint);
126 $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
127
128 return $provided->matches($constraint);
129 }
130
131 /**
132 * Returns a version constraint representing all the range(s) which are installed for a given package
133 *
134 * It is easier to use this via isInstalled() with the $constraint argument if you need to check
135 * whether a given version of a package is installed, and not just whether it exists
136 *
137 * @param string $packageName
138 * @return string Version constraint usable with composer/semver
139 */
140 public static function getVersionRanges($packageName)
141 {
142 foreach (self::getInstalled() as $installed) {
143 if (!isset($installed['versions'][$packageName])) {
144 continue;
145 }
146
147 $ranges = array();
148 if (isset($installed['versions'][$packageName]['pretty_version'])) {
149 $ranges[] = $installed['versions'][$packageName]['pretty_version'];
150 }
151 if (array_key_exists('aliases', $installed['versions'][$packageName])) {
152 $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
153 }
154 if (array_key_exists('replaced', $installed['versions'][$packageName])) {
155 $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
156 }
157 if (array_key_exists('provided', $installed['versions'][$packageName])) {
158 $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
159 }
160
161 return implode(' || ', $ranges);
162 }
163
164 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
165 }
166
167 /**
168 * @param string $packageName
169 * @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
170 */
171 public static function getVersion($packageName)
172 {
173 foreach (self::getInstalled() as $installed) {
174 if (!isset($installed['versions'][$packageName])) {
175 continue;
176 }
177
178 if (!isset($installed['versions'][$packageName]['version'])) {
179 return null;
180 }
181
182 return $installed['versions'][$packageName]['version'];
183 }
184
185 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
186 }
187
188 /**
189 * @param string $packageName
190 * @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
191 */
192 public static function getPrettyVersion($packageName)
193 {
194 foreach (self::getInstalled() as $installed) {
195 if (!isset($installed['versions'][$packageName])) {
196 continue;
197 }
198
199 if (!isset($installed['versions'][$packageName]['pretty_version'])) {
200 return null;
201 }
202
203 return $installed['versions'][$packageName]['pretty_version'];
204 }
205
206 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
207 }
208
209 /**
210 * @param string $packageName
211 * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
212 */
213 public static function getReference($packageName)
214 {
215 foreach (self::getInstalled() as $installed) {
216 if (!isset($installed['versions'][$packageName])) {
217 continue;
218 }
219
220 if (!isset($installed['versions'][$packageName]['reference'])) {
221 return null;
222 }
223
224 return $installed['versions'][$packageName]['reference'];
225 }
226
227 throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
228 }
229
230 /**
231 * @return array
232 * @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}
233 */
234 public static function getRootPackage()
235 {
236 $installed = self::getInstalled();
237
238 return $installed[0]['root'];
239 }
240
241 /**
242 * Returns the raw installed.php data for custom implementations
243 *
244 * @return array[]
245 * @psalm-return array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}, versions: list<string, array{pretty_version: ?string, version: ?string, aliases: ?string[], reference: ?string, replaced: ?string[], provided: ?string[]}>}
246 */
247 public static function getRawData()
248 {
249 return self::$installed;
250 }
251
252 /**
253 * Lets you reload the static array from another file
254 *
255 * This is only useful for complex integrations in which a project needs to use
256 * this class but then also needs to execute another project's autoloader in process,
257 * and wants to ensure both projects have access to their version of installed.php.
258 *
259 * A typical case would be PHPUnit, where it would need to make sure it reads all
260 * the data it needs from this class, then call reload() with
261 * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
262 * the project in which it runs can then also use this class safely, without
263 * interference between PHPUnit's dependencies and the project's dependencies.
264 *
265 * @param array[] $data A vendor/composer/installed.php data set
266 * @return void
267 *
268 * @psalm-param array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}, versions: list<string, array{pretty_version: ?string, version: ?string, aliases: ?string[], reference: ?string, replaced: ?string[], provided: ?string[]}>} $data
269 */
270 public static function reload($data)
271 {
272 self::$installed = $data;
273 self::$installedByVendor = array();
274 }
275
276 /**
277 * @return array[]
278 */
279 private static function getInstalled()
280 {
281 if (null === self::$canGetVendors) {
282 self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
283 }
284
285 $installed = array();
286
287 if (self::$canGetVendors) {
288 // @phpstan-ignore-next-line
289 foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
290 if (isset(self::$installedByVendor[$vendorDir])) {
291 $installed[] = self::$installedByVendor[$vendorDir];
292 } elseif (is_file($vendorDir.'/composer/installed.php')) {
293 $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
294 }
295 }
296 }
297
298 $installed[] = self::$installed;
299
300 return $installed;
301 }
302}
Note: See TracBrowser for help on using the repository browser.