From 937e0ee487f6164701a2b073630652494ac87a4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Mon, 11 Dec 2023 10:26:07 +0100 Subject: [PATCH] refactor: Move bootstrapping function into a dedicated class (#1263) --- bin/box | 4 +- phpunit.xml.dist | 3 +- src/Bootstrap.php | 50 +++++++++++++++++++++++++ src/Parallelization/ProcessFileTask.php | 7 ++-- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 src/Bootstrap.php diff --git a/bin/box b/bin/box index 98d6c6c43..3b4b9de02 100755 --- a/bin/box +++ b/bin/box @@ -50,8 +50,8 @@ if (false === in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed', 'micro'], true)) { throw new RuntimeException('Unable to find the Composer autoloader.'); })(); -register_aliases(); -register_error_handler(); +Bootstrap::registerAliases(); +Bootstrap::registerErrorHandler(); $io = IO::createDefault(); OutputFormatterConfigurator::configure($io); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 53757c21d..819bb1898 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -40,8 +40,7 @@ src/ - src/bootstrap.php - src/consts.php + src/Bootstrap.php src/functions.php src/Console/Command/Build.php src/Console/Command/ChangeableWorkingDirectory.php diff --git a/src/Bootstrap.php b/src/Bootstrap.php new file mode 100644 index 000000000..06716825e --- /dev/null +++ b/src/Bootstrap.php @@ -0,0 +1,50 @@ + + * 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 ErrorException; +use Isolated\Symfony\Component\Finder\Finder as IsolatedFinder; +use Symfony\Component\Finder\Finder as SymfonyFinder; +use function class_alias; +use function class_exists; +use function error_reporting; +use function set_error_handler; + +final class Bootstrap +{ + use NotInstantiable; + + /** + * @private + */ + public static function registerAliases(): void + { + // Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file. + if (false === class_exists(IsolatedFinder::class)) { + class_alias(SymfonyFinder::class, IsolatedFinder::class); + } + } + + public static function registerErrorHandler(): void + { + set_error_handler( + static function (int $code, string $message, string $file = '', int $line = -1): void { + if (error_reporting() & $code) { + throw new ErrorException($message, 0, $code, $file, $line); + } + }, + ); + } +} diff --git a/src/Parallelization/ProcessFileTask.php b/src/Parallelization/ProcessFileTask.php index 756d6bdf7..6678ab119 100644 --- a/src/Parallelization/ProcessFileTask.php +++ b/src/Parallelization/ProcessFileTask.php @@ -19,11 +19,10 @@ use Amp\Sync\Channel; use Fidry\FileSystem\FS; use Humbug\PhpScoper\Symbol\SymbolsRegistry; +use KevinGH\Box\Bootstrap; use KevinGH\Box\Compactor\Compactors; use KevinGH\Box\MapFile; use function array_map; -use function KevinGH\Box\register_aliases; -use function KevinGH\Box\register_error_handler; /** * @private @@ -45,8 +44,8 @@ public function run(Channel $channel, Cancellation $cancellation): TaskResult { chdir($this->cwd); - register_aliases(); - register_error_handler(); + Bootstrap::registerAliases(); + Bootstrap::registerErrorHandler(); $mapFile = $this->mapFile; $compactors = $this->compactors;