1
|
|
|
<?php namespace Arcanedev\Composer\Entities\PackageTraits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Composer\Entities\PluginState; |
4
|
|
|
use Arcanedev\Composer\Entities\StabilityFlags; |
5
|
|
|
use Composer\Package\RootPackageInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait RequiresTrait |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\Composer\Entities\PackageTraits |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
trait RequiresTrait |
14
|
|
|
{ |
15
|
|
|
/* ------------------------------------------------------------------------------------------------ |
16
|
|
|
| Main Functions |
17
|
|
|
| ------------------------------------------------------------------------------------------------ |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* Merge require into a RootPackage. |
21
|
|
|
* |
22
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
23
|
|
|
* @param \Arcanedev\Composer\Entities\PluginState $state |
24
|
|
|
*/ |
25
|
|
|
protected function mergeRequires(RootPackageInterface $root, PluginState $state) |
26
|
|
|
{ |
27
|
|
|
if ( ! empty($requires = $this->package->getRequires())) { |
|
|
|
|
28
|
|
|
$this->mergeStabilityFlags($root, $requires); |
29
|
|
|
|
30
|
|
|
$duplicateLinks = []; |
31
|
|
|
$requires = $this->replaceSelfVersionDependencies( |
|
|
|
|
32
|
|
|
'require', $requires, $root |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$root->setRequires($this->mergeLinks( |
36
|
|
|
$root->getRequires(), |
37
|
|
|
$requires, |
38
|
|
|
$state->replaceDuplicateLinks(), |
39
|
|
|
$duplicateLinks |
40
|
|
|
)); |
41
|
|
|
|
42
|
|
|
$state->addDuplicateLinks('require', $duplicateLinks); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Merge require-dev into RootPackage. |
48
|
|
|
* |
49
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
50
|
|
|
* @param \Arcanedev\Composer\Entities\PluginState $state |
51
|
|
|
*/ |
52
|
|
|
protected function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
53
|
|
|
{ |
54
|
|
|
if ( ! empty($requires = $this->package->getDevRequires())) { |
55
|
|
|
$this->mergeStabilityFlags($root, $requires); |
56
|
|
|
|
57
|
|
|
$duplicateLinks = []; |
58
|
|
|
$requires = $this->replaceSelfVersionDependencies( |
|
|
|
|
59
|
|
|
'require-dev', $requires, $root |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$root->setDevRequires($this->mergeLinks( |
63
|
|
|
$root->getDevRequires(), |
64
|
|
|
$requires, |
65
|
|
|
$state->replaceDuplicateLinks(), |
66
|
|
|
$duplicateLinks |
67
|
|
|
)); |
68
|
|
|
|
69
|
|
|
$state->addDuplicateLinks('require-dev', $duplicateLinks); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/* ------------------------------------------------------------------------------------------------ |
74
|
|
|
| Other Functions |
75
|
|
|
| ------------------------------------------------------------------------------------------------ |
76
|
|
|
*/ |
77
|
|
|
/** |
78
|
|
|
* Extract and merge stability flags from the given collection of |
79
|
|
|
* requires and merge them into a RootPackage. |
80
|
|
|
* |
81
|
|
|
* @param \Composer\Package\RootPackageInterface $root |
82
|
|
|
* @param \Composer\Package\Link[] $requires |
83
|
|
|
*/ |
84
|
|
|
protected function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
85
|
|
|
{ |
86
|
|
|
$flags = StabilityFlags::extract( |
87
|
|
|
$root->getStabilityFlags(), |
88
|
|
|
$root->getMinimumStability(), |
89
|
|
|
$requires |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
self::unwrapIfNeeded($root, 'setStabilityFlags') |
93
|
|
|
->setStabilityFlags($flags); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Merge two collections of package links and collect duplicates for subsequent processing. |
98
|
|
|
* |
99
|
|
|
* @param \Composer\Package\Link[] $origin Primary collection |
100
|
|
|
* @param array $merge Additional collection |
101
|
|
|
* @param bool $replace Replace existing links ? |
102
|
|
|
* @param array $duplicateLinks Duplicate storage |
103
|
|
|
* |
104
|
|
|
* @return \Composer\Package\Link[] Merged collection |
105
|
|
|
*/ |
106
|
|
|
private function mergeLinks(array $origin, array $merge, $replace, array &$duplicateLinks) |
107
|
|
|
{ |
108
|
|
|
foreach ($merge as $name => $link) { |
109
|
|
|
if ( ! isset($origin[$name]) || $replace) { |
110
|
|
|
$this->logger->info("Merging <comment>{$name}</comment>"); |
|
|
|
|
111
|
|
|
$origin[$name] = $link; |
112
|
|
|
} |
113
|
|
|
else { |
114
|
|
|
// Defer to solver. |
115
|
|
|
$this->logger->info("Deferring duplicate <comment>{$name}</comment>"); |
116
|
|
|
$duplicateLinks[] = $link; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $origin; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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: