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