Completed
Push — master ( 516969...f657d9 )
by ARCANEDEV
8s
created

PluginState::shouldMergeExtraDeep()   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 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 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
     * Whether to prepend repositories to repository manager.
88
     *
89
     * @var bool
90
     */
91
    protected $prependRepositories = false;
92
93
    /* ------------------------------------------------------------------------------------------------
94
     |  Constructor
95
     | ------------------------------------------------------------------------------------------------
96
     */
97
    /**
98
     * Make PluginState instance.
99
     *
100
     * @param  \Composer\Composer  $composer
101
     */
102 175
    public function __construct(Composer $composer)
103
    {
104 175
        $this->composer = $composer;
105 175
    }
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 140
    public function getIncludes()
117
    {
118 140
        return $this->includes;
119
    }
120
121
    /**
122
     * Set the first install flag.
123
     *
124
     * @param  bool  $flag
125
     *
126
     * @return self
127
     */
128 10
    public function setFirstInstall($flag)
129
    {
130 10
        $this->firstInstall = (bool) $flag;
131
132 10
        return $this;
133
    }
134
135
    /**
136
     * Is this the first time that the plugin has been installed ?
137
     *
138
     * @return bool
139
     */
140 150
    public function isFirstInstall()
141
    {
142 150
        return $this->firstInstall;
143
    }
144
145
    /**
146
     * Set the locked flag.
147
     *
148
     * @param  bool  $flag
149
     *
150
     * @return self
151
     */
152 15
    public function setLocked($flag)
153
    {
154 15
        $this->locked = (bool) $flag;
155
156 15
        return $this;
157
    }
158
159
    /**
160
     * Was a lockfile present when the plugin was installed ?
161
     *
162
     * @return bool
163
     */
164 20
    public function isLocked()
165
    {
166 20
        return $this->locked;
167
    }
168
169
    /**
170
     * Should an update be forced ?
171
     *
172
     * @return bool
173
     */
174 5
    public function forceUpdate()
175
    {
176 5
        return ! $this->isLocked();
177
    }
178
179
    /**
180
     * Set the devMode flag.
181
     *
182
     * @param  bool  $flag
183
     *
184
     * @return self
185
     */
186 140
    public function setDevMode($flag)
187
    {
188 140
        $this->devMode = (bool) $flag;
189
190 140
        return $this;
191
    }
192
193
    /**
194
     * Should devMode settings be processed ?
195
     *
196
     * @return bool
197
     */
198 135
    public function isDevMode()
199
    {
200 135
        return $this->mergeDev && $this->devMode;
201
    }
202
203
    /**
204
     * Set the dumpAutoloader flag.
205
     *
206
     * @param  bool  $flag
207
     *
208
     * @return self
209
     */
210 140
    public function setDumpAutoloader($flag)
211
    {
212 140
        $this->dumpAutoloader = (bool) $flag;
213
214 140
        return $this;
215
    }
216
217
    /**
218
     * Is the autoloader file supposed to be written out ?
219
     *
220
     * @return bool
221
     */
222 5
    public function shouldDumpAutoloader()
223
    {
224 5
        return $this->dumpAutoloader;
225
    }
226
227
    /**
228
     * Set the optimizeAutoloader flag.
229
     *
230
     * @param  bool  $flag
231
     *
232
     * @return self
233
     */
234 140
    public function setOptimizeAutoloader($flag)
235
    {
236 140
        $this->optimizeAutoloader = (bool) $flag;
237
238 140
        return $this;
239
    }
240
241
    /**
242
     * Should the autoloader be optimized ?
243
     *
244
     * @return bool
245
     */
246 5
    public function shouldOptimizeAutoloader()
247
    {
248 5
        return $this->optimizeAutoloader;
249
    }
250
251
    /**
252
     * Should the merger prepend repositories to repository manager (instead of adding them to end of the list).
253
     *
254
     * @return bool
255
     */
256 135
    public function shouldPrependRepositories()
257
    {
258 135
        return $this->prependRepositories;
259
    }
260
261
    /**
262
     * Add duplicate packages.
263
     *
264
     * @param  string  $type
265
     * @param  array   $packages
266
     *
267
     * @return self
268
     */
269 80
    public function addDuplicateLinks($type, array $packages)
270
    {
271 80
        if ( ! isset($this->duplicateLinks[$type])) {
272 80
            $this->duplicateLinks[$type] = [];
273 64
        }
274
275 80
        $this->duplicateLinks[$type] = array_merge(
276 80
            $this->duplicateLinks[$type],
277
            $packages
278 64
        );
279
280 80
        return $this;
281
    }
282
283
    /**
284
     * Should includes be recursively processed ?
285
     *
286
     * @return bool
287
     */
288 135
    public function recurseIncludes()
289
    {
290 135
        return $this->recurse;
291
    }
292
293
    /**
294
     * Get list of filenames and/or glob patterns to require
295
     *
296
     * @return array
297
     */
298 140
    public function getRequires()
299
    {
300 140
        return $this->requires;
301
    }
302
303
    /**
304
     * Get duplicate packages.
305
     *
306
     * @param  string  $type
307
     *
308
     * @return array
309
     */
310 135
    public function getDuplicateLinks($type)
311
    {
312 135
        return isset($this->duplicateLinks[$type])
313 124
            ? $this->duplicateLinks[$type]
314 135
            : [];
315
    }
316
317
    /**
318
     * Should duplicate links be replaced in a 'last definition wins' order ?
319
     *
320
     * @return bool
321
     */
322 110
    public function replaceDuplicateLinks()
323
    {
324 110
        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 135
    public function shouldMergeExtra()
339
    {
340 135
        return $this->mergeExtra;
341
    }
342
343
    /**
344
     * Should the extra section be merged deep / recursively?
345
     *
346
     * @return bool
347
     */
348 30
    public function shouldMergeExtraDeep()
349
    {
350 30
        return $this->mergeExtraDeep;
351
    }
352
353
    /* ------------------------------------------------------------------------------------------------
354
     |  Main Functions
355
     | ------------------------------------------------------------------------------------------------
356
     */
357
    /**
358
     * Load plugin settings.
359
     */
360 140
    public function loadSettings()
361
    {
362 140
        $extra                     = $this->composer->getPackage()->getExtra();
363 140
        $config                    = $this->mergeConfig($extra);
364 140
        $this->includes            = is_array($config['include']) ? $config['include'] : [$config['include']];
365 140
        $this->requires            = is_array($config['require']) ? $config['require'] : [$config['require']];
366 140
        $this->recurse             = (bool) $config['recurse'];
367 140
        $this->replace             = (bool) $config['replace'];
368 140
        $this->prependRepositories = (bool) $config['prepend-repositories'];
369 140
        $this->mergeDev            = (bool) $config['merge-dev'];
370 140
        $this->mergeExtra          = (bool) $config['merge-extra'];
371 140
        $this->mergeExtraDeep      = (bool) $config['merge-extra-deep'];
372
373 140
    }
374
375
    /* ------------------------------------------------------------------------------------------------
376
     |  Other Functions
377
     | ------------------------------------------------------------------------------------------------
378
     */
379
    /**
380
     * Merge config.
381
     *
382
     * @param  array  $extra
383
     *
384
     * @return array
385
     */
386 140
    private function mergeConfig(array $extra)
387
    {
388 140
        return array_merge(
389
            [
390 140
                'include'              => [],
391 112
                'require'              => [],
392 112
                'recurse'              => true,
393 112
                'replace'              => false,
394 112
                'prepend-repositories' => false,
395 112
                'merge-dev'            => true,
396 112
                'merge-extra'          => false,
397 112
                'merge-extra-deep'     => false,
398 112
            ],
399 140
            isset($extra['merge-plugin']) ? $extra['merge-plugin'] : []
400 112
        );
401
    }
402
}
403