-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPackageDiff.php
More file actions
309 lines (254 loc) · 8.77 KB
/
PackageDiff.php
File metadata and controls
309 lines (254 loc) · 8.77 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
namespace IonBazan\ComposerDiff;
use Composer\DependencyResolver\Operation\InstallOperation;
use Composer\DependencyResolver\Operation\UninstallOperation;
use Composer\DependencyResolver\Operation\UpdateOperation;
use Composer\Package\AliasPackage;
use Composer\Package\CompletePackage;
use Composer\Package\Loader\ArrayLoader;
use Composer\Repository\ArrayRepository;
use Composer\Repository\RepositoryInterface;
use IonBazan\ComposerDiff\Diff\DiffEntries;
use IonBazan\ComposerDiff\Diff\DiffEntry;
use IonBazan\ComposerDiff\Url\GeneratorContainer;
use IonBazan\ComposerDiff\Url\UrlGenerator;
class PackageDiff
{
const COMPOSER = 'composer';
const EXTENSION_LOCK = '.lock';
const EXTENSION_JSON = '.json';
const GIT_SEPARATOR = ':';
/** @var UrlGenerator */
protected $urlGenerator;
/** @var bool */
protected $skipIoErrors = false;
public function __construct()
{
$this->urlGenerator = new GeneratorContainer();
}
/**
* @return void
*/
public function setUrlGenerator(UrlGenerator $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
/**
* @param bool $skipIoErrors
*
* @return void
*/
public function setSkipIoErrors($skipIoErrors)
{
$this->skipIoErrors = (bool) $skipIoErrors;
}
/**
* @param string[] $directPackages
* @param bool $onlyDirect
*
* @return DiffEntries
*/
public function getDiff(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages, array $directPackages = array(), $onlyDirect = false)
{
$entries = array();
foreach ($this->getOperations($oldPackages, $targetPackages) as $operation) {
$package = $operation instanceof UpdateOperation ? $operation->getTargetPackage() : $operation->getPackage();
$direct = in_array($package->getName(), $directPackages, true);
if ($onlyDirect && !$direct) {
continue;
}
$entries[] = new DiffEntry($operation, $this->urlGenerator, $direct);
}
return new DiffEntries($entries);
}
/**
* @return array<InstallOperation|UpdateOperation|UninstallOperation>
*/
public function getOperations(RepositoryInterface $oldPackages, RepositoryInterface $targetPackages)
{
$operations = array();
foreach ($targetPackages->getPackages() as $newPackage) {
$matchingPackages = $oldPackages->findPackages($newPackage->getName());
if ($newPackage instanceof AliasPackage) {
continue;
}
if (0 === count($matchingPackages)) {
$operations[] = new InstallOperation($newPackage);
continue;
}
foreach ($matchingPackages as $oldPackage) {
if ($oldPackage instanceof AliasPackage) {
continue;
}
if ($oldPackage->getFullPrettyVersion() !== $newPackage->getFullPrettyVersion()) {
$operations[] = new UpdateOperation($oldPackage, $newPackage);
}
}
}
foreach ($oldPackages->getPackages() as $oldPackage) {
if ($oldPackage instanceof AliasPackage) {
continue;
}
if (!$targetPackages->findPackage($oldPackage->getName(), '*')) {
$operations[] = new UninstallOperation($oldPackage);
}
}
return $operations;
}
/**
* @param string $from
* @param string $to
* @param bool $dev
* @param bool $withPlatform
* @param bool $onlyDirect
*
* @return DiffEntries
*/
public function getPackageDiff($from, $to, $dev, $withPlatform, $onlyDirect = false)
{
return $this->getDiff(
$this->loadPackages($from, $dev, $withPlatform),
$this->loadPackages($to, $dev, $withPlatform),
array_merge($this->getDirectPackages($from), $this->getDirectPackages($to)),
$onlyDirect
);
}
/**
* @param mixed[] $composerLock
* @param bool $dev
* @param bool $withPlatform
*
* @return ArrayRepository
*/
public function loadPackagesFromArray(array $composerLock, $dev, $withPlatform)
{
$loader = new ArrayLoader();
$packages = array();
$packagesKey = 'packages'.($dev ? '-dev' : '');
$platformKey = 'platform'.($dev ? '-dev' : '');
if (isset($composerLock[$packagesKey])) {
foreach ($composerLock[$packagesKey] as $packageInfo) {
$packages[] = $loader->load($packageInfo);
}
}
if ($withPlatform && isset($composerLock[$platformKey])) {
foreach ($composerLock[$platformKey] as $name => $version) {
$packages[] = new CompletePackage($name, $version, $version);
}
}
return new ArrayRepository($packages);
}
/**
* @param string $path
* @param bool $dev
* @param bool $withPlatform
*
* @return ArrayRepository
*/
private function loadPackages($path, $dev, $withPlatform)
{
$data = \json_decode($this->getFileContents($path), true);
return $this->loadPackagesFromArray($data, $dev, $withPlatform);
}
/**
* @param string $path
*
* @return string[]
*/
private function getDirectPackages($path)
{
$data = \json_decode($this->getFileContents($path, false), true);
$packages = array();
foreach (array('require', 'require-dev') as $key) {
if (isset($data[$key])) {
$packages = array_merge($packages, array_keys($data[$key]));
}
}
return $packages; // @phpstan-ignore return.type
}
/**
* @param string $path
* @param bool $lockFile
*
* @return string
*/
private function getFileContents($path, $lockFile = true)
{
$originalPath = $path;
if (empty($path)) {
$path = self::COMPOSER.($lockFile ? self::EXTENSION_LOCK : self::EXTENSION_JSON);
}
$localPath = $path;
if (!$lockFile) {
$localPath = $this->getJsonPath($localPath);
}
if (filter_var($localPath, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED) || file_exists($localPath)) {
// @phpstan-ignore return.type
return file_get_contents($localPath);
}
$attemptedSpecs = array();
$lastError = '';
foreach ($this->getGitCandidates($path, $lockFile) as $gitPath) {
$attemptedSpecs[] = $gitPath;
$output = array();
@exec(sprintf('git show %s 2>&1', escapeshellarg($gitPath)), $output, $exit);
$outputString = implode("\n", $output);
if (0 !== $exit) {
$lastError = $outputString;
continue;
}
if (!$this->looksLikeJsonDocument($outputString)) {
$lastError = sprintf('Unexpected non-JSON output for git spec %s', $gitPath);
continue;
}
return $outputString;
}
if ($lockFile && !$this->skipIoErrors) {
throw new \RuntimeException(sprintf('Could not open file %s or find it in git as %s: %s', $originalPath, implode(', ', $attemptedSpecs), $lastError));
}
/* @infection-ignore-all False-positive */
// Do not throw exception for composer.json as it might not exist and that's fine.
// For composer.lock this fallback is optional and controlled by setSkipIoErrors().
return '{}';
}
/**
* @param string $path
* @param bool $lockFile
*
* @return string[]
*/
private function getGitCandidates($path, $lockFile = true)
{
$candidates = array();
if ($lockFile) {
$candidates[] = $path;
$candidates[] = $path.self::GIT_SEPARATOR.self::COMPOSER.self::EXTENSION_LOCK;
} else {
$candidates[] = $this->getJsonPath($path);
$candidates[] = $this->getJsonPath($path.self::GIT_SEPARATOR.self::COMPOSER.self::EXTENSION_LOCK);
}
return array_values(array_unique($candidates));
}
/**
* @param string $contents
*
* @return bool
*/
private function looksLikeJsonDocument($contents)
{
$data = json_decode($contents, true);
return JSON_ERROR_NONE === json_last_error() && is_array($data);
}
/**
* @param string $path
*
* @return string
*/
private function getJsonPath($path)
{
if (self::EXTENSION_LOCK === substr($path, -strlen(self::EXTENSION_LOCK))) {
return substr($path, 0, -strlen(self::EXTENSION_LOCK)).self::EXTENSION_JSON;
}
return $path;
}
}