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\Merge; |
12
|
|
|
|
13
|
|
|
use Composer\Composer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Mutable plugin state |
17
|
|
|
* |
18
|
|
|
* @author Bryan Davis <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class PluginState |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Composer $composer |
24
|
|
|
*/ |
25
|
|
|
protected $composer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array $includes |
29
|
|
|
*/ |
30
|
|
|
protected $includes = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array $requires |
34
|
|
|
*/ |
35
|
|
|
protected $requires = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array $duplicateLinks |
39
|
|
|
*/ |
40
|
|
|
protected $duplicateLinks = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool $devMode |
44
|
|
|
*/ |
45
|
|
|
protected $devMode = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool $recurse |
49
|
|
|
*/ |
50
|
|
|
protected $recurse = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool $replace |
54
|
|
|
*/ |
55
|
|
|
protected $replace = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Whether to merge the -dev sections. |
59
|
|
|
* @var bool $mergeDev |
60
|
|
|
*/ |
61
|
|
|
protected $mergeDev = true; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Whether to merge the extra section. |
65
|
|
|
* |
66
|
|
|
* By default, the extra section is not merged and there will be many |
67
|
|
|
* cases where the merge of the extra section is performed too late |
68
|
|
|
* to be of use to other plugins. When enabled, merging uses one of |
69
|
|
|
* two strategies - either 'first wins' or 'last wins'. When enabled, |
70
|
|
|
* 'first wins' is the default behaviour. If Replace mode is activated |
71
|
|
|
* then 'last wins' is used. |
72
|
|
|
* |
73
|
|
|
* @var bool $mergeExtra |
74
|
|
|
*/ |
75
|
|
|
protected $mergeExtra = false; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Whether to merge the extra section in a deep / recursive way. |
79
|
|
|
* |
80
|
|
|
* By default the extra section is merged with array_merge() and duplicate |
81
|
|
|
* keys are ignored. When enabled this allows to merge the arrays recursively |
82
|
|
|
* using the following rule: Integer keys are merged, while array values are |
83
|
|
|
* replaced where the later values overwrite the former. |
84
|
|
|
* |
85
|
|
|
* This is useful especially for the extra section when plugins use larger |
86
|
|
|
* structures like a 'patches' key with the packages as sub-keys and the |
87
|
|
|
* patches as values. |
88
|
|
|
* |
89
|
|
|
* When 'replace' mode is activated the order of array merges is exchanged. |
90
|
|
|
* |
91
|
|
|
* @var bool $mergeExtraDeep |
92
|
|
|
*/ |
93
|
|
|
protected $mergeExtraDeep = false; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Whether to merge the scripts section. |
97
|
|
|
* |
98
|
|
|
* @var bool $mergeScripts |
99
|
|
|
*/ |
100
|
|
|
protected $mergeScripts = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var bool $firstInstall |
104
|
|
|
*/ |
105
|
|
|
protected $firstInstall = false; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var bool $locked |
109
|
|
|
*/ |
110
|
|
|
protected $locked = false; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var bool $dumpAutoloader |
114
|
|
|
*/ |
115
|
|
|
protected $dumpAutoloader = false; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var bool $optimizeAutoloader |
119
|
|
|
*/ |
120
|
|
|
protected $optimizeAutoloader = false; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Composer $composer |
124
|
|
|
*/ |
125
|
200 |
|
public function __construct(Composer $composer) |
126
|
|
|
{ |
127
|
200 |
|
$this->composer = $composer; |
128
|
200 |
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Load plugin settings |
132
|
|
|
*/ |
133
|
165 |
|
public function loadSettings() |
134
|
|
|
{ |
135
|
165 |
|
$extra = $this->composer->getPackage()->getExtra(); |
136
|
165 |
|
$config = array_merge( |
137
|
|
|
array( |
138
|
165 |
|
'include' => array(), |
139
|
165 |
|
'require' => array(), |
140
|
165 |
|
'recurse' => true, |
141
|
165 |
|
'replace' => false, |
142
|
165 |
|
'merge-dev' => true, |
143
|
165 |
|
'merge-extra' => false, |
144
|
165 |
|
'merge-extra-deep' => false, |
145
|
165 |
|
'merge-scripts' => false, |
146
|
165 |
|
), |
147
|
165 |
|
isset($extra['merge-plugin']) ? $extra['merge-plugin'] : array() |
148
|
165 |
|
); |
149
|
|
|
|
150
|
165 |
|
$this->includes = (is_array($config['include'])) ? |
151
|
165 |
|
$config['include'] : array($config['include']); |
152
|
165 |
|
$this->requires = (is_array($config['require'])) ? |
153
|
165 |
|
$config['require'] : array($config['require']); |
154
|
165 |
|
$this->recurse = (bool)$config['recurse']; |
155
|
165 |
|
$this->replace = (bool)$config['replace']; |
156
|
165 |
|
$this->mergeDev = (bool)$config['merge-dev']; |
157
|
165 |
|
$this->mergeExtra = (bool)$config['merge-extra']; |
158
|
165 |
|
$this->mergeExtraDeep = (bool)$config['merge-extra-deep']; |
159
|
165 |
|
$this->mergeScripts = (bool)$config['merge-scripts']; |
160
|
165 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get list of filenames and/or glob patterns to include |
164
|
|
|
* |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
165 |
|
public function getIncludes() |
168
|
|
|
{ |
169
|
165 |
|
return $this->includes; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get list of filenames and/or glob patterns to require |
174
|
|
|
* |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
165 |
|
public function getRequires() |
178
|
|
|
{ |
179
|
165 |
|
return $this->requires; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set the first install flag |
184
|
|
|
* |
185
|
|
|
* @param bool $flag |
186
|
|
|
*/ |
187
|
10 |
|
public function setFirstInstall($flag) |
188
|
|
|
{ |
189
|
10 |
|
$this->firstInstall = (bool)$flag; |
190
|
10 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Is this the first time that the plugin has been installed? |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
175 |
|
public function isFirstInstall() |
198
|
|
|
{ |
199
|
175 |
|
return $this->firstInstall; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set the locked flag |
204
|
|
|
* |
205
|
|
|
* @param bool $flag |
206
|
|
|
*/ |
207
|
15 |
|
public function setLocked($flag) |
208
|
|
|
{ |
209
|
15 |
|
$this->locked = (bool)$flag; |
210
|
15 |
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Was a lockfile present when the plugin was installed? |
214
|
|
|
* |
215
|
|
|
* @return bool |
216
|
|
|
*/ |
217
|
20 |
|
public function isLocked() |
218
|
|
|
{ |
219
|
20 |
|
return $this->locked; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Should an update be forced? |
224
|
|
|
* |
225
|
|
|
* @return true If packages are not locked |
226
|
|
|
*/ |
227
|
5 |
|
public function forceUpdate() |
228
|
|
|
{ |
229
|
5 |
|
return !$this->locked; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set the devMode flag |
234
|
|
|
* |
235
|
|
|
* @param bool $flag |
236
|
|
|
*/ |
237
|
165 |
|
public function setDevMode($flag) |
238
|
|
|
{ |
239
|
165 |
|
$this->devMode = (bool)$flag; |
240
|
165 |
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Should devMode settings be processed? |
244
|
|
|
* |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
160 |
|
public function isDevMode() |
248
|
|
|
{ |
249
|
160 |
|
return $this->shouldMergeDev() && $this->devMode; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Should devMode settings be merged? |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
160 |
|
public function shouldMergeDev() |
258
|
|
|
{ |
259
|
160 |
|
return $this->mergeDev; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Set the dumpAutoloader flag |
264
|
|
|
* |
265
|
|
|
* @param bool $flag |
266
|
|
|
*/ |
267
|
165 |
|
public function setDumpAutoloader($flag) |
268
|
|
|
{ |
269
|
165 |
|
$this->dumpAutoloader = (bool)$flag; |
270
|
165 |
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Is the autoloader file supposed to be written out? |
274
|
|
|
* |
275
|
|
|
* @return bool |
276
|
|
|
*/ |
277
|
5 |
|
public function shouldDumpAutoloader() |
278
|
|
|
{ |
279
|
5 |
|
return $this->dumpAutoloader; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Set the optimizeAutoloader flag |
284
|
|
|
* |
285
|
|
|
* @param bool $flag |
286
|
|
|
*/ |
287
|
165 |
|
public function setOptimizeAutoloader($flag) |
288
|
|
|
{ |
289
|
165 |
|
$this->optimizeAutoloader = (bool)$flag; |
290
|
165 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Should the autoloader be optimized? |
294
|
|
|
* |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
5 |
|
public function shouldOptimizeAutoloader() |
298
|
|
|
{ |
299
|
5 |
|
return $this->optimizeAutoloader; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Add duplicate packages |
304
|
|
|
* |
305
|
|
|
* @param string $type Package type |
306
|
|
|
* @param array $packages |
307
|
|
|
*/ |
308
|
85 |
|
public function addDuplicateLinks($type, array $packages) |
309
|
|
|
{ |
310
|
85 |
|
if (!isset($this->duplicateLinks[$type])) { |
311
|
85 |
|
$this->duplicateLinks[$type] = array(); |
312
|
85 |
|
} |
313
|
85 |
|
$this->duplicateLinks[$type] = |
314
|
85 |
|
array_merge($this->duplicateLinks[$type], $packages); |
315
|
85 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Get duplicate packages |
319
|
|
|
* |
320
|
|
|
* @param string $type Package type |
321
|
|
|
* @return array |
322
|
|
|
*/ |
323
|
160 |
|
public function getDuplicateLinks($type) |
324
|
|
|
{ |
325
|
160 |
|
return isset($this->duplicateLinks[$type]) ? |
326
|
160 |
|
$this->duplicateLinks[$type] : array(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Should includes be recursively processed? |
331
|
|
|
* |
332
|
|
|
* @return bool |
333
|
|
|
*/ |
334
|
160 |
|
public function recurseIncludes() |
335
|
|
|
{ |
336
|
160 |
|
return $this->recurse; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Should duplicate links be replaced in a 'last definition wins' order? |
341
|
|
|
* |
342
|
|
|
* @return bool |
343
|
|
|
*/ |
344
|
85 |
|
public function replaceDuplicateLinks() |
345
|
|
|
{ |
346
|
85 |
|
return $this->replace; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Should the extra section be merged? |
351
|
|
|
* |
352
|
|
|
* By default, the extra section is not merged and there will be many |
353
|
|
|
* cases where the merge of the extra section is performed too late |
354
|
|
|
* to be of use to other plugins. When enabled, merging uses one of |
355
|
|
|
* two strategies - either 'first wins' or 'last wins'. When enabled, |
356
|
|
|
* 'first wins' is the default behaviour. If Replace mode is activated |
357
|
|
|
* then 'last wins' is used. |
358
|
|
|
* |
359
|
|
|
* @return bool |
360
|
|
|
*/ |
361
|
160 |
|
public function shouldMergeExtra() |
362
|
|
|
{ |
363
|
160 |
|
return $this->mergeExtra; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Should the extra section be merged deep / recursively? |
368
|
|
|
* |
369
|
|
|
* By default the extra section is merged with array_merge() and duplicate |
370
|
|
|
* keys are ignored. When enabled this allows to merge the arrays recursively |
371
|
|
|
* using the following rule: Integer keys are merged, while array values are |
372
|
|
|
* replaced where the later values overwrite the former. |
373
|
|
|
* |
374
|
|
|
* This is useful especially for the extra section when plugins use larger |
375
|
|
|
* structures like a 'patches' key with the packages as sub-keys and the |
376
|
|
|
* patches as values. |
377
|
|
|
* |
378
|
|
|
* When 'replace' mode is activated the order of array merges is exchanged. |
379
|
|
|
* |
380
|
|
|
* @return bool |
381
|
|
|
*/ |
382
|
30 |
|
public function shouldMergeExtraDeep() |
383
|
|
|
{ |
384
|
30 |
|
return $this->mergeExtraDeep; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Should the scripts section be merged? |
390
|
|
|
* |
391
|
|
|
* By default, the scripts section is not merged. |
392
|
|
|
* |
393
|
|
|
* @return bool |
394
|
|
|
*/ |
395
|
160 |
|
public function shouldMergeScripts() |
396
|
|
|
{ |
397
|
160 |
|
return $this->mergeScripts; |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
// vim:sw=4:ts=4:sts=4:et: |
401
|
|
|
|