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