Composer.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Support;
  12. use Composer\Autoload\ClassLoader;
  13. use Hyperf\Collection\Collection;
  14. use RuntimeException;
  15. use function Hyperf\Collection\collect;
  16. class Composer
  17. {
  18. private static ?Collection $content = null;
  19. private static ?Collection $json = null;
  20. private static array $extra = [];
  21. private static array $scripts = [];
  22. private static array $versions = [];
  23. private static ?ClassLoader $classLoader = null;
  24. /**
  25. * @throws RuntimeException When `composer.lock` does not exist.
  26. */
  27. public static function getLockContent(): Collection
  28. {
  29. if (! self::$content) {
  30. if (! $path = self::discoverLockFile()) {
  31. throw new RuntimeException('composer.lock not found.');
  32. }
  33. self::$content = collect(json_decode(file_get_contents($path), true));
  34. $packages = self::$content->offsetGet('packages') ?? [];
  35. $packagesDev = self::$content->offsetGet('packages-dev') ?? [];
  36. foreach (array_merge($packages, $packagesDev) as $package) {
  37. $packageName = '';
  38. foreach ($package ?? [] as $key => $value) {
  39. if ($key === 'name') {
  40. $packageName = $value;
  41. continue;
  42. }
  43. $packageName && match ($key) {
  44. 'extra' => self::$extra[$packageName] = $value,
  45. 'scripts' => self::$scripts[$packageName] = $value,
  46. 'version' => self::$versions[$packageName] = $value,
  47. default => null,
  48. };
  49. }
  50. }
  51. }
  52. return self::$content;
  53. }
  54. public static function getJsonContent(): Collection
  55. {
  56. if (self::$json) {
  57. return self::$json;
  58. }
  59. if (! is_readable($path = BASE_PATH . '/composer.json')) {
  60. throw new RuntimeException('composer.json is not readable.');
  61. }
  62. return self::$json = collect(json_decode(file_get_contents($path), true));
  63. }
  64. public static function discoverLockFile(): string
  65. {
  66. if (is_readable($path = BASE_PATH . '/composer.lock')) {
  67. return $path;
  68. }
  69. return '';
  70. }
  71. public static function getMergedExtra(?string $key = null)
  72. {
  73. if (! self::$extra) {
  74. self::getLockContent();
  75. }
  76. if ($key === null) {
  77. return self::$extra;
  78. }
  79. $extra = [];
  80. foreach (self::$extra as $project => $config) {
  81. foreach ($config ?? [] as $configKey => $item) {
  82. if ($key === $configKey && $item) {
  83. foreach ($item as $k => $v) {
  84. if (is_array($v)) {
  85. $extra[$k] = array_merge($extra[$k] ?? [], $v);
  86. } else {
  87. $extra[$k][] = $v;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. return $extra;
  94. }
  95. public static function getLoader(): ClassLoader
  96. {
  97. return self::$classLoader ??= self::findLoader();
  98. }
  99. public static function setLoader(ClassLoader $classLoader): ClassLoader
  100. {
  101. return self::$classLoader = $classLoader;
  102. }
  103. public static function getScripts(): array
  104. {
  105. if (! self::$scripts) {
  106. self::getLockContent();
  107. }
  108. return self::$scripts;
  109. }
  110. public static function getVersions(): array
  111. {
  112. if (! self::$versions) {
  113. self::getLockContent();
  114. }
  115. return self::$versions;
  116. }
  117. public static function hasPackage(string $packageName): bool
  118. {
  119. if (! self::$json) {
  120. self::getJsonContent();
  121. }
  122. if (self::$json['require'][$packageName] ?? self::$json['require-dev'][$packageName] ?? self::$json['replace'][$packageName] ?? '') {
  123. return true;
  124. }
  125. if (! self::$versions) {
  126. self::getLockContent();
  127. }
  128. return isset(self::$versions[$packageName]);
  129. }
  130. private static function findLoader(): ClassLoader
  131. {
  132. $loaders = spl_autoload_functions();
  133. foreach ($loaders as $loader) {
  134. if (is_array($loader) && $loader[0] instanceof ClassLoader) {
  135. return $loader[0];
  136. }
  137. }
  138. throw new RuntimeException('Composer loader not found.');
  139. }
  140. }