1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Composer plugin for config assembling |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/composer-config-plugin |
7
|
|
|
* @package composer-config-plugin |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\ComposerConfigPlugin; |
13
|
|
|
|
14
|
|
|
use Composer\Composer; |
15
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Composer\IO\IOInterface; |
17
|
|
|
use Composer\Package\PackageInterface; |
18
|
|
|
use Composer\Package\RootPackageInterface; |
19
|
|
|
use Composer\Plugin\PluginInterface; |
20
|
|
|
use Composer\Script\Event; |
21
|
|
|
use Composer\Script\ScriptEvents; |
22
|
|
|
use Composer\Util\Filesystem; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Plugin class. |
26
|
|
|
* |
27
|
|
|
* @author Andrii Vasyliev <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Plugin implements PluginInterface, EventSubscriberInterface |
30
|
|
|
{ |
31
|
|
|
const PACKAGE_TYPE = 'yii2-extension'; |
32
|
|
|
const EXTRA_OPTION_NAME = 'config-plugin'; |
33
|
|
|
const OUTPUT_PATH = 'hiqdev/config'; |
34
|
|
|
const BASE_DIR_SAMPLE = '<base-dir>'; |
35
|
|
|
const VENDOR_DIR_SAMPLE = '<base-dir>/vendor'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var PackageInterface[] the array of active composer packages |
39
|
|
|
*/ |
40
|
|
|
protected $packages; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string absolute path to the package base directory |
44
|
|
|
*/ |
45
|
|
|
protected $baseDir; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string absolute path to vendor directory |
49
|
|
|
*/ |
50
|
|
|
protected $vendorDir; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Filesystem utility |
54
|
|
|
*/ |
55
|
|
|
protected $filesystem; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array assembled config data |
59
|
|
|
*/ |
60
|
|
|
protected $data = [ |
61
|
|
|
'aliases' => [], |
62
|
|
|
'extensions' => [], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array raw collected data |
67
|
|
|
*/ |
68
|
|
|
protected $raw = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var array array of not yet merged params |
72
|
|
|
*/ |
73
|
|
|
protected $rawParams = []; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var Composer instance |
77
|
|
|
*/ |
78
|
|
|
protected $composer; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var IOInterface |
82
|
|
|
*/ |
83
|
|
|
public $io; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Initializes the plugin object with the passed $composer and $io. |
87
|
|
|
* @param Composer $composer |
88
|
|
|
* @param IOInterface $io |
89
|
|
|
*/ |
90
|
2 |
|
public function activate(Composer $composer, IOInterface $io) |
91
|
|
|
{ |
92
|
2 |
|
$this->composer = $composer; |
93
|
2 |
|
$this->io = $io; |
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns list of events the plugin is subscribed to. |
98
|
|
|
* @return array list of events |
99
|
|
|
*/ |
100
|
1 |
|
public static function getSubscribedEvents() |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
1 |
|
ScriptEvents::POST_AUTOLOAD_DUMP => [ |
104
|
|
|
['onPostAutoloadDump', 0], |
105
|
1 |
|
], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* This is the main function. |
111
|
|
|
* @param Event $event |
112
|
|
|
*/ |
113
|
|
|
public function onPostAutoloadDump(Event $event) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->io->writeError('<info>Assembling config files</info>'); |
116
|
|
|
|
117
|
|
|
/// scan packages |
118
|
|
|
foreach ($this->getPackages() as $package) { |
119
|
|
|
if ($package instanceof \Composer\Package\CompletePackageInterface) { |
120
|
|
|
$this->processPackage($package); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
$this->processPackage($this->composer->getPackage()); |
124
|
|
|
|
125
|
|
|
$this->assembleParams(); |
126
|
|
|
define('COMPOSER_CONFIG_PLUGIN_DIR', $this->getOutputDir()); |
127
|
|
|
$this->assembleConfigs(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Scans the given package and collects extensions data. |
132
|
|
|
* @param PackageInterface $package |
133
|
|
|
*/ |
134
|
|
|
public function processPackage(PackageInterface $package) |
135
|
|
|
{ |
136
|
|
|
$extra = $package->getExtra(); |
137
|
|
|
$files = isset($extra[self::EXTRA_OPTION_NAME]) ? $extra[self::EXTRA_OPTION_NAME] : null; |
138
|
|
|
if ($package->getType() !== self::PACKAGE_TYPE && is_null($files)) { |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$extension = [ |
143
|
|
|
'name' => $package->getName(), |
144
|
|
|
'version' => $package->getVersion(), |
145
|
|
|
]; |
146
|
|
|
if ($package->getVersion() === '9999999-dev') { |
147
|
|
|
$reference = $package->getSourceReference() ?: $package->getDistReference(); |
148
|
|
|
if ($reference) { |
149
|
|
|
$extension['reference'] = $reference; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$aliases = array_merge( |
154
|
|
|
$this->prepareAliases($package, 'psr-0'), |
155
|
|
|
$this->prepareAliases($package, 'psr-4') |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
if (isset($files['defines'])) { |
159
|
|
|
foreach ((array) $files['defines'] as $file) { |
160
|
|
|
$this->readConfigFile($package, $file); |
161
|
|
|
} |
162
|
|
|
unset($files['defines']); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (isset($files['params'])) { |
166
|
|
|
foreach ((array) $files['params'] as $file) { |
167
|
|
|
$this->rawParams[] = $this->readConfigFile($package, $file); |
168
|
|
|
} |
169
|
|
|
unset($files['params']); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
$this->raw[$package->getName()] = [ |
173
|
|
|
'package' => $package, |
174
|
|
|
'extension' => $extension, |
175
|
|
|
'aliases' => $aliases, |
176
|
|
|
'files' => (array) $files, |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function assembleParams() |
181
|
|
|
{ |
182
|
|
|
$this->assembleFile('params', $this->rawParams); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function assembleConfigs() |
186
|
|
|
{ |
187
|
|
|
$allAliases = []; |
|
|
|
|
188
|
|
|
$extensions = []; |
|
|
|
|
189
|
|
|
$rawConfigs = [ |
190
|
|
|
'aliases' => [], |
191
|
|
|
'extensions' => [], |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
foreach ($this->raw as $name => $info) { |
195
|
|
|
$rawConfigs['extensions'][] = [ |
196
|
|
|
$name => $info['extension'], |
197
|
|
|
]; |
198
|
|
|
|
199
|
|
|
$aliases = $info['aliases']; |
200
|
|
|
$rawConfigs['aliases'][] = $aliases; |
201
|
|
|
|
202
|
|
|
foreach ($info['files'] as $name => $pathes) { |
203
|
|
|
foreach ((array) $pathes as $path) { |
204
|
|
|
$rawConfigs[$name][] = $this->readConfigFile($info['package'], $path); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
foreach ($rawConfigs as $name => $configs) { |
210
|
|
|
if (!in_array($name, ['params', 'aliases', 'extensions'], true)) { |
211
|
|
|
$configs[] = [ |
212
|
|
|
'params' => $this->data['params'], |
213
|
|
|
'aliases' => $this->data['aliases'], |
214
|
|
|
]; |
215
|
|
|
} |
216
|
|
|
$this->assembleFile($name, $configs); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function assembleFile($name, array $configs) |
221
|
|
|
{ |
222
|
|
|
$this->data[$name] = call_user_func_array([Helper::class, 'mergeConfig'], $configs); |
223
|
|
|
$this->writeFile($name, (array) $this->data[$name]); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Read extra config. |
228
|
|
|
* @param string $file |
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
protected function readConfigFile(PackageInterface $package, $file) |
232
|
|
|
{ |
233
|
|
|
$skippable = false; |
234
|
|
|
if (strncmp($file, '?', 1) === 0) { |
235
|
|
|
$skippable = true; |
236
|
|
|
$file = substr($file, 1); |
237
|
|
|
} |
238
|
|
|
$__path = $this->preparePath($package, $file); |
239
|
|
|
if (!file_exists($__path)) { |
240
|
|
|
if ($skippable) { |
241
|
|
|
return []; |
242
|
|
|
} else { |
243
|
|
|
$this->io->writeError('<error>Non existent extension config file</error> ' . $file . ' in ' . $package->getName()); |
244
|
|
|
exit(1); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
extract($this->data); |
248
|
|
|
|
249
|
|
|
return (array) require $__path; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Prepare aliases. |
254
|
|
|
* |
255
|
|
|
* @param PackageInterface $package |
256
|
|
|
* @param string 'psr-0' or 'psr-4' |
257
|
|
|
* @return array |
258
|
|
|
*/ |
259
|
|
|
protected function prepareAliases(PackageInterface $package, $psr) |
260
|
|
|
{ |
261
|
|
|
$autoload = $package->getAutoload(); |
262
|
|
|
if (empty($autoload[$psr])) { |
263
|
|
|
return []; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$aliases = []; |
267
|
|
|
foreach ($autoload[$psr] as $name => $path) { |
268
|
|
|
if (is_array($path)) { |
269
|
|
|
// ignore psr-4 autoload specifications with multiple search paths |
270
|
|
|
// we can not convert them into aliases as they are ambiguous |
271
|
|
|
continue; |
272
|
|
|
} |
273
|
|
|
$name = str_replace('\\', '/', trim($name, '\\')); |
274
|
|
|
$path = $this->preparePath($package, $path); |
275
|
|
|
$path = $this->substitutePath($path, $this->getBaseDir(), self::BASE_DIR_SAMPLE); |
276
|
|
|
if ('psr-0' === $psr) { |
277
|
|
|
$path .= '/' . $name; |
278
|
|
|
} |
279
|
|
|
$aliases["@$name"] = $path; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $aliases; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Substitute path with alias if applicable. |
287
|
|
|
* @param string $path |
288
|
|
|
* @param string $dir |
289
|
|
|
* @param string $alias |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
|
public function substitutePath($path, $dir, $alias) |
293
|
|
|
{ |
294
|
|
|
return (substr($path, 0, strlen($dir) + 1) === $dir . '/') ? $alias . substr($path, strlen($dir)) : $path; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function preparePath(PackageInterface $package, $path) |
298
|
|
|
{ |
299
|
|
|
if (!$this->getFilesystem()->isAbsolutePath($path)) { |
300
|
|
|
$prefix = $package instanceof RootPackageInterface ? $this->getBaseDir() : $this->getVendorDir() . '/' . $package->getPrettyName(); |
301
|
|
|
$path = $prefix . '/' . $path; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $this->getFilesystem()->normalizePath($path); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Get output dir. |
309
|
|
|
* @return string |
310
|
|
|
*/ |
311
|
|
|
public function getOutputDir() |
312
|
|
|
{ |
313
|
|
|
return $this->getVendorDir() . DIRECTORY_SEPARATOR . static::OUTPUT_PATH; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Build full path to write file for a given filename. |
318
|
|
|
* @param string $filename |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
public function buildOutputPath($filename) |
322
|
|
|
{ |
323
|
|
|
return $this->getOutputDir() . DIRECTORY_SEPARATOR . $filename . '.php'; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Writes config file. |
328
|
|
|
* @param string $filename |
329
|
|
|
* @param array $data |
330
|
|
|
*/ |
331
|
|
|
protected function writeFile($filename, array $data) |
332
|
|
|
{ |
333
|
|
|
$path = $this->buildOutputPath($filename); |
334
|
|
|
if (!file_exists(dirname($path))) { |
335
|
|
|
mkdir(dirname($path), 0777, true); |
336
|
|
|
} |
337
|
|
|
$array = str_replace("'" . self::BASE_DIR_SAMPLE, '$baseDir . \'', Helper::exportVar($data)); |
338
|
|
|
file_put_contents($path, "<?php\n\n\$baseDir = dirname(dirname(dirname(__DIR__)));\n\nreturn $array;\n"); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Sets [[packages]]. |
343
|
|
|
* @param PackageInterface[] $packages |
344
|
|
|
*/ |
345
|
2 |
|
public function setPackages(array $packages) |
346
|
|
|
{ |
347
|
2 |
|
$this->packages = $packages; |
348
|
2 |
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Gets [[packages]]. |
352
|
|
|
* @return \Composer\Package\PackageInterface[] |
353
|
|
|
*/ |
354
|
1 |
|
public function getPackages() |
355
|
|
|
{ |
356
|
1 |
|
if ($this->packages === null) { |
357
|
|
|
$this->packages = $this->findPackages(); |
358
|
|
|
} |
359
|
|
|
|
360
|
1 |
|
return $this->packages; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
protected $plainList = []; |
364
|
|
|
protected $orderedList = []; |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Returns ordered list of packages: |
368
|
|
|
* - listed earlier in the composer.json will get earlier in the list |
369
|
|
|
* - childs before parents. |
370
|
|
|
* @return \Composer\Package\PackageInterface[] |
371
|
|
|
*/ |
372
|
|
|
public function findPackages() |
373
|
|
|
{ |
374
|
|
|
$root = $this->composer->getPackage(); |
375
|
|
|
$this->plainList[$root->getName()] = $root; |
376
|
|
|
foreach ($this->composer->getRepositoryManager()->getLocalRepository()->getCanonicalPackages() as $package) { |
377
|
|
|
$this->plainList[$package->getName()] = $package; |
378
|
|
|
} |
379
|
|
|
$this->orderedList = []; |
380
|
|
|
$this->iteratePackage($root, true); |
381
|
|
|
$res = []; |
382
|
|
|
foreach ($this->orderedList as $name) { |
383
|
|
|
$res[] = $this->plainList[$name]; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
return $res; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Iterates through package dependencies. |
391
|
|
|
* @param PackageInterface $package to iterate |
392
|
|
|
* @param bool $includingDev process development dependencies, defaults to not process |
393
|
|
|
*/ |
394
|
|
|
public function iteratePackage(PackageInterface $package, $includingDev = false) |
395
|
|
|
{ |
396
|
|
|
$this->iterateLinks($package->getRequires()); |
397
|
|
|
if ($includingDev) { |
398
|
|
|
$this->iterateLinks($package->getDevRequires()); |
399
|
|
|
} |
400
|
|
|
$name = $package->getName(); |
401
|
|
|
if (!isset($this->orderedList[$name])) { |
402
|
|
|
$this->orderedList[$name] = $name; |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Iterates given list of dependencies. |
408
|
|
|
* @param array $links |
409
|
|
|
*/ |
410
|
|
|
public function iterateLinks(array $links) |
411
|
|
|
{ |
412
|
|
|
foreach ($links as $link) { |
413
|
|
|
$target = $link->getTarget(); |
414
|
|
|
if (isset($this->plainList[$target]) && !isset($this->orderedList[$target])) { |
415
|
|
|
$this->iteratePackage($this->plainList[$target]); |
416
|
|
|
} |
417
|
|
|
} |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Get absolute path to package base dir. |
422
|
|
|
* @return string |
423
|
|
|
*/ |
424
|
|
|
public function getBaseDir() |
425
|
|
|
{ |
426
|
|
|
if ($this->baseDir === null) { |
427
|
|
|
$this->baseDir = dirname($this->getVendorDir()); |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
return $this->baseDir; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
/** |
434
|
|
|
* Get absolute path to composer vendor dir. |
435
|
|
|
* @return string |
436
|
|
|
*/ |
437
|
|
|
public function getVendorDir() |
438
|
|
|
{ |
439
|
|
|
if ($this->vendorDir === null) { |
440
|
|
|
$dir = $this->composer->getConfig()->get('vendor-dir', '/'); |
441
|
|
|
$this->vendorDir = $this->getFilesystem()->normalizePath($dir); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
return $this->vendorDir; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Getter for filesystem utility. |
449
|
|
|
* @return Filesystem |
450
|
|
|
*/ |
451
|
|
|
public function getFilesystem() |
452
|
|
|
{ |
453
|
|
|
if ($this->filesystem === null) { |
454
|
|
|
$this->filesystem = new Filesystem(); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
return $this->filesystem; |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.