1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Composer Merge plugin. |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2015 Bryan Davis, Wikimedia Foundation, and contributors |
6
|
|
|
* |
7
|
|
|
* This software may be modified and distributed under the terms of the MIT |
8
|
|
|
* license. See the LICENSE file for details. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Wikimedia\Composer; |
12
|
|
|
|
13
|
|
|
use Wikimedia\Composer\Merge\ExtraPackage; |
14
|
|
|
use Wikimedia\Composer\Merge\MissingFileException; |
15
|
|
|
use Wikimedia\Composer\Merge\PluginState; |
16
|
|
|
|
17
|
|
|
use Composer\Composer; |
18
|
|
|
use Composer\DependencyResolver\Operation\InstallOperation; |
19
|
|
|
use Composer\EventDispatcher\Event as BaseEvent; |
20
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface; |
21
|
|
|
use Composer\Factory; |
22
|
|
|
use Composer\Installer; |
23
|
|
|
use Composer\Installer\InstallerEvent; |
24
|
|
|
use Composer\Installer\InstallerEvents; |
25
|
|
|
use Composer\Installer\PackageEvent; |
26
|
|
|
use Composer\Installer\PackageEvents; |
27
|
|
|
use Composer\IO\IOInterface; |
28
|
|
|
use Composer\Package\RootPackageInterface; |
29
|
|
|
use Composer\Plugin\PluginInterface; |
30
|
|
|
use Composer\Script\Event as ScriptEvent; |
31
|
|
|
use Composer\Script\ScriptEvents; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Composer plugin that allows merging multiple composer.json files. |
35
|
|
|
* |
36
|
|
|
* When installed, this plugin will look for a "merge-plugin" key in the |
37
|
|
|
* composer configuration's "extra" section. The value for this key is |
38
|
|
|
* a set of options configuring the plugin. |
39
|
|
|
* |
40
|
|
|
* An "include" setting is required. The value of this setting can be either |
41
|
|
|
* a single value or an array of values. Each value is treated as a glob() |
42
|
|
|
* pattern identifying additional composer.json style configuration files to |
43
|
|
|
* merge into the configuration for the current compser execution. |
44
|
|
|
* |
45
|
|
|
* The "autoload", "autoload-dev", "conflict", "provide", "replace", |
46
|
|
|
* "repositories", "require", "require-dev", and "suggest" sections of the |
47
|
|
|
* found configuration files will be merged into the root package |
48
|
|
|
* configuration as though they were directly included in the top-level |
49
|
|
|
* composer.json file. |
50
|
|
|
* |
51
|
|
|
* If included files specify conflicting package versions for "require" or |
52
|
|
|
* "require-dev", the normal Composer dependency solver process will be used |
53
|
|
|
* to attempt to resolve the conflict. Specifying the 'replace' key as true will |
54
|
|
|
* change this default behaviour so that the last-defined version of a package |
55
|
|
|
* will win, allowing for force-overrides of package defines. |
56
|
|
|
* |
57
|
|
|
* By default the "extra" section is not merged. This can be enabled by |
58
|
|
|
* setitng the 'merge-extra' key to true. In normal mode, when the same key is |
59
|
|
|
* found in both the original and the imported extra section, the version in |
60
|
|
|
* the original config is used and the imported version is skipped. If |
61
|
|
|
* 'replace' mode is active, this behaviour changes so the imported version of |
62
|
|
|
* the key is used, replacing the version in the original config. |
63
|
|
|
* |
64
|
|
|
* |
65
|
|
|
* @code |
66
|
|
|
* { |
67
|
|
|
* "require": { |
68
|
|
|
* "wikimedia/composer-merge-plugin": "dev-master" |
69
|
|
|
* }, |
70
|
|
|
* "extra": { |
71
|
|
|
* "merge-plugin": { |
72
|
|
|
* "include": [ |
73
|
|
|
* "composer.local.json" |
74
|
|
|
* ] |
75
|
|
|
* } |
76
|
|
|
* } |
77
|
|
|
* } |
78
|
|
|
* @endcode |
79
|
|
|
* |
80
|
|
|
* @author Bryan Davis <[email protected]> |
81
|
|
|
*/ |
82
|
|
|
class MergePlugin implements PluginInterface, EventSubscriberInterface |
83
|
|
|
{ |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Offical package name |
87
|
|
|
*/ |
88
|
|
|
const PACKAGE_NAME = 'wikimedia/composer-merge-plugin'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Name of the composer 1.1 init event. |
92
|
|
|
*/ |
93
|
|
|
const COMPAT_PLUGINEVENTS_INIT = 'init'; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Name of the pre-merge command |
97
|
|
|
*/ |
98
|
|
|
const PRE_MERGE_CMD = 'pre-merge-cmd'; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Name of the post-merge command |
102
|
|
|
*/ |
103
|
|
|
const POST_MERGE_CMD = 'post-merge-cmd'; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Priority that plugin uses to register callbacks. |
107
|
|
|
*/ |
108
|
|
|
const CALLBACK_PRIORITY = 50000; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var Composer $composer |
112
|
|
|
*/ |
113
|
|
|
protected $composer; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var PluginState $state |
117
|
|
|
*/ |
118
|
|
|
protected $state; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var Logger $logger |
122
|
|
|
*/ |
123
|
|
|
protected $logger; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Files that have already been fully processed |
127
|
|
|
* |
128
|
|
|
* @var string[] $loaded |
129
|
|
|
*/ |
130
|
|
|
protected $loaded = array(); |
131
|
|
|
|
132
|
195 |
|
/** |
133
|
|
|
* Files that have already been partially processed |
134
|
195 |
|
* |
135
|
195 |
|
* @var string[] $loadedNoDev |
136
|
195 |
|
*/ |
137
|
195 |
|
protected $loadedNoDev = array(); |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
5 |
|
public function activate(Composer $composer, IOInterface $io) |
143
|
|
|
{ |
144
|
|
|
$this->composer = $composer; |
145
|
|
|
$this->state = new PluginState($this->composer); |
146
|
|
|
$this->logger = new Logger('merge-plugin', $io); |
147
|
|
|
} |
148
|
5 |
|
|
149
|
5 |
|
/** |
150
|
5 |
|
* {@inheritdoc} |
151
|
5 |
|
*/ |
152
|
5 |
|
public static function getSubscribedEvents() |
153
|
5 |
|
{ |
154
|
5 |
|
return array( |
155
|
5 |
|
// Use our own constant to make this event optional. Once |
156
|
5 |
|
// composer-1.1 is required, this can use PluginEvents::INIT |
157
|
5 |
|
// instead. |
158
|
5 |
|
self::COMPAT_PLUGINEVENTS_INIT => |
159
|
5 |
|
array('onInit', self::CALLBACK_PRIORITY), |
160
|
5 |
|
InstallerEvents::PRE_DEPENDENCIES_SOLVING => |
161
|
5 |
|
array('onDependencySolve', self::CALLBACK_PRIORITY), |
162
|
5 |
|
PackageEvents::POST_PACKAGE_INSTALL => |
163
|
5 |
|
array('onPostPackageInstall', self::CALLBACK_PRIORITY), |
164
|
5 |
|
ScriptEvents::POST_INSTALL_CMD => |
165
|
|
|
array('onPostInstallOrUpdate', self::CALLBACK_PRIORITY), |
166
|
|
|
ScriptEvents::POST_UPDATE_CMD => |
167
|
|
|
array('onPostInstallOrUpdate', self::CALLBACK_PRIORITY), |
168
|
|
|
ScriptEvents::PRE_AUTOLOAD_DUMP => |
169
|
|
|
array('onInstallUpdateOrDump', self::CALLBACK_PRIORITY), |
170
|
|
|
ScriptEvents::PRE_INSTALL_CMD => |
171
|
|
|
array('onInstallUpdateOrDump', self::CALLBACK_PRIORITY), |
172
|
70 |
|
ScriptEvents::PRE_UPDATE_CMD => |
173
|
|
|
array('onInstallUpdateOrDump', self::CALLBACK_PRIORITY), |
174
|
70 |
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
70 |
|
* Handle an event callback for initialization. |
179
|
70 |
|
* |
180
|
70 |
|
* @param \Composer\EventDispatcher\Event $event |
181
|
70 |
|
*/ |
182
|
|
|
public function onInit(BaseEvent $event) |
183
|
|
|
{ |
184
|
|
|
$this->state->loadSettings(); |
185
|
|
|
// It is not possible to know if the user specified --dev or --no-dev |
186
|
|
|
// so assume it is false. The dev section will be merged later when |
187
|
|
|
// the other events fire. |
188
|
|
|
$this->state->setDevMode(false); |
189
|
|
|
$this->mergeFiles($this->state->getIncludes(), false); |
190
|
175 |
|
$this->mergeFiles($this->state->getRequires(), true); |
191
|
|
|
} |
192
|
175 |
|
|
193
|
175 |
|
/** |
194
|
175 |
|
* Handle an event callback for an install, update or dump command by |
195
|
175 |
|
* checking for "merge-plugin" in the "extra" data and merging package |
196
|
|
|
* contents if found. |
197
|
170 |
|
* |
198
|
170 |
|
* @param ScriptEvent $event |
199
|
170 |
|
*/ |
200
|
170 |
|
public function onInstallUpdateOrDump(ScriptEvent $event) |
201
|
170 |
|
{ |
202
|
170 |
|
$this->state->loadSettings(); |
203
|
170 |
|
$this->state->setDevMode($event->isDevMode()); |
204
|
170 |
|
$this->mergeFiles($this->state->getIncludes(), false); |
205
|
|
|
$this->mergeFiles($this->state->getRequires(), true); |
206
|
|
|
|
207
|
|
|
if ($event->getName() === ScriptEvents::PRE_AUTOLOAD_DUMP) { |
208
|
|
|
$this->state->setDumpAutoloader(true); |
209
|
|
|
$flags = $event->getFlags(); |
210
|
|
|
if (isset($flags['optimize'])) { |
211
|
|
|
$this->state->setOptimizeAutoloader($flags['optimize']); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
} |
215
|
175 |
|
|
216
|
|
|
/** |
217
|
175 |
|
* Find configuration files matching the configured glob patterns and |
218
|
|
|
* merge their contents with the master package. |
219
|
175 |
|
* |
220
|
175 |
|
* @param array $patterns List of files/glob patterns |
221
|
175 |
|
* @param bool $required Are the patterns required to match files? |
222
|
5 |
|
* @throws MissingFileException when required and a pattern returns no |
223
|
5 |
|
* results |
224
|
5 |
|
*/ |
225
|
|
|
protected function mergeFiles(array $patterns, $required = false) |
226
|
170 |
|
{ |
227
|
175 |
|
// Dispatch any optional pre-merge commands |
228
|
175 |
|
$this->composer->getEventDispatcher()->dispatchScript(self::PRE_MERGE_CMD, true); |
229
|
|
|
|
230
|
175 |
|
$root = $this->composer->getPackage(); |
231
|
|
|
|
232
|
175 |
|
$files = array_map( |
233
|
170 |
|
function ($files, $pattern) use ($required) { |
234
|
175 |
|
if ($required && !$files) { |
235
|
175 |
|
throw new MissingFileException( |
236
|
|
|
"merge-plugin: No files matched required '{$pattern}'" |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
return $files; |
240
|
|
|
}, |
241
|
|
|
array_map('glob', $patterns), |
242
|
|
|
$patterns |
243
|
170 |
|
); |
244
|
|
|
|
245
|
170 |
|
foreach (array_reduce($files, 'array_merge', array()) as $path) { |
246
|
170 |
|
$this->mergeFile($root, $path); |
247
|
170 |
|
} |
248
|
170 |
|
|
249
|
170 |
|
// Dispatch any optional post-merge commands |
250
|
170 |
|
$this->composer->getEventDispatcher()->dispatchScript(self::POST_MERGE_CMD, true); |
251
|
170 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
170 |
|
* Read a JSON file and merge its contents |
255
|
|
|
* |
256
|
170 |
|
* @param RootPackageInterface $root |
257
|
70 |
|
* @param string $path |
258
|
70 |
|
*/ |
259
|
70 |
|
protected function mergeFile(RootPackageInterface $root, $path) |
260
|
70 |
|
{ |
261
|
70 |
|
$this->composer->getEventDispatcher()->dispatchScript(ScriptEvents::PRE_MERGE_CMD, true); |
262
|
170 |
|
|
263
|
170 |
|
if (isset($this->loaded[$path]) || |
264
|
|
|
(isset($this->loadedNoDev[$path]) && !$this->state->isDevMode()) |
265
|
|
|
) { |
266
|
170 |
|
$this->logger->debug( |
267
|
165 |
|
"Already merged <comment>$path</comment> completely" |
268
|
165 |
|
); |
269
|
75 |
|
return; |
270
|
|
|
} |
271
|
|
|
|
272
|
170 |
|
$package = new ExtraPackage($path, $this->composer, $this->logger); |
273
|
165 |
|
|
274
|
165 |
|
if (isset($this->loadedNoDev[$path])) { |
275
|
165 |
|
$this->logger->info( |
276
|
170 |
|
"Loading -dev sections of <comment>{$path}</comment>..." |
277
|
|
|
); |
278
|
|
|
$package->mergeDevInto($root, $this->state); |
279
|
|
|
} else { |
280
|
|
|
$this->logger->info("Loading <comment>{$path}</comment>..."); |
281
|
|
|
$package->mergeInto($root, $this->state); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if ($this->state->isDevMode()) { |
285
|
|
|
$this->loaded[$path] = true; |
286
|
170 |
|
} else { |
287
|
|
|
$this->loadedNoDev[$path] = true; |
288
|
170 |
|
} |
289
|
170 |
|
|
290
|
25 |
|
if ($this->state->recurseIncludes()) { |
291
|
25 |
|
$this->mergeFiles($package->getIncludes(), false); |
292
|
25 |
|
$this->mergeFiles($package->getRequires(), true); |
293
|
25 |
|
} |
294
|
170 |
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Handle an event callback for pre-dependency solving phase of an install |
298
|
|
|
* or update by adding any duplicate package dependencies found during |
299
|
|
|
* initial merge processing to the request that will be processed by the |
300
|
170 |
|
* dependency solver. |
301
|
165 |
|
* |
302
|
10 |
|
* @param InstallerEvent $event |
303
|
10 |
|
*/ |
304
|
10 |
|
public function onDependencySolve(InstallerEvent $event) |
305
|
10 |
|
{ |
306
|
165 |
|
$request = $event->getRequest(); |
307
|
165 |
|
foreach ($this->state->getDuplicateLinks('require') as $link) { |
308
|
170 |
|
$this->logger->info( |
309
|
|
|
"Adding dependency <comment>{$link}</comment>" |
310
|
|
|
); |
311
|
|
|
$request->install($link->getTarget(), $link->getConstraint()); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
// Issue #113: Check devMode of event rather than our global state. |
315
|
|
|
// Composer fires the PRE_DEPENDENCIES_SOLVING event twice for |
316
|
15 |
|
// `--no-dev` operations to decide which packages are dev only |
317
|
|
|
// requirements. |
318
|
15 |
|
if ($this->state->shouldMergeDev() && $event->isDevMode()) { |
319
|
15 |
|
foreach ($this->state->getDuplicateLinks('require-dev') as $link) { |
320
|
15 |
|
$this->logger->info( |
321
|
15 |
|
"Adding dev dependency <comment>{$link}</comment>" |
322
|
10 |
|
); |
323
|
10 |
|
$request->install($link->getTarget(), $link->getConstraint()); |
324
|
10 |
|
} |
325
|
10 |
|
} |
326
|
10 |
|
} |
327
|
10 |
|
|
328
|
15 |
|
/** |
329
|
15 |
|
* Handle an event callback following installation of a new package by |
330
|
|
|
* checking to see if the package that was installed was our plugin. |
331
|
|
|
* |
332
|
|
|
* @param PackageEvent $event |
333
|
|
|
*/ |
334
|
|
|
public function onPostPackageInstall(PackageEvent $event) |
335
|
|
|
{ |
336
|
|
|
$op = $event->getOperation(); |
337
|
|
|
if ($op instanceof InstallOperation) { |
338
|
170 |
|
$package = $op->getPackage()->getName(); |
339
|
|
|
if ($package === self::PACKAGE_NAME) { |
340
|
|
|
$this->logger->info('composer-merge-plugin installed'); |
341
|
|
|
$this->state->setFirstInstall(true); |
342
|
|
|
$this->state->setLocked( |
343
|
|
|
$event->getComposer()->getLocker()->isLocked() |
344
|
|
|
); |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Handle an event callback following an install or update command. If our |
351
|
|
|
* plugin was installed during the run then trigger an update command to |
352
|
|
|
* process any merge-patterns in the current config. |
353
|
|
|
* |
354
|
|
|
* @param ScriptEvent $event |
355
|
|
|
*/ |
356
|
|
|
public function onPostInstallOrUpdate(ScriptEvent $event) |
357
|
|
|
{ |
358
|
|
|
// @codeCoverageIgnoreStart |
359
|
|
|
if ($this->state->isFirstInstall()) { |
360
|
|
|
$this->state->setFirstInstall(false); |
361
|
|
|
$this->logger->info( |
362
|
|
|
'<comment>' . |
363
|
|
|
'Running additional update to apply merge settings' . |
364
|
|
|
'</comment>' |
365
|
|
|
); |
366
|
|
|
|
367
|
|
|
$config = $this->composer->getConfig(); |
368
|
|
|
|
369
|
|
|
$preferSource = $config->get('preferred-install') == 'source'; |
370
|
|
|
$preferDist = $config->get('preferred-install') == 'dist'; |
371
|
|
|
|
372
|
|
|
$installer = Installer::create( |
373
|
|
|
$event->getIO(), |
374
|
|
|
// Create a new Composer instance to ensure full processing of |
375
|
|
|
// the merged files. |
376
|
|
|
Factory::create($event->getIO(), null, false) |
377
|
|
|
); |
378
|
|
|
|
379
|
170 |
|
$installer->setPreferSource($preferSource); |
380
|
|
|
$installer->setPreferDist($preferDist); |
381
|
|
|
$installer->setDevMode($event->isDevMode()); |
382
|
|
|
$installer->setDumpAutoloader($this->state->shouldDumpAutoloader()); |
383
|
|
|
$installer->setOptimizeAutoloader( |
384
|
|
|
$this->state->shouldOptimizeAutoloader() |
385
|
|
|
); |
386
|
|
|
|
387
|
|
|
if ($this->state->forceUpdate()) { |
388
|
|
|
// Force update mode so that new packages are processed rather |
389
|
|
|
// than just telling the user that composer.json and |
390
|
|
|
// composer.lock don't match. |
391
|
|
|
$installer->setUpdate(true); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$installer->run(); |
395
|
|
|
} |
396
|
|
|
// @codeCoverageIgnoreEnd |
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
// vim:sw=4:ts=4:sts=4:et: |
400
|
|
|
|