Completed
Push — master ( 5dfc66...6e95cd )
by Bryan
03:24
created

PluginState::shouldMergeExtraDeep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
     * 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 prepend repositories to repository manager.
79
     *
80
     * @var bool $prependRepositories
81
     */
82
    protected $prependRepositories = false;
83
84
    /**
85
     * Whether to merge the extra section in a deep / recursive way.
86
     *
87
     * By default the extra section is merged with array_merge() and duplicate
88
     * keys are ignored. When enabled this allows to merge the arrays recursively
89
     * using the following rule: Integer keys are merged, while array values are
90
     * replaced where the later values overwrite the former.
91
     *
92
     * This is useful especially for the extra section when plugins use larger
93
     * structures like a 'patches' key with the packages as sub-keys and the
94
     * patches as values.
95
     *
96
     * When 'replace' mode is activated the order of array merges is exchanged.
97
     *
98
     * @var bool $mergeExtraDeep
99
     */
100
    protected $mergeExtraDeep = 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 140
    public function __construct(Composer $composer)
126
    {
127 140
        $this->composer = $composer;
128 140
    }
129
130
    /**
131
     * Load plugin settings
132
     */
133 105
    public function loadSettings()
134
    {
135 105
        $extra = $this->composer->getPackage()->getExtra();
136 105
        $config = array_merge(
137
            array(
138 105
                'include' => array(),
139 105
                'require' => array(),
140 105
                'recurse' => true,
141 105
                'replace' => false,
142 105
                'prepend-repositories' => false,
143 105
                'merge-dev' => true,
144 105
                'merge-extra' => false,
145 105
                'merge-extra-deep' => false,
146 105
            ),
147 105
            isset($extra['merge-plugin']) ? $extra['merge-plugin'] : array()
148 105
        );
149
150 105
        $this->includes = (is_array($config['include'])) ?
151 105
            $config['include'] : array($config['include']);
152 105
        $this->requires = (is_array($config['require'])) ?
153 105
            $config['require'] : array($config['require']);
154 105
        $this->recurse = (bool)$config['recurse'];
155 105
        $this->replace = (bool)$config['replace'];
156 105
        $this->prependRepositories = (bool)$config['prepend-repositories'];
157 105
        $this->mergeDev = (bool)$config['merge-dev'];
158 105
        $this->mergeExtra = (bool)$config['merge-extra'];
159 105
        $this->mergeExtraDeep = (bool)$config['merge-extra-deep'];
160 105
    }
161
162
    /**
163
     * Get list of filenames and/or glob patterns to include
164
     *
165
     * @return array
166
     */
167 105
    public function getIncludes()
168
    {
169 105
        return $this->includes;
170
    }
171
172
    /**
173
     * Get list of filenames and/or glob patterns to require
174
     *
175
     * @return array
176
     */
177 105
    public function getRequires()
178
    {
179 105
        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 115
    public function isFirstInstall()
198
    {
199 115
        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 105
    public function setDevMode($flag)
238
    {
239 105
        $this->devMode = (bool)$flag;
240 105
    }
241
242
    /**
243
     * Should devMode settings be processed?
244
     *
245
     * @return bool
246
     */
247 100
    public function isDevMode()
248
    {
249 100
        return $this->mergeDev && $this->devMode;
250
    }
251
252
    /**
253
     * Set the dumpAutoloader flag
254
     *
255
     * @param bool $flag
256
     */
257 105
    public function setDumpAutoloader($flag)
258
    {
259 105
        $this->dumpAutoloader = (bool)$flag;
260 105
    }
261
262
    /**
263
     * Is the autoloader file supposed to be written out?
264
     *
265
     * @return bool
266
     */
267 5
    public function shouldDumpAutoloader()
268
    {
269 5
        return $this->dumpAutoloader;
270
    }
271
272
    /**
273
     * Set the optimizeAutoloader flag
274
     *
275
     * @param bool $flag
276
     */
277 105
    public function setOptimizeAutoloader($flag)
278
    {
279 105
        $this->optimizeAutoloader = (bool)$flag;
280 105
    }
281
282
    /**
283
     * Should the autoloader be optimized?
284
     *
285
     * @return bool
286
     */
287 5
    public function shouldOptimizeAutoloader()
288
    {
289 5
        return $this->optimizeAutoloader;
290
    }
291
292
    /**
293
     * Add duplicate packages
294
     *
295
     * @param string $type Package type
296
     * @param array $packages
297
     */
298 65
    public function addDuplicateLinks($type, array $packages)
299
    {
300 65
        if (!isset($this->duplicateLinks[$type])) {
301 65
            $this->duplicateLinks[$type] = array();
302 65
        }
303 65
        $this->duplicateLinks[$type] =
304 65
            array_merge($this->duplicateLinks[$type], $packages);
305 65
    }
306
307
    /**
308
     * Get duplicate packages
309
     *
310
     * @param string $type Package type
311
     * @return array
312
     */
313 100
    public function getDuplicateLinks($type)
314
    {
315 100
        return isset($this->duplicateLinks[$type]) ?
316 100
            $this->duplicateLinks[$type] : array();
317
    }
318
319
    /**
320
     * Should includes be recursively processed?
321
     *
322
     * @return bool
323
     */
324 100
    public function recurseIncludes()
325
    {
326 100
        return $this->recurse;
327
    }
328
329
    /**
330
     * Should duplicate links be replaced in a 'last definition wins' order?
331
     *
332
     * @return bool
333
     */
334 40
    public function replaceDuplicateLinks()
335
    {
336 40
        return $this->replace;
337
    }
338
339
    /**
340
     * Should the extra section be merged?
341
     *
342
     * By default, the extra section is not merged and there will be many
343
     * cases where the merge of the extra section is performed too late
344
     * to be of use to other plugins. When enabled, merging uses one of
345
     * two strategies - either 'first wins' or 'last wins'. When enabled,
346
     * 'first wins' is the default behaviour. If Replace mode is activated
347
     * then 'last wins' is used.
348
     *
349
     * @return bool
350
     */
351 100
    public function shouldMergeExtra()
352
    {
353 100
        return $this->mergeExtra;
354
    }
355
356
    /**
357
     * Should the merger prepend repositories to repository manager (instead of adding them to end of the list).
358
     *
359
     * @return bool
360
     */
361 100
    public function shouldPrependRepositories()
362
    {
363 100
        return $this->prependRepositories;
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 15
    public function shouldMergeExtraDeep()
383
    {
384 15
        return $this->mergeExtraDeep;
385
    }
386
}
387
// vim:sw=4:ts=4:sts=4:et:
388