Completed
Pull Request — master (#163)
by
unknown
20:55
created

PluginState::shouldCreateReplace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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