Composer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. return self::$content;
  31. }
  32. if (! $path = self::discoverLockFile()) {
  33. throw new RuntimeException('composer.lock not found.');
  34. }
  35. self::$content = collect(json_decode(file_get_contents($path), true));
  36. $packages = self::$content->offsetGet('packages') ?? [];
  37. $packagesDev = self::$content->offsetGet('packages-dev') ?? [];
  38. foreach (array_merge($packages, $packagesDev) as $package) {
  39. $packageName = $package['name'] ?? '';
  40. if (! $packageName) {
  41. continue;
  42. }
  43. foreach ($package as $key => $value) {
  44. match ($key) {
  45. 'extra' => self::$extra[$packageName] = $value,
  46. 'scripts' => self::$scripts[$packageName] = $value,
  47. 'version' => self::$versions[$packageName] = $value,
  48. default => null,
  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): array
  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 $config) {
  81. if (! isset($config[$key])) {
  82. continue;
  83. }
  84. foreach ($config[$key] as $k => $v) {
  85. if (is_array($v)) {
  86. $extra[$k] = array_merge($extra[$k] ?? [], $v);
  87. } else {
  88. $extra[$k][] = $v;
  89. }
  90. }
  91. }
  92. return $extra;
  93. }
  94. public static function getLoader(): ClassLoader
  95. {
  96. return self::$classLoader ??= self::findLoader();
  97. }
  98. public static function setLoader(ClassLoader $classLoader): ClassLoader
  99. {
  100. return self::$classLoader = $classLoader;
  101. }
  102. public static function getScripts(): array
  103. {
  104. if (! self::$scripts) {
  105. self::getLockContent();
  106. }
  107. return self::$scripts;
  108. }
  109. public static function getVersions(): array
  110. {
  111. if (! self::$versions) {
  112. self::getLockContent();
  113. }
  114. return self::$versions;
  115. }
  116. public static function hasPackage(string $packageName): bool
  117. {
  118. if (! self::$json) {
  119. self::getJsonContent();
  120. }
  121. if (self::$json['require'][$packageName] ?? self::$json['require-dev'][$packageName] ?? self::$json['replace'][$packageName] ?? '') {
  122. return true;
  123. }
  124. if (! self::$versions) {
  125. self::getLockContent();
  126. }
  127. return isset(self::$versions[$packageName]);
  128. }
  129. private static function findLoader(): ClassLoader
  130. {
  131. $loaders = spl_autoload_functions();
  132. foreach ($loaders as $loader) {
  133. if (is_array($loader) && $loader[0] instanceof ClassLoader) {
  134. return $loader[0];
  135. }
  136. }
  137. throw new RuntimeException('Composer loader not found.');
  138. }
  139. }