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