/
OutdatedCommand.php
178 lines (142 loc) · 5.33 KB
/
OutdatedCommand.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
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
<?php declare(strict_types=1);
/*
* This file is part of Phive.
*
* Copyright (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de> and contributors
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace PharIo\Phive;
use const JSON_PRETTY_PRINT;
use function count;
use function file_put_contents;
use function json_encode;
use function sprintf;
use DOMDocument;
use PharIo\Phive\Cli\ConsoleTable;
class OutdatedCommand implements Cli\Command {
/** @var PhiveXmlConfig */
private $phiveXmlConfig;
/** @var Cli\Output */
private $output;
/** @var RequestedPharResolverService */
private $pharResolver;
/** @var ReleaseSelector */
private $selector;
/** @var OutdatedConfig */
private $outdatedConfig;
public function __construct(OutdatedConfig $outdatedConfig, RequestedPharResolverService $pharResolver, ReleaseSelector $selector, PhiveXmlConfig $phiveXmlConfig, Cli\Output $output) {
$this->pharResolver = $pharResolver;
$this->selector = $selector;
$this->phiveXmlConfig = $phiveXmlConfig;
$this->output = $output;
$this->outdatedConfig = $outdatedConfig;
}
public function execute(): void {
$output = $this->renderOutput(
$this->gatherOutdated()
);
if ($this->outdatedConfig->saveToFile()) {
$this->writeToFile($output);
return;
}
$this->output->writeText($output);
}
private function renderOutput(array $outdated): string {
if ($this->outdatedConfig->wantsJson()) {
return $this->renderJsonOutput($outdated);
}
if ($this->outdatedConfig->wantsXml()) {
return $this->renderXmlOutput($outdated);
}
return $this->renderCliOutput($outdated);
}
private function renderCliOutput(array $outdated): string {
if (count($outdated) === 0) {
return 'Congrats, no outdated phars found';
}
$table = new ConsoleTable(['Name', 'Version Constraint', 'Installed', 'Available']);
foreach ($outdated as $entry) {
$table->addRow([
$entry['name'],
$entry['constraint'],
$entry['installed'],
$entry['available']
]);
}
return sprintf(
"Found %d outdated PHARs in phive.xml:\n\n%s",
count($outdated),
$table->asString()
);
}
private function gatherOutdated(): array {
$outdated = [];
foreach ($this->phiveXmlConfig->getPhars() as $phar) {
if (!$phar->isInstalled()) {
continue;
}
if ($phar->hasUrl() || Url::isUrl($phar->getName())) {
$this->output->writeWarning(
sprintf(
'Phar "%s" installed via URL - cannot check for newer versions',
$phar->getName()
)
);
continue;
}
$latest = $this->resolveToRelease(new RequestedPhar(
new PharAlias($phar->getName()),
$phar->getVersionConstraint(),
$phar->getVersionConstraint(),
$phar->getLocation(),
$phar->isCopy()
));
if (!$latest->getVersion()->isGreaterThan($phar->getInstalledVersion())) {
continue;
}
$outdated[] = [
'name' => $phar->getName(),
'constraint' => $phar->getVersionConstraint()->asString(),
'installed' => $phar->getInstalledVersion()->getVersionString(),
'available' => $latest->getVersion()->getVersionString()
];
}
return $outdated;
}
private function resolveToRelease(RequestedPhar $requestedPhar): SupportedRelease {
$repository = $this->pharResolver->resolve($requestedPhar);
$releases = $repository->getReleasesByRequestedPhar($requestedPhar);
return $this->selector->select(
$requestedPhar->getIdentifier(),
$releases,
$requestedPhar->getVersionConstraint(),
true
);
}
private function renderJsonOutput(array $outdated): string {
return json_encode(['outdated' => $outdated], JSON_PRETTY_PRINT);
}
private function renderXmlOutput(array $outdated): string {
$dom = new DOMDocument();
$dom->loadXML('<?xml version="1.0" encoding="UTF-8" ?><outdated xmlns="https://phar.io/outdated" />');
$root = $dom->documentElement;
foreach ($outdated as $entry) {
$node = $dom->createElementNS('https://phar.io/outdated', 'phar');
foreach ($entry as $field => $value) {
$node->setAttribute($field, $value);
}
$root->appendChild($node);
}
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
return $dom->saveXML();
}
private function writeToFile(string $output): void {
$destination = $this->outdatedConfig->outputFilename();
$destination->getDirectory()->ensureExists();
file_put_contents($destination->asString(), $output . "\n");
}
}