-
Notifications
You must be signed in to change notification settings - Fork 160
/
MergePlugin.php
396 lines (357 loc) · 12.8 KB
/
MergePlugin.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php
/**
* This file is part of the Composer Merge plugin.
*
* Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors
*
* This software may be modified and distributed under the terms of the MIT
* license. See the LICENSE file for details.
*/
namespace Wikimedia\Composer\Merge\V2;
use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\EventDispatcher\Event as BaseEvent;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\Factory;
use Composer\Installer;
use Composer\Installer\PackageEvent;
use Composer\Installer\PackageEvents;
use Composer\IO\IOInterface;
use Composer\Package\RootPackageInterface;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event as ScriptEvent;
use Composer\Script\ScriptEvents;
/**
* Composer plugin that allows merging multiple composer.json files.
*
* When installed, this plugin will look for a "merge-plugin" key in the
* composer configuration's "extra" section. The value for this key is
* a set of options configuring the plugin.
*
* An "include" setting is required. The value of this setting can be either
* a single value or an array of values. Each value is treated as a glob()
* pattern identifying additional composer.json style configuration files to
* merge into the configuration for the current compser execution.
*
* The "autoload", "autoload-dev", "conflict", "provide", "replace",
* "repositories", "require", "require-dev", and "suggest" sections of the
* found configuration files will be merged into the root package
* configuration as though they were directly included in the top-level
* composer.json file.
*
* If included files specify conflicting package versions for "require" or
* "require-dev", the normal Composer dependency solver process will be used
* to attempt to resolve the conflict. Specifying the 'replace' key as true will
* change this default behaviour so that the last-defined version of a package
* will win, allowing for force-overrides of package defines.
*
* By default the "extra" section is not merged. This can be enabled by
* setitng the 'merge-extra' key to true. In normal mode, when the same key is
* found in both the original and the imported extra section, the version in
* the original config is used and the imported version is skipped. If
* 'replace' mode is active, this behaviour changes so the imported version of
* the key is used, replacing the version in the original config.
*
*
* @code
* {
* "require": {
* "wikimedia/composer-merge-plugin": "dev-master"
* },
* "extra": {
* "merge-plugin": {
* "include": [
* "composer.local.json"
* ]
* }
* }
* }
* @endcode
*
* @author Bryan Davis <bd808@bd808.com>
*/
class MergePlugin implements PluginInterface, EventSubscriberInterface
{
/**
* Official package name
*/
public const PACKAGE_NAME = 'wikimedia/composer-merge-plugin';
/**
* Priority that plugin uses to register callbacks.
*/
private const CALLBACK_PRIORITY = 50000;
/**
* @var Composer $composer
*/
protected $composer;
/**
* @var PluginState $state
*/
protected $state;
/**
* @var Logger $logger
*/
protected $logger;
/**
* Files that have already been fully processed
*
* @var array<string, bool> $loaded
*/
protected $loaded = [];
/**
* Files that have already been partially processed
*
* @var array<string, bool> $loadedNoDev
*/
protected $loadedNoDev = [];
/**
* Nested packages to restrict update operations.
*
* @var array<string, bool> $updateAllowList
*/
protected $updateAllowList = [];
/**
* {@inheritdoc}
*/
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->state = new PluginState($this->composer);
$this->logger = new Logger('merge-plugin', $io);
}
/**
* {@inheritdoc}
*/
public function deactivate(Composer $composer, IOInterface $io)
{
}
/**
* {@inheritdoc}
*/
public function uninstall(Composer $composer, IOInterface $io)
{
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
PluginEvents::INIT =>
['onInit', self::CALLBACK_PRIORITY],
PackageEvents::POST_PACKAGE_INSTALL =>
['onPostPackageInstall', self::CALLBACK_PRIORITY],
ScriptEvents::POST_INSTALL_CMD =>
['onPostInstallOrUpdate', self::CALLBACK_PRIORITY],
ScriptEvents::POST_UPDATE_CMD =>
['onPostInstallOrUpdate', self::CALLBACK_PRIORITY],
ScriptEvents::PRE_AUTOLOAD_DUMP =>
['onInstallUpdateOrDump', self::CALLBACK_PRIORITY],
ScriptEvents::PRE_INSTALL_CMD =>
['onInstallUpdateOrDump', self::CALLBACK_PRIORITY],
ScriptEvents::PRE_UPDATE_CMD =>
['onInstallUpdateOrDump', self::CALLBACK_PRIORITY],
];
}
/**
* Get list of packages to restrict update operations.
*
* @return string[]
* @see \Composer\Installer::setUpdateAllowList()
*/
public function getUpdateAllowList()
{
return array_keys($this->updateAllowList);
}
/**
* Handle an event callback for initialization.
*
* @param BaseEvent $event
*/
public function onInit(BaseEvent $event)
{
$this->state->loadSettings();
// It is not possible to know if the user specified --dev or --no-dev
// so assume it is false. The dev section will be merged later when
// the other events fire.
$this->state->setDevMode(false);
$this->mergeFiles($this->state->getIncludes(), false);
$this->mergeFiles($this->state->getRequires(), true);
}
/**
* Handle an event callback for an install, update or dump command by
* checking for "merge-plugin" in the "extra" data and merging package
* contents if found.
*
* @param ScriptEvent $event
*/
public function onInstallUpdateOrDump(ScriptEvent $event)
{
$this->state->loadSettings();
$this->state->setDevMode($event->isDevMode());
$this->mergeFiles($this->state->getIncludes(), false);
$this->mergeFiles($this->state->getRequires(), true);
if ($event->getName() === ScriptEvents::PRE_AUTOLOAD_DUMP) {
$this->state->setDumpAutoloader(true);
$flags = $event->getFlags();
if (isset($flags['optimize'])) {
$this->state->setOptimizeAutoloader($flags['optimize']);
}
}
}
/**
* Find configuration files matching the configured glob patterns and
* merge their contents with the master package.
*
* @param array $patterns List of files/glob patterns
* @param bool $required Are the patterns required to match files?
* @throws MissingFileException when required and a pattern returns no
* results
*/
protected function mergeFiles(array $patterns, $required = false)
{
$root = $this->composer->getPackage();
$files = array_map(
static function ($files, $pattern) use ($required) {
if ($required && !$files) {
throw new MissingFileException(
"merge-plugin: No files matched required '{$pattern}'"
);
}
return $files;
},
array_map('glob', $patterns),
$patterns
);
foreach (array_reduce($files, 'array_merge', []) as $path) {
$this->mergeFile($root, $path);
}
}
/**
* Read a JSON file and merge its contents
*
* @param RootPackageInterface $root
* @param string $path
*/
protected function mergeFile(RootPackageInterface $root, $path)
{
if (isset($this->loaded[$path]) ||
(isset($this->loadedNoDev[$path]) && !$this->state->isDevMode())
) {
$this->logger->debug(
"Already merged <comment>$path</comment> completely"
);
return;
}
$package = new ExtraPackage($path, $this->composer, $this->logger);
if (isset($this->loadedNoDev[$path])) {
$this->logger->info(
"Loading -dev sections of <comment>{$path}</comment>..."
);
$package->mergeDevInto($root, $this->state);
} else {
$this->logger->info("Loading <comment>{$path}</comment>...");
$package->mergeInto($root, $this->state);
}
$requirements = $package->getMergedRequirements();
if (!empty($requirements)) {
$this->updateAllowList = array_replace(
$this->updateAllowList,
array_fill_keys($requirements, true)
);
}
if ($this->state->isDevMode()) {
$this->loaded[$path] = true;
} else {
$this->loadedNoDev[$path] = true;
}
if ($this->state->recurseIncludes()) {
$this->mergeFiles($package->getIncludes(), false);
$this->mergeFiles($package->getRequires(), true);
}
}
/**
* Handle an event callback following installation of a new package by
* checking to see if the package that was installed was our plugin.
*
* @param PackageEvent $event
*/
public function onPostPackageInstall(PackageEvent $event)
{
$op = $event->getOperation();
if ($op instanceof InstallOperation) {
$package = $op->getPackage()->getName();
if ($package === self::PACKAGE_NAME) {
$this->logger->info('composer-merge-plugin installed');
$this->state->setFirstInstall(true);
$this->state->setLocked(
$event->getComposer()->getLocker()->isLocked()
);
}
}
}
/**
* Handle an event callback following an install or update command. If our
* plugin was installed during the run then trigger an update command to
* process any merge-patterns in the current config.
*
* @param ScriptEvent $event
*/
public function onPostInstallOrUpdate(ScriptEvent $event)
{
// @codeCoverageIgnoreStart
if ($this->state->isFirstInstall()) {
$this->state->setFirstInstall(false);
$requirements = $this->getUpdateAllowList();
if (empty($requirements)) {
return;
}
$this->logger->log("\n".'<info>Running composer update to apply merge settings</info>');
$lockBackup = null;
$lock = null;
if (!$this->state->isComposer1()) {
$file = Factory::getComposerFile();
$lock = Factory::getLockFile($file);
if (file_exists($lock)) {
$lockBackup = file_get_contents($lock);
}
}
$config = $this->composer->getConfig();
$preferSource = $config->get('preferred-install') === 'source';
$preferDist = $config->get('preferred-install') === 'dist';
$installer = Installer::create(
$event->getIO(),
// Create a new Composer instance to ensure full processing of
// the merged files.
Factory::create($event->getIO(), null, false)
);
$installer->setPreferSource($preferSource);
$installer->setPreferDist($preferDist);
$installer->setDevMode($event->isDevMode());
$installer->setDumpAutoloader($this->state->shouldDumpAutoloader());
$installer->setOptimizeAutoloader(
$this->state->shouldOptimizeAutoloader()
);
$installer->setUpdate(true);
if ($this->state->isComposer1()) {
// setUpdateWhitelist() only exists in composer 1.x. Configure as to run phan against composer 2.x
// @phan-suppress-next-line PhanUndeclaredMethod
$installer->setUpdateWhitelist($requirements);
} else {
$installer->setUpdateAllowList($requirements);
}
$status = $installer->run();
if (( $status !== 0 ) && $lockBackup && $lock && !$this->state->isComposer1()) {
$this->logger->log(
"\n".'<error>'.
'Update to apply merge settings failed, reverting '.$lock.' to its original content.'.
'</error>'
);
file_put_contents($lock, $lockBackup);
}
}
// @codeCoverageIgnoreEnd
}
}
// vim:sw=4:ts=4:sts=4:et: