[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare(strict_types=1); 4 5 namespace PackageVersions; 6 7 use Generator; 8 use OutOfBoundsException; 9 use UnexpectedValueException; 10 use function array_key_exists; 11 use function array_merge; 12 use function basename; 13 use function file_exists; 14 use function file_get_contents; 15 use function getcwd; 16 use function iterator_to_array; 17 use function json_decode; 18 use function json_encode; 19 use function sprintf; 20 21 /** 22 * @internal 23 * 24 * This is a fallback for {@see \PackageVersions\Versions::getVersion()} 25 * Do not use this class directly: it is intended to be only used when 26 * {@see \PackageVersions\Versions} fails to be generated, which typically 27 * happens when running composer with `--no-scripts` flag) 28 */ 29 final class FallbackVersions 30 { 31 const ROOT_PACKAGE_NAME = 'unknown/root-package@UNKNOWN'; 32 33 private function __construct() 34 { 35 } 36 37 /** 38 * @throws OutOfBoundsException If a version cannot be located. 39 * @throws UnexpectedValueException If the composer.lock file could not be located. 40 */ 41 public static function getVersion(string $packageName): string 42 { 43 $versions = iterator_to_array(self::getVersions(self::getPackageData())); 44 45 if (! array_key_exists($packageName, $versions)) { 46 throw new OutOfBoundsException( 47 'Required package "' . $packageName . '" is not installed: check your ./vendor/composer/installed.json and/or ./composer.lock files' 48 ); 49 } 50 51 return $versions[$packageName]; 52 } 53 54 /** 55 * @return mixed[] 56 * 57 * @throws UnexpectedValueException 58 */ 59 private static function getPackageData(): array 60 { 61 $checkedPaths = [ 62 // The top-level project's ./vendor/composer/installed.json 63 getcwd() . '/vendor/composer/installed.json', 64 __DIR__ . '/../../../../composer/installed.json', 65 // The top-level project's ./composer.lock 66 getcwd() . '/composer.lock', 67 __DIR__ . '/../../../../../composer.lock', 68 // This package's composer.lock 69 __DIR__ . '/../../composer.lock', 70 ]; 71 72 $packageData = []; 73 foreach ($checkedPaths as $path) { 74 if (! file_exists($path)) { 75 continue; 76 } 77 78 $data = json_decode(file_get_contents($path), true); 79 switch (basename($path)) { 80 case 'installed.json': 81 // composer 2.x installed.json format 82 if (isset($data['packages'])) { 83 $packageData[] = $data['packages']; 84 } else { 85 // composer 1.x installed.json format 86 $packageData[] = $data; 87 } 88 89 break; 90 case 'composer.lock': 91 $packageData[] = $data['packages'] + ($data['packages-dev'] ?? []); 92 break; 93 default: 94 // intentionally left blank 95 } 96 } 97 98 if ($packageData !== []) { 99 return array_merge(...$packageData); 100 } 101 102 throw new UnexpectedValueException(sprintf( 103 'PackageVersions could not locate the `vendor/composer/installed.json` or your `composer.lock` ' 104 . 'location. This is assumed to be in %s. If you customized your composer vendor directory and ran composer ' 105 . 'installation with --no-scripts, or if you deployed without the required composer files, PackageVersions ' 106 . 'can\'t detect installed versions.', 107 json_encode($checkedPaths) 108 )); 109 } 110 111 /** 112 * @param mixed[] $packageData 113 * 114 * @return Generator&string[] 115 * 116 * @psalm-return Generator<string, string> 117 */ 118 private static function getVersions(array $packageData): Generator 119 { 120 foreach ($packageData as $package) { 121 yield $package['name'] => $package['version'] . '@' . ( 122 $package['source']['reference'] ?? $package['dist']['reference'] ?? '' 123 ); 124 } 125 126 yield self::ROOT_PACKAGE_NAME => self::ROOT_PACKAGE_NAME; 127 } 128 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |