Completed
Pull Request — master (#120)
by
unknown
39:17 queued 14:20
created

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