Completed
Push — master ( 0a4184...fc073f )
by ARCANEDEV
11s
created

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