1 | <?php namespace Arcanedev\Composer\Entities\PackageTraits; |
||
13 | trait RequiresTrait |
||
14 | { |
||
15 | /* ------------------------------------------------------------------------------------------------ |
||
16 | | Traits |
||
17 | | ------------------------------------------------------------------------------------------------ |
||
18 | */ |
||
19 | use PackageTrait; |
||
20 | |||
21 | /* ------------------------------------------------------------------------------------------------ |
||
22 | | Main Functions |
||
23 | | ------------------------------------------------------------------------------------------------ |
||
24 | */ |
||
25 | /** |
||
26 | * Merge require into a RootPackage. |
||
27 | * |
||
28 | * @param \Composer\Package\RootPackageInterface $root |
||
29 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
30 | */ |
||
31 | 99 | protected function mergeRequires(RootPackageInterface $root, PluginState $state) |
|
32 | { |
||
33 | 99 | if ( ! empty($requires = $this->getPackage()->getRequires())) { |
|
34 | 54 | $this->mergeStabilityFlags($root, $requires); |
|
35 | |||
36 | 54 | $duplicateLinks = []; |
|
37 | 54 | $requires = $this->replaceSelfVersionDependencies( |
|
38 | 54 | 'require', $requires, $root |
|
39 | ); |
||
40 | |||
41 | 54 | $root->setRequires($this->mergeLinks( |
|
42 | 54 | $root->getRequires(), $requires, $state, $duplicateLinks |
|
43 | )); |
||
44 | |||
45 | 54 | $state->addDuplicateLinks('require', $duplicateLinks); |
|
46 | } |
||
47 | 99 | } |
|
48 | |||
49 | /** |
||
50 | * Merge require-dev into RootPackage. |
||
51 | * |
||
52 | * @param \Composer\Package\RootPackageInterface $root |
||
53 | * @param \Arcanedev\Composer\Entities\PluginState $state |
||
54 | */ |
||
55 | 99 | protected function mergeDevRequires(RootPackageInterface $root, PluginState $state) |
|
56 | { |
||
57 | 99 | if ( ! empty($requires = $this->getPackage()->getDevRequires())) { |
|
58 | 18 | $this->mergeStabilityFlags($root, $requires); |
|
59 | |||
60 | 18 | $duplicateLinks = []; |
|
61 | 18 | $requires = $this->replaceSelfVersionDependencies( |
|
62 | 18 | 'require-dev', $requires, $root |
|
63 | ); |
||
64 | |||
65 | 18 | $root->setDevRequires($this->mergeLinks( |
|
66 | 18 | $root->getDevRequires(), $requires, $state, $duplicateLinks |
|
67 | )); |
||
68 | |||
69 | 18 | $state->addDuplicateLinks('require-dev', $duplicateLinks); |
|
70 | } |
||
71 | 99 | } |
|
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 | 54 | protected function mergeStabilityFlags(RootPackageInterface $root, array $requires) |
|
85 | { |
||
86 | 54 | $flags = StabilityFlags::extract( |
|
87 | 54 | $root->getStabilityFlags(), |
|
88 | 54 | $root->getMinimumStability(), |
|
89 | 36 | $requires |
|
90 | ); |
||
91 | |||
92 | 54 | self::unwrapIfNeeded($root, 'setStabilityFlags') |
|
93 | 54 | ->setStabilityFlags($flags); |
|
94 | 54 | } |
|
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 \Arcanedev\Composer\Entities\PluginState $state Plugin state |
||
102 | * @param array $duplicateLinks Duplicate storage |
||
103 | * |
||
104 | * @return \Composer\Package\Link[] Merged collection |
||
105 | */ |
||
106 | 54 | private function mergeLinks(array $origin, array $merge, PluginState $state, array &$duplicateLinks) |
|
107 | { |
||
108 | 54 | if ($state->ignoreDuplicateLinks() && $state->replaceDuplicateLinks()) { |
|
109 | 3 | $this->getLogger()->warning('Both replace and ignore-duplicates are true. These are mutually exclusive.'); |
|
110 | 3 | $this->getLogger()->warning('Duplicate packages will be ignored.'); |
|
111 | } |
||
112 | |||
113 | 54 | foreach ($merge as $name => $link) { |
|
114 | 54 | if (isset($origin[$name]) && $state->ignoreDuplicateLinks()) { |
|
115 | 6 | $this->getLogger()->info("Ignoring duplicate <comment>{$name}</comment>"); |
|
116 | } |
||
117 | 48 | elseif ( ! isset($origin[$name]) || $state->replaceDuplicateLinks()) { |
|
118 | 48 | $this->getLogger()->info("Merging <comment>{$name}</comment>"); |
|
119 | 48 | $origin[$name] = $link; |
|
120 | } |
||
121 | else { |
||
122 | // Defer to solver. |
||
123 | 9 | $this->getLogger()->info("Deferring duplicate <comment>{$name}</comment>"); |
|
124 | 24 | $duplicateLinks[] = $link; |
|
125 | } |
||
126 | } |
||
127 | |||
128 | 54 | return $origin; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * Update Links with a 'self.version' constraint with the root package's version. |
||
133 | * |
||
134 | * @param string $type |
||
135 | * @param array $links |
||
136 | * @param \Composer\Package\RootPackageInterface $root |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | abstract protected function replaceSelfVersionDependencies( |
||
143 | } |
||
144 |