1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Composer plugin for config assembling |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/composer-config-plugin |
6
|
|
|
* @package composer-config-plugin |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2018, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\composer\config; |
12
|
|
|
|
13
|
|
|
use Composer\Composer; |
14
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
15
|
|
|
use Composer\IO\IOInterface; |
16
|
|
|
use Composer\Plugin\PluginInterface; |
17
|
|
|
use Composer\Script\Event; |
18
|
|
|
use Composer\Script\ScriptEvents; |
19
|
|
|
use hiqdev\composer\config\exceptions\BadConfigurationException; |
20
|
|
|
use hiqdev\composer\config\exceptions\FailedReadException; |
21
|
|
|
use hiqdev\composer\config\readers\ReaderFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Plugin class. |
25
|
|
|
* |
26
|
|
|
* @author Andrii Vasyliev <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Package[] the array of active composer packages |
32
|
|
|
*/ |
33
|
|
|
protected $packages; |
34
|
|
|
|
35
|
|
|
private $alternatives = []; |
36
|
|
|
|
37
|
|
|
private $outputDir; |
38
|
|
|
|
39
|
|
|
private $rootPackage; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array config name => list of files |
43
|
|
|
*/ |
44
|
|
|
protected $files = [ |
45
|
|
|
'dotenv' => [], |
46
|
|
|
'defines' => [], |
47
|
|
|
'params' => [], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
protected $colors = ['red', 'green', 'yellow', 'cyan', 'magenta', 'blue']; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array package name => configs as listed in `composer.json` |
54
|
|
|
*/ |
55
|
|
|
protected $originalFiles = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Builder |
59
|
|
|
*/ |
60
|
|
|
protected $builder; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var Composer instance |
64
|
|
|
*/ |
65
|
|
|
protected $composer; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var IOInterface |
69
|
|
|
*/ |
70
|
|
|
public $io; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initializes the plugin object with the passed $composer and $io. |
74
|
|
|
* @param Composer $composer |
75
|
|
|
* @param IOInterface $io |
76
|
|
|
*/ |
77
|
|
|
public function activate(Composer $composer, IOInterface $io) |
78
|
|
|
{ |
79
|
|
|
$this->composer = $composer; |
80
|
|
|
$this->io = $io; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns list of events the plugin is subscribed to. |
85
|
|
|
* @return array list of events |
86
|
|
|
*/ |
87
|
|
|
public static function getSubscribedEvents() |
88
|
2 |
|
{ |
89
|
|
|
return [ |
90
|
2 |
|
ScriptEvents::POST_AUTOLOAD_DUMP => [ |
91
|
2 |
|
['onPostAutoloadDump', 0], |
92
|
2 |
|
], |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* This is the main function. |
98
|
1 |
|
* @param Event $event |
99
|
|
|
*/ |
100
|
|
|
public function onPostAutoloadDump(Event $event) |
|
|
|
|
101
|
1 |
|
{ |
102
|
|
|
$this->io->overwriteError('<info>Assembling config files</info>'); |
103
|
|
|
|
104
|
|
|
$this->builder = new Builder(); |
105
|
|
|
|
106
|
|
|
$this->initAutoload(); |
107
|
|
|
$this->scanPackages(); |
108
|
|
|
$this->reorderFiles(); |
109
|
|
|
$this->showDepsTree(); |
110
|
|
|
|
111
|
|
|
$this->builder->setOutputDir($this->outputDir); |
112
|
|
|
$this->builder->buildAllConfigs($this->files); |
113
|
|
|
|
114
|
|
|
$saveFiles = $this->files; |
115
|
|
|
$saveEnv = $_ENV; |
116
|
|
|
foreach ($this->alternatives as $name => $files) { |
117
|
|
|
$this->files = $saveFiles; |
118
|
|
|
$_ENV = $saveEnv; |
119
|
|
|
$builder = $this->builder->createAlternative($name); |
120
|
|
|
$this->addFiles($this->rootPackage, $files); |
121
|
|
|
$builder->buildAllConfigs($this->files); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function initAutoload() |
126
|
|
|
{ |
127
|
|
|
$dir = dirname(dirname(dirname(__DIR__))); |
128
|
|
|
require_once "$dir/autoload.php"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function scanPackages() |
132
|
|
|
{ |
133
|
|
|
foreach ($this->getPackages() as $package) { |
134
|
|
|
if ($package->isComplete()) { |
135
|
|
|
$this->processPackage($package); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function reorderFiles(): void |
141
|
|
|
{ |
142
|
|
|
foreach (array_keys($this->files) as $name) { |
143
|
|
|
$this->files[$name] = $this->getAllFiles($name); |
144
|
|
|
} |
145
|
|
|
foreach ($this->files as $name => $files) { |
146
|
|
|
$this->files[$name] = $this->orderFiles($files); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function getAllFiles(string $name, array $stack = []): array |
151
|
|
|
{ |
152
|
|
|
if (empty($this->files[$name])) { |
153
|
|
|
return[]; |
154
|
|
|
} |
155
|
|
|
$res = []; |
156
|
|
|
foreach ($this->files[$name] as $file) { |
157
|
|
|
if (strncmp($file, '$', 1) === 0) { |
158
|
|
|
if (!in_array($name, $stack, true)) { |
159
|
|
|
$res = array_merge($res, $this->getAllFiles(substr($file, 1), array_merge($stack, [$name]))); |
160
|
|
|
} |
161
|
|
|
} else { |
162
|
|
|
$res[] = $file; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $res; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function orderFiles(array $files): array |
170
|
|
|
{ |
171
|
|
|
if (empty($files)) { |
172
|
|
|
return []; |
173
|
|
|
} |
174
|
|
|
$keys = array_combine($files, $files); |
175
|
|
|
$res = []; |
176
|
|
|
foreach ($this->orderedFiles as $file) { |
177
|
|
|
if (isset($keys[$file])) { |
178
|
|
|
$res[$file] = $file; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return array_values($res); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Scans the given package and collects packages data. |
187
|
|
|
* @param Package $package |
188
|
|
|
*/ |
189
|
|
|
protected function processPackage(Package $package) |
190
|
|
|
{ |
191
|
|
|
$files = $package->getFiles(); |
192
|
|
|
$this->originalFiles[$package->getPrettyName()] = $files; |
193
|
|
|
|
194
|
|
|
if (!empty($files)) { |
195
|
|
|
$this->addFiles($package, $files); |
196
|
|
|
} |
197
|
|
|
if ($package->isRoot()) { |
198
|
|
|
$this->rootPackage = $package; |
199
|
|
|
$this->loadDotEnv($package); |
200
|
|
|
$devFiles = $package->getDevFiles(); |
201
|
|
|
if (!empty($devFiles)) { |
202
|
|
|
$this->addFiles($package, $devFiles); |
203
|
|
|
} |
204
|
|
|
$this->outputDir = $package->getOutputDir(); |
205
|
|
|
$alternatives = $package->getAlternatives(); |
206
|
|
|
if (is_string($alternatives)) { |
|
|
|
|
207
|
|
|
$this->alternatives = $this->readConfig($package, $alternatives); |
208
|
|
|
} elseif (is_array($alternatives)) { |
|
|
|
|
209
|
|
|
$this->alternatives = $alternatives; |
210
|
|
|
} elseif (!empty($alternatives)) { |
211
|
|
|
throw new BadConfigurationException('alternatives must be array or path to configuration file'); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$aliases = $package->collectAliases(); |
216
|
|
|
|
217
|
|
|
$this->builder->mergeAliases($aliases); |
218
|
|
|
$this->builder->setPackage($package->getPrettyName(), array_filter([ |
219
|
|
|
'name' => $package->getPrettyName(), |
220
|
|
|
'version' => $package->getVersion(), |
221
|
|
|
'reference' => $package->getSourceReference() ?: $package->getDistReference(), |
222
|
|
|
'aliases' => $aliases, |
223
|
|
|
])); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
private function readConfig($package, $file): array |
227
|
|
|
{ |
228
|
|
|
$path = $package->preparePath($file); |
229
|
|
|
if (!file_exists($path)) { |
230
|
|
|
throw new FailedReadException("failed read file: $file"); |
231
|
|
|
} |
232
|
|
|
$reader = ReaderFactory::get($this->builder, $path); |
233
|
|
|
|
234
|
|
|
return $reader->read($path); |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
protected function loadDotEnv(Package $package) |
239
|
|
|
{ |
240
|
|
|
$path = $package->preparePath('.env'); |
241
|
|
|
if (file_exists($path) && class_exists('Dotenv\Dotenv')) { |
242
|
|
|
$this->addFile($package, 'dotenv', $path); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Adds given files to the list of files to be processed. |
248
|
|
|
* Prepares `defines` in reversed order (outer package first) because |
249
|
|
|
* constants cannot be redefined. |
250
|
|
|
* @param Package $package |
251
|
|
|
* @param array $files |
252
|
|
|
*/ |
253
|
|
|
protected function addFiles(Package $package, array $files) |
254
|
|
|
{ |
255
|
|
|
foreach ($files as $name => $paths) { |
256
|
|
|
$paths = (array) $paths; |
257
|
|
|
if ('defines' === $name) { |
258
|
|
|
$paths = array_reverse($paths); |
259
|
|
|
} |
260
|
|
|
foreach ($paths as $path) { |
261
|
|
|
$this->addFile($package, $name, $path); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
protected $orderedFiles = []; |
267
|
|
|
|
268
|
|
|
protected function addFile(Package $package, string $name, string $path) |
269
|
|
|
{ |
270
|
|
|
$path = $package->preparePath($path); |
271
|
|
|
if (!isset($this->files[$name])) { |
272
|
|
|
$this->files[$name] = []; |
273
|
|
|
} |
274
|
|
|
if (in_array($path, $this->files[$name], true)) { |
275
|
|
|
return; |
276
|
|
|
} |
277
|
|
|
if ('defines' === $name) { |
278
|
|
|
array_unshift($this->orderedFiles, $path); |
279
|
|
|
array_unshift($this->files[$name], $path); |
280
|
|
|
} else { |
281
|
|
|
array_push($this->orderedFiles, $path); |
282
|
|
|
array_push($this->files[$name], $path); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Sets [[packages]]. |
288
|
|
|
* @param Package[] $packages |
289
|
|
|
*/ |
290
|
|
|
public function setPackages(array $packages) |
291
|
|
|
{ |
292
|
|
|
$this->packages = $packages; |
293
|
2 |
|
} |
294
|
|
|
|
295
|
2 |
|
/** |
296
|
2 |
|
* Gets [[packages]]. |
297
|
|
|
* @return Package[] |
298
|
|
|
*/ |
299
|
|
|
public function getPackages() |
300
|
|
|
{ |
301
|
|
|
if (null === $this->packages) { |
302
|
1 |
|
$this->packages = $this->findPackages(); |
303
|
|
|
} |
304
|
1 |
|
|
305
|
|
|
return $this->packages; |
306
|
|
|
} |
307
|
|
|
|
308
|
1 |
|
/** |
309
|
|
|
* Plain list of all project dependencies (including nested) as provided by composer. |
310
|
|
|
* The list is unordered (chaotic, can be different after every update). |
311
|
|
|
*/ |
312
|
|
|
protected $plainList = []; |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Ordered list of package in form: package => depth |
316
|
|
|
* For order description @see findPackages. |
317
|
|
|
*/ |
318
|
|
|
protected $orderedList = []; |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Returns ordered list of packages: |
322
|
|
|
* - listed earlier in the composer.json will get earlier in the list |
323
|
|
|
* - childs before parents. |
324
|
|
|
* @return Package[] |
325
|
|
|
*/ |
326
|
|
|
public function findPackages() |
327
|
|
|
{ |
328
|
|
|
$root = new Package($this->composer->getPackage(), $this->composer); |
329
|
|
|
$this->plainList[$root->getPrettyName()] = $root; |
330
|
|
|
foreach ($this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages() as $package) { |
331
|
|
|
$this->plainList[$package->getPrettyName()] = new Package($package, $this->composer); |
332
|
|
|
} |
333
|
|
|
$this->orderedList = []; |
334
|
|
|
$this->iteratePackage($root, true); |
335
|
|
|
|
336
|
|
|
$res = []; |
337
|
|
|
foreach (array_keys($this->orderedList) as $name) { |
338
|
|
|
$res[] = $this->plainList[$name]; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $res; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Iterates through package dependencies. |
346
|
|
|
* @param Package $package to iterate |
347
|
|
|
* @param bool $includingDev process development dependencies, defaults to not process |
348
|
|
|
*/ |
349
|
|
|
protected function iteratePackage(Package $package, $includingDev = false) |
350
|
|
|
{ |
351
|
|
|
$name = $package->getPrettyName(); |
352
|
|
|
|
353
|
|
|
/// prevent infinite loop in case of circular dependencies |
354
|
|
|
static $processed = []; |
355
|
|
|
if (isset($processed[$name])) { |
356
|
|
|
return; |
357
|
|
|
} else { |
358
|
|
|
$processed[$name] = 1; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/// package depth in dependency hierarchy |
362
|
|
|
static $depth = 0; |
363
|
|
|
++$depth; |
364
|
|
|
|
365
|
|
|
$this->iterateDependencies($package); |
366
|
|
|
if ($includingDev) { |
367
|
|
|
$this->iterateDependencies($package, true); |
368
|
|
|
} |
369
|
|
|
if (!isset($this->orderedList[$name])) { |
370
|
|
|
$this->orderedList[$name] = $depth; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
--$depth; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* Iterates dependencies of the given package. |
378
|
|
|
* @param Package $package |
379
|
|
|
* @param bool $dev which dependencies to iterate: true - dev, default - general |
380
|
|
|
*/ |
381
|
|
|
protected function iterateDependencies(Package $package, $dev = false) |
382
|
|
|
{ |
383
|
|
|
$deps = $dev ? $package->getDevRequires() : $package->getRequires(); |
384
|
|
|
foreach (array_keys($deps) as $target) { |
385
|
|
|
if (isset($this->plainList[$target]) && empty($this->orderedList[$target])) { |
386
|
|
|
$this->iteratePackage($this->plainList[$target]); |
387
|
|
|
} |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
protected function showDepsTree() |
392
|
|
|
{ |
393
|
|
|
if (!$this->io->isVerbose()) { |
394
|
|
|
return; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
foreach (array_reverse($this->orderedList) as $name => $depth) { |
398
|
|
|
$deps = $this->originalFiles[$name]; |
399
|
|
|
$color = $this->colors[$depth % count($this->colors)]; |
400
|
|
|
$indent = str_repeat(' ', $depth - 1); |
401
|
|
|
$package = $this->plainList[$name]; |
402
|
|
|
$showdeps = $deps ? '<comment>[' . implode(',', array_keys($deps)) . ']</>' : ''; |
403
|
|
|
$this->io->write(sprintf('%s - <fg=%s;options=bold>%s</> %s %s', $indent, $color, $name, $package->getFullPrettyVersion(), $showdeps)); |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.