Completed
Push — master ( 210110...022d76 )
by ARCANEDEV
8s
created

PluginState::replaceDuplicateLinks()   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->mergeDev && $this->devMode;
194
    }
195
196
    /**
197
     * Set the dumpAutoloader flag.
198 135
     *
199
     * @param  bool  $flag
200 135
     *
201
     * @return self
202
     */
203
    public function setDumpAutoloader($flag)
204
    {
205
        $this->dumpAutoloader = (bool) $flag;
206
207
        return $this;
208
    }
209
210 140
    /**
211
     * Is the autoloader file supposed to be written out ?
212 140
     *
213
     * @return bool
214 140
     */
215
    public function shouldDumpAutoloader()
216
    {
217
        return $this->dumpAutoloader;
218
    }
219
220
    /**
221
     * Set the optimizeAutoloader flag.
222 5
     *
223
     * @param  bool  $flag
224 5
     *
225
     * @return self
226
     */
227
    public function setOptimizeAutoloader($flag)
228
    {
229
        $this->optimizeAutoloader = (bool) $flag;
230
231
        return $this;
232
    }
233
234 140
    /**
235
     * Should the autoloader be optimized ?
236 140
     *
237
     * @return bool
238 140
     */
239
    public function shouldOptimizeAutoloader()
240
    {
241
        return $this->optimizeAutoloader;
242
    }
243
244
    /**
245
     * Add duplicate packages.
246 5
     *
247
     * @param  string  $type
248 5
     * @param  array   $packages
249
     *
250
     * @return self
251
     */
252
    public function addDuplicateLinks($type, array $packages)
253
    {
254
        if ( ! isset($this->duplicateLinks[$type])) {
255
            $this->duplicateLinks[$type] = [];
256 135
        }
257
258 135
        $this->duplicateLinks[$type] = array_merge(
259
            $this->duplicateLinks[$type],
260
            $packages
261
        );
262
263
        return $this;
264
    }
265
266
    /**
267
     * Should includes be recursively processed ?
268
     *
269 80
     * @return bool
270
     */
271 80
    public function recurseIncludes()
272 80
    {
273 64
        return $this->recurse;
274
    }
275 80
276 80
    /**
277
     * Get list of filenames and/or glob patterns to require
278 64
     *
279
     * @return array
280 80
     */
281
    public function getRequires()
282
    {
283
        return $this->requires;
284
    }
285
286
    /**
287
     * Get duplicate packages.
288 135
     *
289
     * @param  string  $type
290 135
     *
291
     * @return array
292
     */
293
    public function getDuplicateLinks($type)
294
    {
295
        return isset($this->duplicateLinks[$type])
296
            ? $this->duplicateLinks[$type]
297
            : [];
298 140
    }
299
300 140
    /**
301
     * Should duplicate links be replaced in a 'last definition wins' order ?
302
     *
303
     * @return bool
304
     */
305
    public function replaceDuplicateLinks()
306
    {
307
        return $this->replace;
308
    }
309
310 135
    /**
311
     * Should the extra section be merged ?
312 135
     *
313 124
     * By default, the extra section is not merged and there will be many cases where
314 135
     * the merge of the extra section is performed too late to be of use to other plugins.
315
     * When enabled, merging uses one of two strategies - either 'first wins' or 'last wins'.
316
     * When enabled, 'first wins' is the default behaviour. If Replace mode is activated
317
     * then 'last wins' is used.
318
     *
319
     * @return bool
320
     */
321
    public function shouldMergeExtra()
322 110
    {
323
        return $this->mergeExtra;
324 110
    }
325
326
    /**
327
     * Should the extra section be merged deep / recursively?
328
     *
329
     * @return bool
330
     */
331
    public function shouldMergeExtraDeep()
332
    {
333
        return $this->mergeExtraDeep;
334
    }
335
336
    /* ------------------------------------------------------------------------------------------------
337
     |  Main Functions
338 135
     | ------------------------------------------------------------------------------------------------
339
     */
340 135
    /**
341
     * Load plugin settings.
342
     */
343
    public function loadSettings()
344
    {
345
        $extra                     = $this->composer->getPackage()->getExtra();
346
        $config                    = $this->mergeConfig($extra);
347
        $this->includes            = is_array($config['include']) ? $config['include'] : [$config['include']];
348 30
        $this->requires            = is_array($config['require']) ? $config['require'] : [$config['require']];
349
        $this->recurse             = (bool) $config['recurse'];
350 30
        $this->replace             = (bool) $config['replace'];
351
        $this->mergeDev            = (bool) $config['merge-dev'];
352
        $this->mergeExtra          = (bool) $config['merge-extra'];
353
        $this->mergeExtraDeep      = (bool) $config['merge-extra-deep'];
354
355
    }
356
357
    /* ------------------------------------------------------------------------------------------------
358
     |  Other Functions
359
     | ------------------------------------------------------------------------------------------------
360 140
     */
361
    /**
362 140
     * Merge config.
363 140
     *
364 140
     * @param  array  $extra
365 140
     *
366 140
     * @return array
367 140
     */
368 140
    private function mergeConfig(array $extra)
369 140
    {
370 140
        return array_merge(
371 140
            [
372
                'include'              => [],
373 140
                'require'              => [],
374
                'recurse'              => true,
375
                'replace'              => false,
376
                'prepend-repositories' => false,
377
                'merge-dev'            => true,
378
                'merge-extra'          => false,
379
                'merge-extra-deep'     => false,
380
            ],
381
            isset($extra['merge-plugin']) ? $extra['merge-plugin'] : []
382
        );
383
    }
384
}
385