Completed
Pull Request — master (#11)
by ARCANEDEV
03:20
created

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