forked from composer/composer
/
CheckPlatformReqsCommand.php
221 lines (191 loc) · 9 KB
/
CheckPlatformReqsCommand.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
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
<?php declare(strict_types=1);
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Composer\Command;
use Composer\Package\Link;
use Composer\Semver\Constraint\Constraint;
use Symfony\Component\Console\Input\InputInterface;
use Composer\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RootPackageRepository;
use Composer\Repository\InstalledRepository;
use Composer\Json\JsonFile;
class CheckPlatformReqsCommand extends BaseCommand
{
/**
* @return void
*/
protected function configure(): void
{
$this->setName('check-platform-reqs')
->setDescription('Check that platform requirements are satisfied')
->setDefinition(array(
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables checking of require-dev packages requirements.'),
new InputOption('lock', null, InputOption::VALUE_NONE, 'Checks requirements only from the lock file, not from installed packages.'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']),
))
->setHelp(
<<<EOT
Checks that your PHP and extensions versions match the platform requirements of the installed packages.
Unlike update/install, this command will ignore config.platform settings and check the real platform packages so you can be certain you have the required platform dependencies.
<info>php composer.phar check-platform-reqs</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composer = $this->requireComposer();
$requires = array();
$removePackages = array();
if ($input->getOption('lock')) {
$this->getIO()->writeError('<info>Checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements using the lock file</info>');
$installedRepo = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev'));
} else {
$installedRepo = $composer->getRepositoryManager()->getLocalRepository();
// fallback to lockfile if installed repo is empty
if (!$installedRepo->getPackages()) {
$this->getIO()->writeError('<warning>No vendor dir present, checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements from the lock file</warning>');
$installedRepo = $composer->getLocker()->getLockedRepository(!$input->getOption('no-dev'));
} else {
if ($input->getOption('no-dev')) {
$removePackages = $installedRepo->getDevPackageNames();
}
$this->getIO()->writeError('<info>Checking '.($input->getOption('no-dev') ? 'non-dev ' : '').'platform requirements for packages in the vendor dir</info>');
}
}
if (!$input->getOption('no-dev')) {
$requires += $composer->getPackage()->getDevRequires();
}
foreach ($requires as $require => $link) {
$requires[$require] = array($link);
}
$installedRepo = new InstalledRepository(array($installedRepo, new RootPackageRepository(clone $composer->getPackage())));
foreach ($installedRepo->getPackages() as $package) {
if (in_array($package->getName(), $removePackages, true)) {
continue;
}
foreach ($package->getRequires() as $require => $link) {
$requires[$require][] = $link;
}
}
ksort($requires);
$installedRepo->addRepository(new PlatformRepository(array(), array()));
$results = array();
$exitCode = 0;
/**
* @var Link[] $links
*/
foreach ($requires as $require => $links) {
if (PlatformRepository::isPlatformPackage($require)) {
$candidates = $installedRepo->findPackagesWithReplacersAndProviders($require);
if ($candidates) {
$reqResults = array();
foreach ($candidates as $candidate) {
$candidateConstraint = null;
if ($candidate->getName() === $require) {
$candidateConstraint = new Constraint('=', $candidate->getVersion());
$candidateConstraint->setPrettyString($candidate->getPrettyVersion());
} else {
foreach (array_merge($candidate->getProvides(), $candidate->getReplaces()) as $link) {
if ($link->getTarget() === $require) {
$candidateConstraint = $link->getConstraint();
break;
}
}
}
// safety check for phpstan, but it should not be possible to get a candidate out of findPackagesWithReplacersAndProviders without a constraint matching $require
if (!$candidateConstraint) {
continue;
}
foreach ($links as $link) {
if (!$link->getConstraint()->matches($candidateConstraint)) {
$reqResults[] = array(
$candidate->getName() === $require ? $candidate->getPrettyName() : $require,
$candidateConstraint->getPrettyString(),
$link,
'<error>failed</error>',
$candidate->getName() === $require ? '' : '<comment>provided by '.$candidate->getPrettyName().'</comment>',
);
// skip to next candidate
continue 2;
}
}
$results[] = array(
$candidate->getName() === $require ? $candidate->getPrettyName() : $require,
$candidateConstraint->getPrettyString(),
null,
'<info>success</info>',
$candidate->getName() === $require ? '' : '<comment>provided by '.$candidate->getPrettyName().'</comment>',
);
// candidate matched, skip to next requirement
continue 2;
}
// show the first error from every failed candidate
$results = array_merge($results, $reqResults);
$exitCode = max($exitCode, 1);
continue;
}
$results[] = array(
$require,
'n/a',
$links[0],
'<error>missing</error>',
'',
);
$exitCode = max($exitCode, 2);
}
}
$this->printTable($output, $results, $input->getOption('format'));
return $exitCode;
}
/**
* @param mixed[] $results
*
* @return void
*/
protected function printTable(OutputInterface $output, array $results, string $format): void
{
$rows = array();
foreach ($results as $result) {
/**
* @var Link|null $link
*/
list($platformPackage, $version, $link, $status, $provider) = $result;
if ('json' === $format) {
$rows[] = array(
"name" => $platformPackage,
"version" => $version,
"status" => strip_tags($status),
"failed_requirement" => $link instanceof Link ? [
'source' => $link->getSource(),
'type' => $link->getDescription(),
'target' => $link->getTarget(),
'constraint' => $link->getPrettyConstraint(),
] : null,
"provider" => $provider === '' ? null : strip_tags($provider),
);
} else {
$rows[] = array(
$platformPackage,
$version,
$link,
$link ? sprintf('%s %s %s (%s)', $link->getSource(), $link->getDescription(), $link->getTarget(), $link->getPrettyConstraint()) : '',
rtrim($status.' '.$provider),
);
}
}
if ('json' === $format) {
$this->getIO()->write(JsonFile::encode($rows));
} else {
$this->renderTable($rows, $output);
}
}
}