From 62cb1300d8674472698e3af4d4b726cc4c9c5baf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Tue, 21 Nov 2023 23:53:18 +0100 Subject: [PATCH 1/3] refactor: Move finding the PHP or Box bin into a dedicated class --- src/ExecutableFinder.php | 52 ++++++++++++++++++++++++++++++++++++++++ src/Phar/PharInfo.php | 31 +++--------------------- 2 files changed, 55 insertions(+), 28 deletions(-) create mode 100644 src/ExecutableFinder.php diff --git a/src/ExecutableFinder.php b/src/ExecutableFinder.php new file mode 100644 index 000000000..f617f8791 --- /dev/null +++ b/src/ExecutableFinder.php @@ -0,0 +1,52 @@ + + * Théo Fidry + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace KevinGH\Box; + +use RuntimeException; +use Symfony\Component\Process\PhpExecutableFinder as SymfonyPhpExecutableFinder; + +final class ExecutableFinder +{ + private static string $boxExecutable; + private static string $phpExecutable; + + public static function findBoxExecutable(): string + { + if (isset(self::$boxExecutable)) { + return self::$boxExecutable; + } + + self::$boxExecutable = getenv(BOX_BIN) ?: $_SERVER['SCRIPT_NAME']; + + return self::$boxExecutable; + } + + public static function findPhpExecutable(): string + { + if (isset(self::$phpExecutable)) { + return self::$phpExecutable; + } + + $phpExecutable = (new SymfonyPhpExecutableFinder())->find(); + + if (false === $phpExecutable) { + throw new RuntimeException('Could not find a PHP executable.'); + } + + self::$phpExecutable = $phpExecutable; + + return self::$phpExecutable; + } +} diff --git a/src/Phar/PharInfo.php b/src/Phar/PharInfo.php index ed10a7e0a..9a7be3929 100644 --- a/src/Phar/PharInfo.php +++ b/src/Phar/PharInfo.php @@ -45,18 +45,16 @@ use Fidry\FileSystem\FS; use KevinGH\Box\Console\Command\Extract; +use KevinGH\Box\ExecutableFinder; use OutOfBoundsException; use Phar; -use RuntimeException; use Symfony\Component\Filesystem\Path; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; use Symfony\Component\Process\Exception\ProcessFailedException; -use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; use function bin2hex; use function file_exists; -use function getenv; use function is_readable; use function iter\mapKeys; use function iter\toArrayWithKeys; @@ -287,8 +285,8 @@ private static function initStubFileName(): void private static function dumpPhar(string $file, string $tmp): void { $extractPharProcess = new Process([ - self::getPhpExecutable(), - self::getBoxBin(), + ExecutableFinder::findPhpExecutable(), + ExecutableFinder::findBoxExecutable(), 'extract', $file, $tmp, @@ -328,29 +326,6 @@ private static function loadDumpedPharFiles(string $tmp): array return [$meta, $dumpedFiles]; } - private static function getPhpExecutable(): string - { - if (isset(self::$phpExecutable)) { - return self::$phpExecutable; - } - - $phpExecutable = (new PhpExecutableFinder())->find(); - - if (false === $phpExecutable) { - throw new RuntimeException('Could not find a PHP executable.'); - } - - self::$phpExecutable = $phpExecutable; - - return self::$phpExecutable; - } - - private static function getBoxBin(): string - { - // TODO: move the constraint strings declaration in one place - return getenv('BOX_BIN') ?: $_SERVER['SCRIPT_NAME']; - } - /** * @param array $filesMeta */ From 0368c3bf38c2dc3f5dcc5c16c93ad42674a1c7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 22 Nov 2023 00:13:07 +0100 Subject: [PATCH 2/3] fix --- src/Phar/PharInfo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Phar/PharInfo.php b/src/Phar/PharInfo.php index 8ff1d78aa..9a7be3929 100644 --- a/src/Phar/PharInfo.php +++ b/src/Phar/PharInfo.php @@ -45,7 +45,7 @@ use Fidry\FileSystem\FS; use KevinGH\Box\Console\Command\Extract; -use KevinGH\Box\Constants; +use KevinGH\Box\ExecutableFinder; use OutOfBoundsException; use Phar; use Symfony\Component\Filesystem\Path; From 1ddbf55be18ba083e29bd5d16b8119877533b658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 22 Nov 2023 00:22:23 +0100 Subject: [PATCH 3/3] fix --- src/ExecutableFinder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ExecutableFinder.php b/src/ExecutableFinder.php index f617f8791..f692d0cde 100644 --- a/src/ExecutableFinder.php +++ b/src/ExecutableFinder.php @@ -28,7 +28,7 @@ public static function findBoxExecutable(): string return self::$boxExecutable; } - self::$boxExecutable = getenv(BOX_BIN) ?: $_SERVER['SCRIPT_NAME']; + self::$boxExecutable = getenv(Constants::BIN) ?: $_SERVER['SCRIPT_NAME']; return self::$boxExecutable; }