Completed
Push — master ( 022d76...a280db )
by ARCANEDEV
13:03
created

PluginState::shouldMergeDev()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php namespace Arcanedev\Composer\Entities;
2
3
use Composer\Composer;
4
5
/**
6
 * Class     PluginState
7
 *
8
 * @package  Arcanedev\Composer\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class PluginState
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /** @var \Composer\Composer */
18
    protected $composer;
19
20
    /** @var array */
21
    protected $includes = [];
22
23
    /** @var array */
24
    protected $requires = [];
25
26
    /** @var array */
27
    protected $duplicateLinks = [];
28
29
    /** @var bool */
30
    protected $devMode = false;
31
32
    /** @var bool */
33
    protected $recurse = true;
34
35
    /** @var bool */
36
    protected $replace = false;
37
38
    /**
39
     * Whether to merge the -dev sections.
40
     *
41
     * @var bool
42
     */
43
    protected $mergeDev = true;
44
45
    /**
46
     * Whether to merge the extra section.
47
     *
48
     * By default, the extra section is not merged and there will be many cases where
49
     * the merge of the extra section is performed too late to be of use to other plugins.
50
     * When enabled, merging uses one of two strategies - either 'first wins' or 'last wins'.
51
     * When enabled, 'first wins' is the default behaviour. If Replace mode is activated
52
     * then 'last wins' is used.
53
     *
54
     * @var bool
55
     */
56
    protected $mergeExtra = false;
57
58
    /**
59
     * Whether to merge the extra section in a deep / recursive way.
60
     *
61
     * By default the extra section is merged with array_merge() and duplicate keys are ignored.
62
     * When enabled this allows to merge the arrays recursively using the following rule:
63
     *   Integer keys are merged, while array values are replaced where the later values overwrite the former.
64
     *
65
     * This is useful especially for the extra section when plugins use larger structures like a 'patches' key with
66
     * the packages as sub-keys and the patches as values.
67
     *
68
     * When 'replace' mode is activated the order of array merges is exchanged.
69
     *
70
     * @var bool
71
     */
72
    protected $mergeExtraDeep = false;
73
74
    /** @var bool */
75
    protected $firstInstall = false;
76
77
    /** @var bool */
78
    protected $locked = false;
79
80
    /** @var bool */
81
    protected $dumpAutoloader = false;
82
83
    /** @var bool */
84
    protected $optimizeAutoloader = false;
85
86
    /* ------------------------------------------------------------------------------------------------
87
     |  Constructor
88
     | ------------------------------------------------------------------------------------------------
89
     */
90
    /**
91
     * Make PluginState instance.
92
     *
93
     * @param  \Composer\Composer  $composer
94
     */
95
    public function __construct(Composer $composer)
96
    {
97
        $this->composer = $composer;
98
    }
99
100
    /* ------------------------------------------------------------------------------------------------
101
     |  Getters & Setters
102 175
     | ------------------------------------------------------------------------------------------------
103
     */
104 175
    /**
105 175
     * Get list of filenames and/or glob patterns to include.
106
     *
107
     * @return array
108
     */
109
    public function getIncludes()
110
    {
111
        return $this->includes;
112
    }
113
114
    /**
115
     * Set the first install flag.
116 140
     *
117
     * @param  bool  $flag
118 140
     *
119
     * @return self
120
     */
121
    public function setFirstInstall($flag)
122
    {
123
        $this->firstInstall = (bool) $flag;
124
125
        return $this;
126
    }
127
128 10
    /**
129
     * Is this the first time that the plugin has been installed ?
130 10
     *
131
     * @return bool
132 10
     */
133
    public function isFirstInstall()
134
    {
135
        return $this->firstInstall;
136
    }
137
138
    /**
139
     * Set the locked flag.
140 150
     *
141
     * @param  bool  $flag
142 150
     *
143
     * @return self
144
     */
145
    public function setLocked($flag)
146
    {
147
        $this->locked = (bool) $flag;
148
149
        return $this;
150
    }
151
152 15
    /**
153
     * Was a lockfile present when the plugin was installed ?
154 15
     *
155
     * @return bool
156 15
     */
157
    public function isLocked()
158
    {
159
        return $this->locked;
160
    }
161
162
    /**
163
     * Should an update be forced ?
164 20
     *
165
     * @return bool
166 20
     */
167
    public function forceUpdate()
168
    {
169
        return ! $this->isLocked();
170
    }
171
172
    /**
173
     * Set the devMode flag.
174 5
     *
175
     * @param  bool  $flag
176 5
     *
177
     * @return self
178
     */
179
    public function setDevMode($flag)
180
    {
181
        $this->devMode = (bool) $flag;
182
183
        return $this;
184
    }
185
186 140
    /**
187
     * Should devMode settings be processed ?
188 140
     *
189
     * @return bool
190 140
     */
191
    public function isDevMode()
192
    {
193
        return $this->shouldMergeDev() && $this->devMode;
194
    }
195
196
    /**
197
     * Should devMode settings be merged?
198 135
     *
199
     * @return bool
200 135
     */
201
    public function shouldMergeDev()
202
    {
203
        return $this->mergeDev;
204
    }
205
206
    /**
207
     * Set the dumpAutoloader flag.
208
     *
209
     * @param  bool  $flag
210 140
     *
211
     * @return self
212 140
     */
213
    public function setDumpAutoloader($flag)
214 140
    {
215
        $this->dumpAutoloader = (bool) $flag;
216
217
        return $this;
218
    }
219
220
    /**
221
     * Is the autoloader file supposed to be written out ?
222 5
     *
223
     * @return bool
224 5
     */
225
    public function shouldDumpAutoloader()
226
    {
227
        return $this->dumpAutoloader;
228
    }
229
230
    /**
231
     * Set the optimizeAutoloader flag.
232
     *
233
     * @param  bool  $flag
234 140
     *
235
     * @return self
236 140
     */
237
    public function setOptimizeAutoloader($flag)
238 140
    {
239
        $this->optimizeAutoloader = (bool) $flag;
240
241
        return $this;
242
    }
243
244
    /**
245
     * Should the autoloader be optimized ?
246 5
     *
247
     * @return bool
248 5
     */
249
    public function shouldOptimizeAutoloader()
250
    {
251
        return $this->optimizeAutoloader;
252
    }
253
254
    /**
255
     * Add duplicate packages.
256 135
     *
257
     * @param  string  $type
258 135
     * @param  array   $packages
259
     *
260
     * @return self
261
     */
262
    public function addDuplicateLinks($type, array $packages)
263
    {
264
        if ( ! isset($this->duplicateLinks[$type])) {
265
            $this->duplicateLinks[$type] = [];
266
        }
267
268
        $this->duplicateLinks[$type] = array_merge(
269 80
            $this->duplicateLinks[$type],
270
            $packages
271 80
        );
272 80
273 64
        return $this;
274
    }
275 80
276 80
    /**
277
     * Should includes be recursively processed ?
278 64
     *
279
     * @return bool
280 80
     */
281
    public function recurseIncludes()
282
    {
283
        return $this->recurse;
284
    }
285
286
    /**
287
     * Get list of filenames and/or glob patterns to require
288 135
     *
289
     * @return array
290 135
     */
291
    public function getRequires()
292
    {
293
        return $this->requires;
294
    }
295
296
    /**
297
     * Get duplicate packages.
298 140
     *
299
     * @param  string  $type
300 140
     *
301
     * @return array
302
     */
303
    public function getDuplicateLinks($type)
304
    {
305
        return isset($this->duplicateLinks[$type])
306
            ? $this->duplicateLinks[$type]
307
            : [];
308
    }
309
310 135
    /**
311
     * Should duplicate links be replaced in a 'last definition wins' order ?
312 135
     *
313 124
     * @return bool
314 135
     */
315
    public function replaceDuplicateLinks()
316
    {
317
        return $this->replace;
318
    }
319
320
    /**
321
     * Should the extra section be merged ?
322 110
     *
323
     * By default, the extra section is not merged and there will be many cases where
324 110
     * the merge of the extra section is performed too late to be of use to other plugins.
325
     * When enabled, merging uses one of two strategies - either 'first wins' or 'last wins'.
326
     * When enabled, '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 extra section be merged deep / recursively?
338 135
     *
339
     * @return bool
340 135
     */
341
    public function shouldMergeExtraDeep()
342
    {
343
        return $this->mergeExtraDeep;
344
    }
345
346
    /* ------------------------------------------------------------------------------------------------
347
     |  Main Functions
348 30
     | ------------------------------------------------------------------------------------------------
349
     */
350 30
    /**
351
     * Load plugin settings.
352
     */
353
    public function loadSettings()
354
    {
355
        $extra                     = $this->composer->getPackage()->getExtra();
356
        $config                    = $this->mergeConfig($extra);
357
        $this->includes            = is_array($config['include']) ? $config['include'] : [$config['include']];
358
        $this->requires            = is_array($config['require']) ? $config['require'] : [$config['require']];
359
        $this->recurse             = (bool) $config['recurse'];
360 140
        $this->replace             = (bool) $config['replace'];
361
        $this->mergeDev            = (bool) $config['merge-dev'];
362 140
        $this->mergeExtra          = (bool) $config['merge-extra'];
363 140
        $this->mergeExtraDeep      = (bool) $config['merge-extra-deep'];
364 140
365 140
    }
366 140
367 140
    /* ------------------------------------------------------------------------------------------------
368 140
     |  Other Functions
369 140
     | ------------------------------------------------------------------------------------------------
370 140
     */
371 140
    /**
372
     * Merge config.
373 140
     *
374
     * @param  array  $extra
375
     *
376
     * @return array
377
     */
378
    private function mergeConfig(array $extra)
379
    {
380
        return array_merge(
381
            [
382
                'include'              => [],
383
                'require'              => [],
384
                'recurse'              => true,
385
                'replace'              => false,
386 140
                'prepend-repositories' => false,
387
                'merge-dev'            => true,
388 140
                'merge-extra'          => false,
389
                'merge-extra-deep'     => false,
390 140
            ],
391 112
            isset($extra['merge-plugin']) ? $extra['merge-plugin'] : []
392 112
        );
393 112
    }
394
}
395