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