Completed
Push — master ( 9a6301...9645a9 )
by Bryan
04:34
created

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