From 43de09a3e0f48a6c1380771e46f4ca50766fbc0f Mon Sep 17 00:00:00 2001 From: Hugo Alliaume Date: Tue, 24 Sep 2024 17:23:00 +0200 Subject: [PATCH] refactor(biomejs-binary): extra mucl logic into dedicated method, and make it safer --- src/BiomeJsBinary.php | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/src/BiomeJsBinary.php b/src/BiomeJsBinary.php index e6b80e8..2c7199a 100644 --- a/src/BiomeJsBinary.php +++ b/src/BiomeJsBinary.php @@ -154,15 +154,6 @@ 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', @@ -170,8 +161,8 @@ public static function getBinaryName(): string 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) { @@ -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() ?: ''); + } }