1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Composer\Config; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use Yiisoft\Composer\Config\Config\Config; |
9
|
|
|
use Yiisoft\Composer\Config\Config\ConfigFactory; |
10
|
|
|
use Yiisoft\Composer\Config\Util\Resolver; |
11
|
|
|
|
12
|
|
|
use function dirname; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Builder assembles config files. |
16
|
|
|
*/ |
17
|
|
|
class Builder |
18
|
|
|
{ |
19
|
|
|
private const OUTPUT_DIR_SUFFIX = '-output'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string path to the Composer project root |
23
|
|
|
*/ |
24
|
|
|
private string $baseDir; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string path to output assembled configs |
28
|
|
|
*/ |
29
|
|
|
private string $outputDir; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Config[] configurations |
33
|
|
|
*/ |
34
|
|
|
private array $configs = []; |
35
|
|
|
|
36
|
|
|
private ConfigFactory $configFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Builder constructor. |
40
|
|
|
* |
41
|
|
|
* @param ConfigFactory $configFactory |
42
|
|
|
* @param string $baseDir path to the Composer project root |
43
|
|
|
*/ |
44
|
|
|
public function __construct(ConfigFactory $configFactory, string $baseDir) |
45
|
|
|
{ |
46
|
|
|
$this->configFactory = $configFactory; |
47
|
|
|
$this->baseDir = $baseDir; |
48
|
|
|
$this->outputDir = self::findOutputDir($baseDir); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function createAlternative($name): Builder |
52
|
|
|
{ |
53
|
|
|
$alt = new static($this->configFactory, $this->baseDir); |
54
|
|
|
$alt->setOutputDir($this->outputDir . DIRECTORY_SEPARATOR . $name); |
55
|
|
|
foreach (['aliases', 'packages'] as $key) { |
56
|
|
|
$alt->configs[$key] = $this->getConfig($key)->clone($alt); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $alt; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setOutputDir(?string $outputDir): void |
63
|
|
|
{ |
64
|
|
|
$this->outputDir = $outputDir |
65
|
|
|
? static::buildAbsPath($this->getBaseDir(), $outputDir) |
66
|
|
|
: static::findOutputDir($this->getBaseDir()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public static function rebuild(?string $baseDir = null): void |
70
|
|
|
{ |
71
|
|
|
// Ensure COMPOSER_HOME is set in case web server does not give PHP OS environment variables |
72
|
|
|
if (!(getenv('APPDATA') || getenv('HOME') || getenv('COMPOSER_HOME'))) { |
73
|
|
|
$path = sys_get_temp_dir() . '/.composer'; |
74
|
|
|
if (!is_dir($path) && !mkdir($path)) { |
75
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $path)); |
76
|
|
|
} |
77
|
|
|
putenv('COMPOSER_HOME=' . $path); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
Plugin::buildAllConfigs($baseDir ?? self::findBaseDir()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns default output dir. |
85
|
|
|
* |
86
|
|
|
* @param string|null $baseDir path to the root Composer package. When `null`, |
87
|
|
|
* @return string |
88
|
|
|
* @throws JsonException |
89
|
|
|
*/ |
90
|
7 |
|
private static function findOutputDir(string $baseDir = null): string |
91
|
|
|
{ |
92
|
7 |
|
if ($baseDir === null) { |
93
|
|
|
$baseDir = static::findBaseDir(); |
94
|
|
|
} |
95
|
7 |
|
$path = $baseDir . DIRECTORY_SEPARATOR . 'composer.json'; |
96
|
7 |
|
$data = @json_decode(file_get_contents($path), true); |
97
|
7 |
|
$dir = $data['extra'][Package::EXTRA_OUTPUT_DIR_OPTION_NAME] ?? null; |
98
|
|
|
|
99
|
7 |
|
return $dir ? static::buildAbsPath($baseDir, $dir) : static::defaultOutputDir($baseDir); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private static function findBaseDir(): string |
103
|
|
|
{ |
104
|
|
|
$candidates = [ |
105
|
|
|
// normal relative path |
106
|
|
|
dirname(__DIR__, 4), |
107
|
|
|
// console |
108
|
|
|
getcwd(), |
109
|
|
|
// symlinked web |
110
|
|
|
dirname(getcwd()) |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
foreach ($candidates as $baseDir) { |
114
|
|
|
if (file_exists($baseDir . DIRECTORY_SEPARATOR . 'composer.json')) { |
115
|
|
|
return $baseDir; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
throw new \RuntimeException('Cannot find directory that contains composer.json'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns default output dir. |
124
|
|
|
* |
125
|
|
|
* @param string $baseDir path to base directory |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
7 |
|
private static function defaultOutputDir(string $baseDir = null): string |
129
|
|
|
{ |
130
|
7 |
|
if ($baseDir) { |
131
|
7 |
|
$dir = $baseDir . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'yiisoft' . DIRECTORY_SEPARATOR . basename(dirname(__DIR__)); |
132
|
|
|
} else { |
133
|
|
|
$dir = dirname(__DIR__); |
134
|
|
|
} |
135
|
|
|
|
136
|
7 |
|
return $dir . static::OUTPUT_DIR_SUFFIX; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns full path to assembled config file. |
141
|
|
|
* |
142
|
|
|
* @param string $filename name of config |
143
|
|
|
* @param string $baseDir path to base dir |
144
|
|
|
* @return string absolute path |
145
|
|
|
* @throws JsonException |
146
|
|
|
*/ |
147
|
7 |
|
public static function path(string $filename, string $baseDir = null): string |
148
|
|
|
{ |
149
|
7 |
|
return static::buildAbsPath(static::findOutputDir($baseDir), $filename . '.php'); |
150
|
|
|
} |
151
|
|
|
|
152
|
7 |
|
private static function buildAbsPath(string $dir, string $file): string |
153
|
|
|
{ |
154
|
7 |
|
return self::isAbsolutePath($file) ? $file : $dir . DIRECTORY_SEPARATOR . $file; |
155
|
|
|
} |
156
|
|
|
|
157
|
7 |
|
private static function isAbsolutePath(string $path): bool |
158
|
|
|
{ |
159
|
7 |
|
return strpos($path, '/') === 0 || strpos($path, ':') === 1 || strpos($path, '\\\\') === 0; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Builds all (user and system) configs by given files list. |
164
|
|
|
* |
165
|
|
|
* @param null|array $files files to process: config name => list of files |
166
|
|
|
*/ |
167
|
|
|
public function buildAllConfigs(array $files): void |
168
|
|
|
{ |
169
|
|
|
$this->buildUserConfigs($files); |
170
|
|
|
$this->buildSystemConfigs(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Builds configs by given files list. |
175
|
|
|
* |
176
|
|
|
* @param null|array $files files to process: config name => list of files |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
private function buildUserConfigs(array $files): array |
180
|
|
|
{ |
181
|
|
|
$resolver = new Resolver($files); |
182
|
|
|
$files = $resolver->get(); |
183
|
|
|
foreach ($files as $name => $paths) { |
184
|
|
|
$this->getConfig($name)->load($paths)->build()->write(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $files; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
private function buildSystemConfigs(): void |
191
|
|
|
{ |
192
|
|
|
foreach (['aliases', 'packages'] as $name) { |
193
|
|
|
$this->getConfig($name)->build()->write(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getOutputPath(string $name): string |
198
|
|
|
{ |
199
|
|
|
return $this->outputDir . DIRECTORY_SEPARATOR . $name . '.php'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getConfig(string $name) |
203
|
|
|
{ |
204
|
|
|
if (!array_key_exists($name, $this->configs)) { |
205
|
|
|
$this->configs[$name] = $this->configFactory->create($this, $name); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $this->configs[$name]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getVars(): array |
212
|
|
|
{ |
213
|
|
|
$vars = []; |
214
|
|
|
foreach ($this->configs as $name => $config) { |
215
|
|
|
$vars[$name] = $config->getValues(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $vars; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function mergeAliases(array $aliases): void |
222
|
|
|
{ |
223
|
|
|
$this->getConfig('aliases')->mergeValues($aliases); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function setPackage(string $name, array $data): void |
227
|
|
|
{ |
228
|
|
|
$this->getConfig('packages')->setValue($name, $data); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return string a full path to the project root |
233
|
|
|
*/ |
234
|
|
|
public function getBaseDir(): string |
235
|
|
|
{ |
236
|
|
|
return $this->baseDir; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Require another configuration by name. |
241
|
|
|
* |
242
|
|
|
* It will result in "require 'my-config' in the assembled configuration file. |
243
|
|
|
* |
244
|
|
|
* @param string $config config name |
245
|
|
|
* @return callable |
246
|
|
|
*/ |
247
|
|
|
public static function require(string $config): callable |
248
|
|
|
{ |
249
|
|
|
return static fn () => require $config; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|