Completed
Pull Request — master (#114)
by Fabian
27:09 queued 02:10
created

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