1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Vaimo Group. All rights reserved. |
4
|
|
|
* See LICENSE_VAIMO.txt for license details. |
5
|
|
|
*/ |
6
|
|
|
namespace Vaimo\ComposerPatches\Composer; |
7
|
|
|
|
8
|
|
|
class Context |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Composer\Composer[] |
12
|
|
|
*/ |
13
|
|
|
private $instances; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Composer\Package\PackageInterface[][] |
17
|
|
|
*/ |
18
|
|
|
private $packages; |
19
|
|
|
|
20
|
|
|
public function __construct($instances) |
21
|
|
|
{ |
22
|
|
|
$this->instances = $instances; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getActivePackages() |
26
|
|
|
{ |
27
|
|
|
return array_reduce($this->getPackageMap(), 'array_replace', array()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getLocalComposer() |
31
|
|
|
{ |
32
|
|
|
return end($this->instances); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function getPackageMap() |
36
|
|
|
{ |
37
|
|
|
if ($this->packages === null) { |
38
|
|
|
$result = array(); |
39
|
|
|
|
40
|
|
|
foreach ($this->instances as $instanceIndex => $instance) { |
41
|
|
|
$repository = $instance->getRepositoryManager()->getLocalRepository(); |
42
|
|
|
|
43
|
|
|
if (!isset($this->packages[$instanceIndex])) { |
44
|
|
|
$this->packages[$instanceIndex] = array(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
foreach ($repository->getCanonicalPackages() as $package) { |
48
|
|
|
$name = $package->getName(); |
49
|
|
|
|
50
|
|
|
if (isset($result[$name])) { |
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$result[$name] = true; |
55
|
|
|
$this->packages[$instanceIndex][$name] = $package; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->packages; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getInstanceForPackage(\Composer\Package\PackageInterface $package) |
64
|
|
|
{ |
65
|
|
|
$name = $package->getName(); |
66
|
|
|
$packageMap = $this->getPackageMap(); |
67
|
|
|
|
68
|
|
|
foreach ($packageMap as $index => $packages) { |
69
|
|
|
if (!isset($packages[$name])) { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $this->instances[$index]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new \Vaimo\ComposerPatches\Exceptions\RuntimeException( |
77
|
|
|
sprintf('Failed to resolve Composer instance for package: %s', $name) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|