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