Skip to content

Commit

Permalink
refactor(biomejs-binary): extra mucl logic into dedicated method, and…
Browse files Browse the repository at this point in the history
… make it safer
  • Loading branch information
Kocal committed Sep 25, 2024
1 parent 6c5cbd0 commit 43de09a
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/BiomeJsBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,15 @@ public static function getBinaryName(): string
$os = strtolower(\PHP_OS);
$machine = strtolower(php_uname('m'));

$isMusl = false;
ob_start();
phpinfo(INFO_GENERAL);
$buildinfo = ob_get_contents();
ob_end_clean();
if (preg_match('/--build=.*?-linux-musl/', $buildinfo)) {
$isMusl = true;
}

return match (true) {
str_contains($os, 'darwin') => match ($machine) {
'arm64' => 'biome-darwin-arm64',
'x86_64' => 'biome-darwin-x64',
default => throw new \Exception(sprintf('No matching machine found for Darwin platform (Machine: %s).', $machine)),
},
str_contains($os, 'linux') => match ($machine) {
'arm64', 'aarch64' => !$isMusl ? 'biome-linux-arm64' : 'biome-linux-arm64-musl',
'x86_64' => !$isMusl ? 'biome-linux-x64' : 'biome-linux-x64-musl',
'arm64', 'aarch64' => self::isMusl() ? 'biome-linux-arm64-musl' : 'biome-linux-arm64',
'x86_64' => self::isMusl() ? 'biome-linux-x64-musl' : 'biome-linux-x64',
default => throw new \Exception(sprintf('No matching machine found for Linux platform (Machine: %s).', $machine)),
},
str_contains($os, 'win') => match ($machine) {
Expand All @@ -182,4 +173,26 @@ public static function getBinaryName(): string
default => throw new \Exception(sprintf('Unknown platform or architecture (OS: %s, Machine: %s).', $os, $machine)),
};
}

/**
* Whether the current PHP environment is using musl libc.
* This is used to determine the correct Biome.js binary to download.
*/
private static function isMusl(): bool
{
static $isMusl = null;

if (null !== $isMusl) {
return $isMusl;
}

if (!\function_exists('phpinfo')) {
return $isMusl = false;
}

ob_start();
phpinfo(\INFO_GENERAL);

return $isMusl = 1 === preg_match('/--build=.*?-linux-musl/', ob_get_clean() ?: '');
}
}

0 comments on commit 43de09a

Please # to comment.