Completed
Push — master ( 5e447a...e8aa6a )
by
unknown
03:32
created

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