← All changes
|
includes/sdk/google/composer/InstalledVersions.php
+41
-24
1.2.13
→
1.4.1
View file →
| @@ -8,12 +8,12 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | 10 | * file that was distributed with this source code. |
| 11 | 11 | */ |
| 12 | -namespace Dudlewebs\WPMCS\Composer; | |
| 12 | +namespace Dudlewebs\WPMCS\GCP\Composer; | |
| 13 | 13 | |
| 14 | -use Dudlewebs\WPMCS\Composer\Autoload\ClassLoader; | |
| 15 | -use Dudlewebs\WPMCS\Composer\Semver\VersionParser; | |
| 14 | +use Dudlewebs\WPMCS\GCP\Composer\Autoload\ClassLoader; | |
| 15 | +use Dudlewebs\WPMCS\GCP\Composer\Semver\VersionParser; | |
| 16 | 16 | /** |
| 17 | 17 | * This class is copied in every Composer installed project and available to all |
| 18 | 18 | * |
| 19 | 19 | * See also https://getcomposer.org/doc/07-runtime.md#installed-versions |
| @@ -29,8 +29,12 @@ | ||
| 29 | 29 | * @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 |
| 30 | 30 | */ |
| 31 | 31 | private static $installed; |
| 32 | 32 | /** |
| 33 | + * @var bool | |
| 34 | + */ | |
| 35 | + private static $installedIsLocalDir; | |
| 36 | + /** | |
| 33 | 37 | * @var bool|null |
| 34 | 38 | */ |
| 35 | 39 | private static $canGetVendors; |
| 36 | 40 | /** |
| @@ -47,14 +51,14 @@ | ||
| 47 | 51 | public static function getInstalledPackages() |
| 48 | 52 | { |
| 49 | 53 | $packages = array(); |
| 50 | 54 | foreach (self::getInstalled() as $installed) { |
| 51 | - $packages[] = array_keys($installed['versions']); | |
| 55 | + $packages[] = \array_keys($installed['versions']); | |
| 52 | 56 | } |
| 53 | 57 | if (1 === \count($packages)) { |
| 54 | 58 | return $packages[0]; |
| 55 | 59 | } |
| 56 | - return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); | |
| 60 | + return \array_keys(\array_flip(\call_user_func_array('array_merge', $packages))); | |
| 57 | 61 | } |
| 58 | 62 | /** |
| 59 | 63 | * Returns a list of all package names with a specific type e.g. 'library' |
| 60 | 64 | * |
| @@ -128,18 +132,18 @@ | ||
| 128 | 132 | $ranges = array(); |
| 129 | 133 | if (isset($installed['versions'][$packageName]['pretty_version'])) { |
| 130 | 134 | $ranges[] = $installed['versions'][$packageName]['pretty_version']; |
| 131 | 135 | } |
| 132 | - if (array_key_exists('aliases', $installed['versions'][$packageName])) { | |
| 133 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); | |
| 136 | + if (\array_key_exists('aliases', $installed['versions'][$packageName])) { | |
| 137 | + $ranges = \array_merge($ranges, $installed['versions'][$packageName]['aliases']); | |
| 134 | 138 | } |
| 135 | - if (array_key_exists('replaced', $installed['versions'][$packageName])) { | |
| 136 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); | |
| 139 | + if (\array_key_exists('replaced', $installed['versions'][$packageName])) { | |
| 140 | + $ranges = \array_merge($ranges, $installed['versions'][$packageName]['replaced']); | |
| 137 | 141 | } |
| 138 | - if (array_key_exists('provided', $installed['versions'][$packageName])) { | |
| 139 | - $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); | |
| 142 | + if (\array_key_exists('provided', $installed['versions'][$packageName])) { | |
| 143 | + $ranges = \array_merge($ranges, $installed['versions'][$packageName]['provided']); | |
| 140 | 144 | } |
| 141 | - return implode(' || ', $ranges); | |
| 145 | + return \implode(' || ', $ranges); | |
| 142 | 146 | } |
| 143 | 147 | throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); |
| 144 | 148 | } |
| 145 | 149 | /** |
| @@ -224,14 +228,14 @@ | ||
| 224 | 228 | * @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[]}>} |
| 225 | 229 | */ |
| 226 | 230 | public static function getRawData() |
| 227 | 231 | { |
| 228 | - @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); | |
| 232 | + @\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); | |
| 229 | 233 | if (null === self::$installed) { |
| 230 | 234 | // only require the installed.php file if this file is loaded from its dumped location, |
| 231 | 235 | // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
| 232 | - if (substr(__DIR__, -8, 1) !== 'C') { | |
| 233 | - self::$installed = include __DIR__ . '/installed.php'; | |
| 236 | + if (\substr(__DIR__, -8, 1) !== 'C') { | |
| 237 | + self::$installed = (include __DIR__ . '/installed.php'); | |
| 234 | 238 | } else { |
| 235 | 239 | self::$installed = array(); |
| 236 | 240 | } |
| 237 | 241 | } |
| @@ -268,8 +272,13 @@ | ||
| 268 | 272 | public static function reload($data) |
| 269 | 273 | { |
| 270 | 274 | self::$installed = $data; |
| 271 | 275 | self::$installedByVendor = array(); |
| 276 | + // when using reload, we disable the duplicate protection to ensure that self::$installed data is | |
| 277 | + // always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not, | |
| 278 | + // so we have to assume it does not, and that may result in duplicate data being returned when listing | |
| 279 | + // all installed packages for example | |
| 280 | + self::$installedIsLocalDir = \false; | |
| 272 | 281 | } |
| 273 | 282 | /** |
| 274 | 283 | * @return array[] |
| 275 | 284 | * @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[]}>}> |
| @@ -276,37 +285,45 @@ | ||
| 276 | 285 | */ |
| 277 | 286 | private static function getInstalled() |
| 278 | 287 | { |
| 279 | 288 | if (null === self::$canGetVendors) { |
| 280 | - self::$canGetVendors = method_exists('Dudlewebs\WPMCS\Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); | |
| 289 | + self::$canGetVendors = \method_exists('Dudlewebs\\WPMCS\\GCP\\Dudlewebs\\WPMCS\\GCP\\Composer\\Autoload\\ClassLoader', 'getRegisteredLoaders'); | |
| 281 | 290 | } |
| 282 | 291 | $installed = array(); |
| 292 | + $copiedLocalDir = \false; | |
| 283 | 293 | if (self::$canGetVendors) { |
| 294 | + $selfDir = \strtr(__DIR__, '\\', '/'); | |
| 284 | 295 | foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { |
| 296 | + $vendorDir = \strtr($vendorDir, '\\', '/'); | |
| 285 | 297 | if (isset(self::$installedByVendor[$vendorDir])) { |
| 286 | 298 | $installed[] = self::$installedByVendor[$vendorDir]; |
| 287 | - } elseif (is_file($vendorDir . '/composer/installed.php')) { | |
| 299 | + } elseif (\is_file($vendorDir . '/composer/installed.php')) { | |
| 288 | 300 | /** @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 */ |
| 289 | - $required = require $vendorDir . '/composer/installed.php'; | |
| 290 | - $installed[] = self::$installedByVendor[$vendorDir] = $required; | |
| 291 | - if (null === self::$installed && strtr($vendorDir . '/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { | |
| 292 | - self::$installed = $installed[count($installed) - 1]; | |
| 301 | + $required = (require $vendorDir . '/composer/installed.php'); | |
| 302 | + self::$installedByVendor[$vendorDir] = $required; | |
| 303 | + $installed[] = $required; | |
| 304 | + if (self::$installed === null && $vendorDir . '/composer' === $selfDir) { | |
| 305 | + self::$installed = $required; | |
| 306 | + self::$installedIsLocalDir = \true; | |
| 293 | 307 | } |
| 294 | 308 | } |
| 309 | + if (self::$installedIsLocalDir && $vendorDir . '/composer' === $selfDir) { | |
| 310 | + $copiedLocalDir = \true; | |
| 311 | + } | |
| 295 | 312 | } |
| 296 | 313 | } |
| 297 | 314 | if (null === self::$installed) { |
| 298 | 315 | // only require the installed.php file if this file is loaded from its dumped location, |
| 299 | 316 | // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 |
| 300 | - if (substr(__DIR__, -8, 1) !== 'C') { | |
| 317 | + if (\substr(__DIR__, -8, 1) !== 'C') { | |
| 301 | 318 | /** @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 __DIR__ . '/installed.php'; | |
| 319 | + $required = (require __DIR__ . '/installed.php'); | |
| 303 | 320 | self::$installed = $required; |
| 304 | 321 | } else { |
| 305 | 322 | self::$installed = array(); |
| 306 | 323 | } |
| 307 | 324 | } |
| 308 | - if (self::$installed !== array()) { | |
| 325 | + if (self::$installed !== array() && !$copiedLocalDir) { | |
| 309 | 326 | $installed[] = self::$installed; |
| 310 | 327 | } |
| 311 | 328 | return $installed; |
| 312 | 329 | } |