-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathComposerLoaderExtension.php
124 lines (110 loc) · 3.61 KB
/
ComposerLoaderExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace BringYourOwnIdeas\UpdateChecker\Extensions;
use Composer\Composer;
use Composer\Factory;
use Composer\IO\NullIO;
use Composer\Package\Link;
use Composer\Repository\InstalledRepository;
use Composer\Repository\RootPackageRepository;
use Composer\Repository\RepositoryInterface;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Extension;
class ComposerLoaderExtension extends Extension
{
/**
* @var Composer
*/
protected $composer;
/**
* @param Composer $composer
* @return $this
*/
public function setComposer(Composer $composer)
{
$this->composer = $composer;
return $this;
}
/**
* @return Composer
*/
public function getComposer()
{
return $this->composer;
}
/**
* Retrieve an array of primary composer dependencies from composer.json.
* Packages are filtered by allowed type.
* Dependencies in composer.json that do not match any of the given types are not returned.
*
* @param array|null $allowedTypes An array of "allowed" package types.
* @return array[]
*/
public function getPackages(array $allowedTypes = null)
{
$packages = [];
$repository = $this->getRepository();
foreach ($repository->getPackages() as $package) {
// Filter out packages that are not "allowed types"
if (is_array($allowedTypes) && !in_array($package->getType(), $allowedTypes)) {
continue;
}
// Find the constraint used for installation
$constraint = $this->getInstalledConstraint($repository, $package->getName());
$packages[$package->getName()] = [
'constraint' => $constraint,
'package' => $package,
];
}
return $packages;
}
/**
* Provides access to the Composer repository
*/
protected function getRepository(): RepositoryInterface
{
/** @var Composer $composer */
$composer = $this->getComposer();
return new InstalledRepository([
new RootPackageRepository($composer->getPackage()),
$composer->getRepositoryManager()->getLocalRepository()
]);
}
/**
* Find all dependency constraints for the given package in the current repository and return the strictest one
*/
protected function getInstalledConstraint(InstalledRepository $repository, string $packageName): string
{
$constraints = [];
foreach ($repository->getDependents($packageName) as $dependent) {
/** @var Link $link */
list (, $link) = $dependent;
$constraints[] = $link->getPrettyConstraint() ?? '';
}
usort($constraints, 'version_compare');
return array_pop($constraints);
}
/**
* Builds an instance of Composer
*/
public function onAfterBuild()
{
// Mock COMPOSER_HOME if it's not defined already. Composer requires one of the two to be set.
if (!Environment::getEnv('COMPOSER_HOME')) {
$home = Environment::getEnv('HOME');
if (!$home || !is_dir($home) || !is_writable($home)) {
// Set our own directory
putenv('COMPOSER_HOME=' . sys_get_temp_dir());
}
}
$originalDir = getcwd();
if ($originalDir !== BASE_PATH) {
chdir(BASE_PATH);
}
/** @var Composer $composer */
$composer = Factory::create(new NullIO());
$this->setComposer($composer);
if ($originalDir !== BASE_PATH) {
chdir($originalDir);
}
}
}