Skip to content

Commit 24ba68c

Browse files
committed
Download the PHPStan Pro PHAR of the current platform
PHPStan Pro PHARs bundle a native binary now, so /latest gets the os (PHP_OS_FAMILY) and arch (php_uname('m')) of the system and answers the PHAR of its platform. phar-info.json remembers the URL, os and arch of the downloaded PHAR. A PHAR downloaded on another system (the directory was carried over, or is shared with a Docker container) or by an older PHPStan is deleted and downloaded again, and so is one whose URL /latest no longer answers. Amp-Thread-ID: https://ampcode.com/threads/T-01a1c135-4a5f-71d6-8fd1-dd47a96f2e3a
1 parent de42b4f commit 24ba68c

1 file changed

Lines changed: 42 additions & 20 deletions

File tree

‎src/Command/FixerApplication.php‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
use function microtime;
5050
use function min;
5151
use function parse_url;
52+
use function php_uname;
5253
use function sprintf;
5354
use function unlink;
5455
use const JSON_INVALID_UTF8_IGNORE;
5556
use const PHP_BINARY;
57+
use const PHP_OS_FAMILY;
5658
use const PHP_URL_PORT;
5759
use const PHP_VERSION_ID;
5860

@@ -338,37 +340,51 @@ private function downloadPhar(
338340
string $infoPath,
339341
): void
340342
{
341-
$currentVersion = null;
343+
$currentUrl = null;
342344
$branch = '2.2.x';
345+
// the PHAR bundles a native binary, download-api answers the one for this system
346+
$os = PHP_OS_FAMILY;
347+
$arch = php_uname('m');
343348
if (is_file($pharPath) && is_file($infoPath)) {
344-
/** @var array{version: string, date: string, branch?: string} $currentInfo */
349+
/** @var array{version: string, date: string, branch?: string, url?: string, os?: string, arch?: string} $currentInfo */
345350
$currentInfo = Json::decode(FileReader::read($infoPath), Json::FORCE_ARRAY);
346-
$currentVersion = $currentInfo['version'];
347-
$currentBranch = $currentInfo['branch'] ?? 'master';
348-
$currentDate = DateTime::createFromFormat(DateTime::ATOM, $currentInfo['date']);
349-
if ($currentDate === false) {
350-
throw new ShouldNotHappenException();
351-
}
352-
if (
353-
$currentBranch === $branch
354-
&& (new DateTimeImmutable('', new DateTimeZone('UTC'))) <= $currentDate->modify('+24 hours')
355-
) {
356-
return;
351+
if (($currentInfo['os'] ?? null) === $os && ($currentInfo['arch'] ?? null) === $arch) {
352+
$currentUrl = $currentInfo['url'] ?? null;
357353
}
358354

359-
$output->writeln('<fg=green>Checking if there\'s a new PHPStan Pro release...</>');
355+
if ($currentUrl === null) {
356+
// downloaded on another system (the directory was carried over, or is shared with a Docker container),
357+
// or by an older PHPStan; its native binary might not run here
358+
@unlink($pharPath);
359+
@unlink($infoPath);
360+
} else {
361+
$currentBranch = $currentInfo['branch'] ?? 'master';
362+
$currentDate = DateTime::createFromFormat(DateTime::ATOM, $currentInfo['date']);
363+
if ($currentDate === false) {
364+
throw new ShouldNotHappenException();
365+
}
366+
if (
367+
$currentBranch === $branch
368+
&& (new DateTimeImmutable('', new DateTimeZone('UTC'))) <= $currentDate->modify('+24 hours')
369+
) {
370+
return;
371+
}
372+
373+
$output->writeln('<fg=green>Checking if there\'s a new PHPStan Pro release...</>');
374+
}
360375
}
361376

362377
$client = $this->httpClientFactory->createClient([]);
363378

364-
$latestUrl = sprintf('https://fixer-download-api.phpstan.com/latest?%s', http_build_query(['phpVersion' => PHP_VERSION_ID, 'branch' => $branch]));
379+
$latestUrl = sprintf('https://fixer-download-api.phpstan.com/latest?%s', http_build_query(['phpVersion' => PHP_VERSION_ID, 'branch' => $branch, 'os' => $os, 'arch' => $arch]));
365380

366381
/**
367382
* @var array{url: string, version: string} $latestInfo
368383
*/
369384
$latestInfo = Json::decode($client->get($latestUrl)->getBody()->getContents(), Json::FORCE_ARRAY);
370-
if ($currentVersion !== null && $latestInfo['version'] === $currentVersion) {
371-
$this->writeInfoFile($infoPath, $latestInfo['version'], $branch);
385+
// the URL changes with the version, and with the PHAR download-api picks for this system
386+
if ($latestInfo['url'] === $currentUrl) {
387+
$this->writeInfoFile($infoPath, $latestInfo, $branch, $os, $arch);
372388
$output->writeln('<fg=green>You\'re running the latest PHPStan Pro!</>');
373389
return;
374390
}
@@ -405,7 +421,7 @@ private function downloadPhar(
405421
$output->writeln('');
406422
$output->writeln('');
407423

408-
$this->writeInfoFile($infoPath, $latestInfo['version'], $branch);
424+
$this->writeInfoFile($infoPath, $latestInfo, $branch, $os, $arch);
409425
}
410426

411427
private function printDownloadError(OutputInterface $output, Throwable $e): void
@@ -414,10 +430,16 @@ private function printDownloadError(OutputInterface $output, Throwable $e): void
414430
$output->writeln('');
415431
}
416432

417-
private function writeInfoFile(string $infoPath, string $version, string $branch): void
433+
/**
434+
* @param array{url: string, version: string} $latestInfo
435+
*/
436+
private function writeInfoFile(string $infoPath, array $latestInfo, string $branch, string $os, string $arch): void
418437
{
419438
FileWriter::write($infoPath, Json::encode([
420-
'version' => $version,
439+
'version' => $latestInfo['version'],
440+
'url' => $latestInfo['url'],
441+
'os' => $os,
442+
'arch' => $arch,
421443
'branch' => $branch,
422444
'date' => (new DateTimeImmutable('', new DateTimeZone('UTC')))->format(DateTime::ATOM),
423445
]));

0 commit comments

Comments
 (0)