-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathComposerFile.php
More file actions
169 lines (135 loc) · 4.34 KB
/
Copy pathComposerFile.php
File metadata and controls
169 lines (135 loc) · 4.34 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace ecoAPM\LibYear;
class ComposerFile
{
const DEFAULT_URL = 'https://repo.packagist.org';
private FileSystem $file_system;
/** @var array[] */
private array $cache = [];
/** @var resource */
private $stderr;
public function __construct(FileSystem $file_system, $stderr)
{
$this->file_system = $file_system;
$this->stderr = $stderr;
}
/**
* @param string $directory
* @return string[]
*/
public function getRepositories(string $directory): array
{
$json = $this->getComposerJSON($directory);
$repositories = isset($json['repositories'])
? array_filter($json['repositories'], fn ($repository) => is_array($repository) && key_exists('url', $repository))
: [];
$urls = array_map(fn (array $repository) => rtrim($repository['url'], '/'), $repositories);
if (!in_array(self::DEFAULT_URL, $urls)
&& (!isset($json['repositories']['packagist.org'])
|| $json['repositories']['packagist.org'] !== false)) {
$urls[] = self::DEFAULT_URL;
}
return $urls;
}
/**
* @param string $directory
* @return Dependency[]
*/
public function getDependencies(string $directory): array
{
$packages = $this->getPackageNames($directory);
$installed_versions = $this->getInstalledVersions($directory);
$dependencies = [];
foreach ($packages as $package_name => $declared_version) {
$dependencies[$package_name] = self::createDependency($package_name, $declared_version, $installed_versions);
}
return $dependencies;
}
public function getMinimumStability(string $directory): string
{
$json = $this->getComposerJSON($directory);
return $json['minimum-stability'] ?? 'stable';
}
private function getPackageNames(string $directory): array
{
$json = $this->getComposerJSON($directory);
return array_merge(
array_key_exists('require', $json) ? $json['require'] : [],
array_key_exists('require-dev', $json) ? $json['require-dev'] : []
);
}
private function getInstalledVersions(string $directory): array
{
$json = $this->getComposerLock($directory);
$installed_versions = [];
$packages = array_merge(
array_key_exists('packages', $json) ? $json['packages'] : [],
array_key_exists('packages-dev', $json) ? $json['packages-dev'] : []
);
foreach ($packages as $package_info) {
$installed_versions[$package_info['name']] = $package_info['version'];
}
return $installed_versions;
}
private static function createDependency(
string $package_name,
string $declared_version,
array $installed_versions
): Dependency {
$version_number = array_key_exists($package_name, $installed_versions)
? $installed_versions[$package_name]
: $declared_version;
return new Dependency($package_name, $version_number);
}
private function getComposerJSON(string $directory): array
{
$path = self::jsonPath($directory);
return $this->getComposerFile($path);
}
private function getComposerLock(string $directory): array
{
$path = self::lockPath($directory);
return $this->getComposerFile($path);
}
private function getComposerFile(string $path): array
{
if (array_key_exists($path, $this->cache)) {
return $this->cache[$path];
}
if (!$this->file_system->exists($path)) {
fwrite($this->stderr, "File not found at $path\n");
return [];
}
$this->cache[$path] = $this->file_system->getJSON($path);
return $this->cache[$path];
}
private static function jsonPath(string $directory): string
{
return $directory . DIRECTORY_SEPARATOR . 'composer.json';
}
private static function lockPath(string $directory): string
{
return $directory . DIRECTORY_SEPARATOR . 'composer.lock';
}
/**
* @param string $directory
* @param Dependency[] $updates
*/
public function update(string $directory, array $dependencies): void
{
$json = $this->getComposerJSON($directory);
foreach ($dependencies as $dependency) {
if (!isset($dependency->newest_version->version_number)) {
continue;
}
if (array_key_exists('require', $json) && array_key_exists($dependency->name, $json['require'])) {
$json['require'][$dependency->name] = $dependency->newest_version->version_number;
}
if (array_key_exists('require-dev', $json) && array_key_exists($dependency->name, $json['require-dev'])) {
$json['require-dev'][$dependency->name] = $dependency->newest_version->version_number;
}
}
$path = self::jsonPath($directory);
$this->file_system->saveJSON($path, $json);
}
}