-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComposerVersionChecker.php
More file actions
119 lines (94 loc) · 3.18 KB
/
ComposerVersionChecker.php
File metadata and controls
119 lines (94 loc) · 3.18 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
<?php
declare(strict_types=1);
/**
* Fast Forward Development Tools for PHP projects.
*
* This file is part of fast-forward/dev-tools project.
*
* @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com>
* @license https://opensource.org/licenses/MIT MIT License
*
* @see https://github.com/php-fast-forward/
* @see https://github.com/php-fast-forward/dev-tools
* @see https://github.com/php-fast-forward/dev-tools/issues
* @see https://php-fast-forward.github.io/dev-tools/
* @see https://datatracker.ietf.org/doc/html/rfc2119
*/
namespace FastForward\DevTools\SelfUpdate;
use Composer\InstalledVersions;
use FastForward\DevTools\Path\DevToolsPathResolver;
use FastForward\DevTools\Process\ProcessBuilderInterface;
use JsonException;
use function Safe\preg_match;
use function Safe\json_decode;
/**
* Resolves DevTools freshness through Composer metadata without coupling callers to Composer commands.
*/
final readonly class ComposerVersionChecker implements VersionCheckerInterface
{
private const string PACKAGE = 'fast-forward/dev-tools';
private const int TIMEOUT_SECONDS = 5;
/**
* @param ProcessBuilderInterface $processBuilder the process builder used to query Composer metadata
*/
public function __construct(
private ProcessBuilderInterface $processBuilder,
) {}
/**
* Returns version information when it can be resolved without blocking command execution.
*/
public function check(): ?VersionCheckResult
{
if (DevToolsPathResolver::isRepositoryCheckout()) {
return null;
}
$currentVersion = InstalledVersions::getPrettyVersion(self::PACKAGE)
?? InstalledVersions::getVersion(self::PACKAGE);
if (null === $currentVersion) {
return null;
}
$latestVersion = $this->resolveLatestStableVersion();
if (null === $latestVersion) {
return null;
}
return new VersionCheckResult($currentVersion, $latestVersion);
}
/**
* Resolves the latest stable DevTools version available to Composer.
*/
private function resolveLatestStableVersion(): ?string
{
$process = $this->processBuilder
->withArgument(self::PACKAGE)
->withArgument('--available')
->withArgument('--format=json')
->withArgument('--no-interaction')
->build('composer show');
$process->setTimeout(self::TIMEOUT_SECONDS);
$process->run();
if (! $process->isSuccessful()) {
return null;
}
try {
$payload = json_decode($process->getOutput(), true);
} catch (JsonException) {
return null;
}
if (! \is_array($payload)) {
return null;
}
$versions = $payload['versions'] ?? null;
if (! \is_array($versions)) {
return null;
}
foreach ($versions as $version) {
if (! \is_string($version)) {
continue;
}
if (1 === preg_match('/^v?\d+\.\d+\.\d+$/', $version)) {
return $version;
}
}
return null;
}
}