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