1
|
|
|
<?php namespace Arcanedev\Composer\Entities\PackageTraits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Composer\Entities\PluginState; |
4
|
|
|
use Arcanedev\Composer\Utilities\NestedArray; |
5
|
|
|
use Composer\Package\RootPackageInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait ExtraTrait |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Composer\Entities\PackageTraits |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait ExtraTrait |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Main Functions |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* Merge extra config into a RootPackage. |
21
|
|
|
* |
22
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
23
|
|
|
* @param \Arcanedev\Composer\Entities\PluginState $state |
24
|
|
|
*/ |
25
|
|
|
protected function mergeExtra(RootPackageInterface $root, PluginState $state) |
26
|
|
|
{ |
27
|
|
|
$extra = $this->package->getExtra(); |
|
|
|
|
28
|
|
|
unset($extra['merge-plugin']); |
29
|
|
|
|
30
|
|
|
if ($state->shouldMergeExtra() && ! empty($extra)) { |
31
|
|
|
/** @var \Composer\Package\RootPackageInterface $unwrapped */ |
32
|
|
|
$unwrapped = static::unwrapIfNeeded($root, 'setExtra'); |
33
|
|
|
$unwrapped->setExtra( |
34
|
|
|
$this->getExtra($unwrapped, $state, $extra) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get extra config. |
41
|
|
|
* |
42
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
43
|
|
|
* @param \Arcanedev\Composer\Entities\PluginState $state |
44
|
|
|
* @param array $extra |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
private function getExtra( |
49
|
|
|
RootPackageInterface $root, PluginState $state, $extra |
50
|
|
|
) { |
51
|
|
|
$rootExtra = $root->getExtra(); |
52
|
|
|
|
53
|
|
|
if ($state->replaceDuplicateLinks()) { |
54
|
|
|
return self::mergeExtraArray($state->shouldMergeExtraDeep(), $rootExtra, $extra); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ( ! $state->shouldMergeExtraDeep()) { |
58
|
|
|
foreach (array_intersect(array_keys($extra), array_keys($rootExtra)) as $key) { |
59
|
|
|
$this->logger->info( |
|
|
|
|
60
|
|
|
"Ignoring duplicate <comment>{$key}</comment> in ". |
61
|
|
|
"<comment>{$this->path}</comment> extra config." |
|
|
|
|
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return self::mergeExtraArray($state->shouldMergeExtraDeep(), $extra, $rootExtra); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Merges two arrays either via arrayMergeDeep or via array_merge. |
71
|
|
|
* |
72
|
|
|
* @param bool $mergeDeep |
73
|
|
|
* @param array $array1 |
74
|
|
|
* @param array $array2 |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
private static function mergeExtraArray($mergeDeep, $array1, $array2) |
79
|
|
|
{ |
80
|
|
|
return $mergeDeep |
81
|
|
|
? NestedArray::mergeDeep($array1, $array2) |
82
|
|
|
: array_merge($array1, $array2); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: