|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BringYourOwnIdeas\UpdateChecker\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use Composer\Composer; |
|
6
|
|
|
use Composer\Factory; |
|
7
|
|
|
use Composer\IO\NullIO; |
|
8
|
|
|
use Composer\Package\Link; |
|
9
|
|
|
use Composer\Repository\ArrayRepository; |
|
10
|
|
|
use Composer\Repository\BaseRepository; |
|
11
|
|
|
use Composer\Repository\CompositeRepository; |
|
12
|
|
|
use Extension; |
|
13
|
|
|
|
|
14
|
|
|
class ComposerLoaderExtension extends Extension |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Composer |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $composer; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param Composer $composer |
|
23
|
|
|
* @return $this |
|
24
|
|
|
*/ |
|
25
|
|
|
public function setComposer(Composer $composer) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->composer = $composer; |
|
28
|
|
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return Composer |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getComposer() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->composer; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retrieve an array of primary composer dependencies from composer.json. |
|
41
|
|
|
* |
|
42
|
|
|
* Packages are filtered by allowed type. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array|null $allowedTypes An array of "allowed" package types. Dependencies in composer.json that do not |
|
45
|
|
|
* match any of the given types are not returned. |
|
46
|
|
|
* @return array[] |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getPackages(array $allowedTypes = null) |
|
49
|
|
|
{ |
|
50
|
|
|
/** @var Composer $composer */ |
|
51
|
|
|
$composer = $this->getComposer(); |
|
52
|
|
|
|
|
53
|
|
|
/** @var BaseRepository $repository */ |
|
54
|
|
|
$repository = new CompositeRepository([ |
|
55
|
|
|
new ArrayRepository([$composer->getPackage()]), |
|
56
|
|
|
$composer->getRepositoryManager()->getLocalRepository(), |
|
57
|
|
|
]); |
|
58
|
|
|
|
|
59
|
|
|
$packages = []; |
|
60
|
|
|
foreach ($repository->getPackages() as $package) { |
|
61
|
|
|
// Filter out packages that are not "allowed types" |
|
62
|
|
|
if (is_array($allowedTypes) && !in_array($package->getType(), $allowedTypes)) { |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Find the constraint used for installation |
|
67
|
|
|
$constraint = $this->getInstalledConstraint($repository, $package->getName()); |
|
68
|
|
|
$packages[$package->getName()] = [ |
|
69
|
|
|
'constraint' => $constraint, |
|
70
|
|
|
'package' => $package, |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
return $packages; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Find all dependency constraints for the given package in the current repository and return the strictest one |
|
78
|
|
|
* |
|
79
|
|
|
* @param BaseRepository $repository |
|
80
|
|
|
* @param string $packageName |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function getInstalledConstraint(BaseRepository $repository, $packageName) |
|
84
|
|
|
{ |
|
85
|
|
|
$constraints = []; |
|
86
|
|
|
foreach ($repository->getDependents($packageName) as $dependent) { |
|
87
|
|
|
/** @var Link $link */ |
|
88
|
|
|
list (, $link) = $dependent; |
|
89
|
|
|
$constraints[] = $link->getPrettyConstraint(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
usort($constraints, 'version_compare'); |
|
93
|
|
|
|
|
94
|
|
|
return array_pop($constraints); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Builds an instance of Composer |
|
99
|
|
|
*/ |
|
100
|
|
|
public function onAfterBuild() |
|
101
|
|
|
{ |
|
102
|
|
|
$originalDir = getcwd(); |
|
103
|
|
|
chdir(BASE_PATH); |
|
104
|
|
|
/** @var Composer $composer */ |
|
105
|
|
|
$composer = Factory::create(new NullIO()); |
|
106
|
|
|
$this->setComposer($composer); |
|
107
|
|
|
chdir($originalDir); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|