1<?php
2
3
4
5
6
7
8
9
10
11
12
13namespace Composer;
14
15use Composer\Autoload\ClassLoader;
16use Composer\Semver\VersionParser;
17
18
19
20
21
22
23
24
25class InstalledVersions
26{
27private static $installed = array (
28  'root' =>
29  array (
30    'pretty_version' => '1.20.x-dev',
31    'version' => '1.20.9999999.9999999-dev',
32    'aliases' =>
33    array (
34    ),
35    'reference' => 'e00360b6decd41771547ddf57a5535bfc4e32e34',
36    'name' => 'combostrap/combo',
37  ),
38  'versions' =>
39  array (
40    'antlr/antlr4-php-runtime' =>
41    array (
42      'pretty_version' => '0.5.0',
43      'version' => '0.5.0.0',
44      'aliases' =>
45      array (
46      ),
47      'reference' => '0cb16628b575b50bd087a81d11bd961ac3b8e9e9',
48    ),
49    'combostrap/combo' =>
50    array (
51      'pretty_version' => '1.20.x-dev',
52      'version' => '1.20.9999999.9999999-dev',
53      'aliases' =>
54      array (
55      ),
56      'reference' => 'e00360b6decd41771547ddf57a5535bfc4e32e34',
57    ),
58    'dragonmantank/cron-expression' =>
59    array (
60      'pretty_version' => 'v1.2.1',
61      'version' => '1.2.1.0',
62      'aliases' =>
63      array (
64      ),
65      'reference' => '9504fa9ea681b586028adaaa0877db4aecf32bad',
66    ),
67  ),
68);
69private static $canGetVendors;
70private static $installedByVendor = array();
71
72
73
74
75
76
77
78public static function getInstalledPackages()
79{
80$packages = array();
81foreach (self::getInstalled() as $installed) {
82$packages[] = array_keys($installed['versions']);
83}
84
85if (1 === \count($packages)) {
86return $packages[0];
87}
88
89return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
90}
91
92
93
94
95
96
97
98
99
100public static function isInstalled($packageName)
101{
102foreach (self::getInstalled() as $installed) {
103if (isset($installed['versions'][$packageName])) {
104return true;
105}
106}
107
108return false;
109}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124public static function satisfies(VersionParser $parser, $packageName, $constraint)
125{
126$constraint = $parser->parseConstraints($constraint);
127$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
128
129return $provided->matches($constraint);
130}
131
132
133
134
135
136
137
138
139
140
141public static function getVersionRanges($packageName)
142{
143foreach (self::getInstalled() as $installed) {
144if (!isset($installed['versions'][$packageName])) {
145continue;
146}
147
148$ranges = array();
149if (isset($installed['versions'][$packageName]['pretty_version'])) {
150$ranges[] = $installed['versions'][$packageName]['pretty_version'];
151}
152if (array_key_exists('aliases', $installed['versions'][$packageName])) {
153$ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
154}
155if (array_key_exists('replaced', $installed['versions'][$packageName])) {
156$ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
157}
158if (array_key_exists('provided', $installed['versions'][$packageName])) {
159$ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
160}
161
162return implode(' || ', $ranges);
163}
164
165throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
166}
167
168
169
170
171
172public static function getVersion($packageName)
173{
174foreach (self::getInstalled() as $installed) {
175if (!isset($installed['versions'][$packageName])) {
176continue;
177}
178
179if (!isset($installed['versions'][$packageName]['version'])) {
180return null;
181}
182
183return $installed['versions'][$packageName]['version'];
184}
185
186throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
187}
188
189
190
191
192
193public static function getPrettyVersion($packageName)
194{
195foreach (self::getInstalled() as $installed) {
196if (!isset($installed['versions'][$packageName])) {
197continue;
198}
199
200if (!isset($installed['versions'][$packageName]['pretty_version'])) {
201return null;
202}
203
204return $installed['versions'][$packageName]['pretty_version'];
205}
206
207throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
208}
209
210
211
212
213
214public static function getReference($packageName)
215{
216foreach (self::getInstalled() as $installed) {
217if (!isset($installed['versions'][$packageName])) {
218continue;
219}
220
221if (!isset($installed['versions'][$packageName]['reference'])) {
222return null;
223}
224
225return $installed['versions'][$packageName]['reference'];
226}
227
228throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
229}
230
231
232
233
234
235public static function getRootPackage()
236{
237$installed = self::getInstalled();
238
239return $installed[0]['root'];
240}
241
242
243
244
245
246
247
248
249public static function getRawData()
250{
251@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);
252
253return self::$installed;
254}
255
256
257
258
259
260
261
262public static function getAllRawData()
263{
264return self::getInstalled();
265}
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285public static function reload($data)
286{
287self::$installed = $data;
288self::$installedByVendor = array();
289}
290
291
292
293
294
295private static function getInstalled()
296{
297if (null === self::$canGetVendors) {
298self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
299}
300
301$installed = array();
302
303if (self::$canGetVendors) {
304foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
305if (isset(self::$installedByVendor[$vendorDir])) {
306$installed[] = self::$installedByVendor[$vendorDir];
307} elseif (is_file($vendorDir.'/composer/installed.php')) {
308$installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
309}
310}
311}
312
313$installed[] = self::$installed;
314
315return $installed;
316}
317}
318