1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Composer\Config\Config; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Composer\Config\Builder; |
9
|
|
|
use Yiisoft\Composer\Config\ContentWriter; |
10
|
|
|
use Yiisoft\Composer\Config\Reader\ReaderFactory; |
11
|
|
|
use Yiisoft\Composer\Config\Util\Helper; |
12
|
|
|
use Yiisoft\Composer\Config\Util\PathHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Config class represents output configuration file. |
16
|
|
|
*/ |
17
|
|
|
class Config |
18
|
|
|
{ |
19
|
|
|
private const BASE_DIR_MARKER = '<<<base-dir>>>'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string config name |
23
|
|
|
*/ |
24
|
|
|
private string $name; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array sources - paths to config source files |
28
|
|
|
*/ |
29
|
|
|
private array $sources = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array config value |
33
|
|
|
*/ |
34
|
|
|
protected array $values = []; |
35
|
|
|
|
36
|
|
|
protected Builder $builder; |
37
|
|
|
|
38
|
|
|
protected ContentWriter $contentWriter; |
39
|
|
|
|
40
|
2 |
|
public function __construct(Builder $builder, string $name) |
41
|
|
|
{ |
42
|
2 |
|
$this->builder = $builder; |
43
|
2 |
|
$this->name = $name; |
44
|
2 |
|
$this->contentWriter = new ContentWriter(); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
public function clone(Builder $builder): self |
48
|
|
|
{ |
49
|
|
|
$config = new self($builder, $this->name); |
50
|
|
|
$config->sources = $this->sources; |
51
|
|
|
$config->values = $this->values; |
52
|
|
|
|
53
|
|
|
return $config; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function getValues(): array |
57
|
|
|
{ |
58
|
1 |
|
return $this->values; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function load(array $paths = []): self |
62
|
|
|
{ |
63
|
1 |
|
$this->sources = $this->loadFiles($paths); |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
private function loadFiles(array $paths): array |
69
|
|
|
{ |
70
|
1 |
|
switch (count($paths)) { |
71
|
1 |
|
case 0: |
72
|
1 |
|
return []; |
73
|
1 |
|
case 1: |
74
|
1 |
|
$path = reset($paths); |
75
|
|
|
if ($this->containsWildcard($path) === false) { |
76
|
|
|
return [$this->loadFile(reset($paths))]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$configs = []; |
81
|
|
|
foreach ($paths as $path) { |
82
|
|
|
$cs = $this->loadFiles($this->glob($path)); |
83
|
|
|
foreach ($cs as $config) { |
84
|
|
|
if (!empty($config)) { |
85
|
|
|
$configs[] = $config; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $configs; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function glob(string $path): array |
94
|
|
|
{ |
95
|
|
|
if ($this->containsWildcard($path) === false) { |
96
|
|
|
return [$path]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return glob($path); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function containsWildcard(string $path): bool |
103
|
|
|
{ |
104
|
|
|
return strpos($path, '*') !== false; |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
/** |
108
|
|
|
* Reads config file. |
109
|
1 |
|
* |
110
|
|
|
* @param string $path |
111
|
|
|
* |
112
|
|
|
* @return array configuration read from file |
113
|
|
|
*/ |
114
|
|
|
protected function loadFile(string $path): array |
115
|
|
|
{ |
116
|
|
|
$reader = ReaderFactory::get($this->builder, $path); |
117
|
1 |
|
|
118
|
|
|
return $reader->read($path); |
119
|
1 |
|
} |
120
|
|
|
|
121
|
1 |
|
/** |
122
|
|
|
* Merges given configs and writes at given name. |
123
|
|
|
* |
124
|
1 |
|
* @return Config |
125
|
|
|
*/ |
126
|
1 |
|
public function build(): self |
127
|
|
|
{ |
128
|
1 |
|
$this->values = $this->calcValues($this->sources); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
1 |
|
} |
132
|
|
|
|
133
|
1 |
|
public function write(): self |
134
|
|
|
{ |
135
|
1 |
|
$this->writeFile($this->getOutputPath(), $this->values); |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
1 |
|
} |
139
|
|
|
|
140
|
1 |
|
protected function calcValues(array $sources): array |
141
|
1 |
|
{ |
142
|
|
|
$values = ArrayHelper::merge(...$sources); |
143
|
1 |
|
|
144
|
1 |
|
return $this->substituteOutputDirs($values); |
145
|
1 |
|
} |
146
|
1 |
|
|
147
|
|
|
protected function writeFile(string $path, array $data): void |
148
|
|
|
{ |
149
|
1 |
|
$depth = $this->findDepth(); |
150
|
|
|
$baseDir = $depth > 0 ? "dirname(__DIR__, $depth)" : '__DIR__'; |
151
|
1 |
|
|
152
|
|
|
$envs = $this->envsRequired() ? "\$_ENV = array_merge((array) require __DIR__ . '/envs.php', \$_ENV);" : ''; |
153
|
1 |
|
$constants = $this->constantsRequired() ? $this->builder->getConfig('constants')->buildRequires() : ''; |
154
|
|
|
$params = $this->paramsRequired() ? "\$params = (array) require __DIR__ . '/params.php';" : ''; |
155
|
1 |
|
$variables = Helper::exportVar($data); |
156
|
|
|
|
157
|
1 |
|
$content = <<<PHP |
158
|
|
|
<?php |
159
|
1 |
|
|
160
|
|
|
\$baseDir = {$baseDir}; |
161
|
|
|
|
162
|
1 |
|
{$envs} |
163
|
1 |
|
|
164
|
|
|
{$constants} |
165
|
1 |
|
|
166
|
|
|
{$params} |
167
|
1 |
|
|
168
|
|
|
return {$variables}; |
169
|
|
|
PHP; |
170
|
1 |
|
|
171
|
|
|
$this->contentWriter->write($path, $this->replaceMarkers($content) . "\n"); |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
protected function envsRequired(): bool |
175
|
1 |
|
{ |
176
|
|
|
return true; |
177
|
1 |
|
} |
178
|
|
|
|
179
|
|
|
protected function constantsRequired(): bool |
180
|
1 |
|
{ |
181
|
|
|
return true; |
182
|
1 |
|
} |
183
|
1 |
|
|
184
|
|
|
protected function paramsRequired(): bool |
185
|
1 |
|
{ |
186
|
|
|
return true; |
187
|
|
|
} |
188
|
1 |
|
|
189
|
|
|
private function findDepth(): int |
190
|
1 |
|
{ |
191
|
1 |
|
$outDir = PathHelper::realpath(dirname($this->getOutputPath())); |
192
|
1 |
|
$diff = substr($outDir, strlen(PathHelper::realpath($this->getBaseDir()))); |
193
|
|
|
|
194
|
|
|
return substr_count($diff, '/'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
private function replaceMarkers(string $content): string |
198
|
|
|
{ |
199
|
|
|
return str_replace( |
200
|
|
|
["'" . self::BASE_DIR_MARKER, "'?" . self::BASE_DIR_MARKER], |
201
|
|
|
["\$baseDir . '", "'?' . \$baseDir . '"], |
202
|
|
|
$content |
203
|
1 |
|
); |
204
|
|
|
} |
205
|
1 |
|
|
206
|
|
|
/** |
207
|
1 |
|
* Substitute output paths in given data array recursively with marker. |
208
|
|
|
* |
209
|
|
|
* @param array $data |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
protected function substituteOutputDirs(array $data): array |
214
|
|
|
{ |
215
|
|
|
$dir = PathHelper::normalize($this->getBaseDir()); |
216
|
|
|
|
217
|
1 |
|
return $this->substitutePaths($data, $dir); |
218
|
|
|
} |
219
|
1 |
|
|
220
|
1 |
|
/** |
221
|
1 |
|
* Substitute all paths in given array recursively with marker if applicable. |
222
|
|
|
* |
223
|
|
|
* @param array $data |
224
|
1 |
|
* @param string $dir |
225
|
|
|
* |
226
|
|
|
* @return array |
227
|
|
|
*/ |
228
|
|
|
private function substitutePaths($data, $dir): array |
229
|
|
|
{ |
230
|
|
|
$res = []; |
231
|
|
|
foreach ($data as $key => $value) { |
232
|
|
|
$res[$this->substitutePath($key, $dir)] = $this->substitutePath($value, $dir); |
233
|
|
|
} |
234
|
1 |
|
|
235
|
|
|
return $res; |
236
|
1 |
|
} |
237
|
1 |
|
|
238
|
|
|
/** |
239
|
1 |
|
* Substitute all paths in given value if applicable. |
240
|
1 |
|
* |
241
|
|
|
* @param mixed $value |
242
|
|
|
* @param string $dir |
243
|
1 |
|
* |
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
|
|
private function substitutePath($value, $dir) |
247
|
|
|
{ |
248
|
|
|
if (is_string($value)) { |
249
|
|
|
return $this->substitutePathInString($value, $dir); |
250
|
|
|
} |
251
|
|
|
if (is_array($value)) { |
252
|
|
|
return $this->substitutePaths($value, $dir); |
253
|
1 |
|
} |
254
|
|
|
|
255
|
1 |
|
return $value; |
256
|
1 |
|
} |
257
|
1 |
|
|
258
|
|
|
/** |
259
|
|
|
* Substitute path with marker in string if applicable. |
260
|
1 |
|
* |
261
|
|
|
* @param string $path |
262
|
1 |
|
* @param string $dir |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
1 |
|
*/ |
266
|
|
|
private function substitutePathInString($path, $dir): string |
267
|
|
|
{ |
268
|
1 |
|
$end = $dir . '/'; |
269
|
|
|
$skippable = 0 === strncmp($path, '?', 1); |
270
|
|
|
if ($skippable) { |
271
|
1 |
|
$path = substr($path, 1); |
272
|
|
|
} |
273
|
1 |
|
if ($path === $dir) { |
274
|
|
|
$result = self::BASE_DIR_MARKER; |
275
|
|
|
} elseif (strpos($path, $end) === 0) { |
276
|
1 |
|
$result = self::BASE_DIR_MARKER . substr($path, strlen($end) - 1); |
277
|
|
|
} else { |
278
|
1 |
|
$result = $path; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
return ($skippable ? '?' : '') . $result; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
private function getBaseDir(): string |
285
|
|
|
{ |
286
|
|
|
return $this->builder->getBaseDir(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
protected function getOutputPath(string $name = null): string |
290
|
|
|
{ |
291
|
|
|
return $this->builder->getOutputPath($name ?: $this->name); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|