Completed
Pull Request — master (#136)
by
unknown
23:13
created

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