PluginState   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 397
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 30
lcom 3
cbo 2
dl 0
loc 397
ccs 85
cts 85
cp 1
rs 10
c 0
b 0
f 0

24 Methods

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