Debian Codegraph @

vendor/wikimedia/composer-merge-plugin/src/PluginState.php

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