Completed
Push — master ( 69b875...70df7e )
by ARCANEDEV
9s
created

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