From 127bc07e4f447cac5fda84b7a1974130beb4e736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 22 Nov 2023 00:48:06 +0100 Subject: [PATCH 1/2] refactor: Move controlling the parallelization settings into a dedicated class --- src/Box.php | 5 ++- src/Console/Command/Compile.php | 4 +- .../ParallelizationSettings.php | 42 +++++++++++++++++++ src/functions.php | 21 ---------- 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 src/Parallelization/ParallelizationSettings.php diff --git a/src/Box.php b/src/Box.php index 04581a4d0..bc1d7fb53 100644 --- a/src/Box.php +++ b/src/Box.php @@ -23,6 +23,7 @@ use KevinGH\Box\Compactor\Compactors; use KevinGH\Box\Compactor\PhpScoper; use KevinGH\Box\Compactor\Placeholder; +use KevinGH\Box\Parallelization\ParallelizationSettings; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; use KevinGH\Box\PhpScoper\NullScoper; @@ -457,7 +458,7 @@ private function processContents(array $files): array // Keep the fully qualified call here since this function may be executed without the right autoloading // mechanism \KevinGH\Box\register_aliases(); - if (true === \KevinGH\Box\is_parallel_processing_enabled()) { + if (true === ParallelizationSettings::isParallelProcessingEnabled()) { \KevinGH\Box\register_error_handler(); } @@ -470,7 +471,7 @@ private function processContents(array $files): array return [$local, $processedContents, $compactors->getScoperSymbolsRegistry()]; }; - if ($this->scoper instanceof NullScoper || false === is_parallel_processing_enabled()) { + if ($this->scoper instanceof NullScoper || false === ParallelizationSettings::isParallelProcessingEnabled()) { return array_map($processFile, $files); } diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index cbb7207e6..72c1a2349 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -39,6 +39,7 @@ use KevinGH\Box\Console\MessageRenderer; use KevinGH\Box\Constants; use KevinGH\Box\MapFile; +use KevinGH\Box\Parallelization\ParallelizationSettings; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; use KevinGH\Box\RequirementChecker\DecodedComposerJson; @@ -64,7 +65,6 @@ use function is_string; use function KevinGH\Box\bump_open_file_descriptor_limit; use function KevinGH\Box\check_php_settings; -use function KevinGH\Box\disable_parallel_processing; use function KevinGH\Box\format_size; use function KevinGH\Box\format_time; use function memory_get_peak_usage; @@ -193,7 +193,7 @@ public function execute(IO $io): int check_php_settings($io); if ($io->getTypedOption(self::NO_PARALLEL_PROCESSING_OPTION)->asBoolean()) { - disable_parallel_processing(); + ParallelizationSettings::disableParallelProcessing(); $io->writeln( '[debug] Disabled parallel processing', OutputInterface::VERBOSITY_DEBUG, diff --git a/src/Parallelization/ParallelizationSettings.php b/src/Parallelization/ParallelizationSettings.php new file mode 100644 index 000000000..d60b9dc1a --- /dev/null +++ b/src/Parallelization/ParallelizationSettings.php @@ -0,0 +1,42 @@ + + * 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\Parallelization; + +use KevinGH\Box\Constants; +use KevinGH\Box\NotInstantiable; +use function constant; +use function define; +use function defined; + +/** + * @private + */ +final class ParallelizationSettings +{ + use NotInstantiable; + + public static function disableParallelProcessing(): void + { + if (false === defined(Constants::NO_PARALLEL_PROCESSING)) { + define(Constants::NO_PARALLEL_PROCESSING, true); + } + } + + public static function isParallelProcessingEnabled(): bool + { + return false === defined(Constants::NO_PARALLEL_PROCESSING) + || false === constant(Constants::NO_PARALLEL_PROCESSING); + } +} diff --git a/src/functions.php b/src/functions.php index 6b8f05019..411bf2578 100644 --- a/src/functions.php +++ b/src/functions.php @@ -27,9 +27,6 @@ use function bin2hex; use function class_alias; use function class_exists; -use function constant; -use function define; -use function defined; use function floor; use function function_exists; use function is_float; @@ -178,24 +175,6 @@ class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component } } -/** - * @private - */ -function disable_parallel_processing(): void -{ - if (false === defined(Constants::NO_PARALLEL_PROCESSING)) { - define(Constants::NO_PARALLEL_PROCESSING, true); - } -} - -/** - * @private - */ -function is_parallel_processing_enabled(): bool -{ - return false === defined(Constants::NO_PARALLEL_PROCESSING) || false === constant(Constants::NO_PARALLEL_PROCESSING); -} - /** * @private * From 1a240e2b3a45dc87f63cbf739e38be63a9ddf86b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Thu, 23 Nov 2023 00:04:23 +0100 Subject: [PATCH 2/2] fix --- src/Box.php | 18 +++++--- src/Console/Command/Compile.php | 11 ++--- src/Constants.php | 3 -- .../ParallelizationSettings.php | 42 ------------------- 4 files changed, 18 insertions(+), 56 deletions(-) delete mode 100644 src/Parallelization/ParallelizationSettings.php diff --git a/src/Box.php b/src/Box.php index bc1d7fb53..39d3fb747 100644 --- a/src/Box.php +++ b/src/Box.php @@ -23,7 +23,6 @@ use KevinGH\Box\Compactor\Compactors; use KevinGH\Box\Compactor\PhpScoper; use KevinGH\Box\Compactor\Placeholder; -use KevinGH\Box\Parallelization\ParallelizationSettings; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; use KevinGH\Box\PhpScoper\NullScoper; @@ -71,6 +70,7 @@ final class Box implements Countable private function __construct( private Phar $phar, private readonly string $pharFilePath, + private readonly bool $enableParallelization, ) { $this->compactors = new Compactors(); $this->placeholderCompactor = new Placeholder([]); @@ -87,8 +87,12 @@ private function __construct( * * @see RecursiveDirectoryIterator */ - public static function create(string $pharFilePath, int $pharFlags = 0, ?string $pharAlias = null): self - { + public static function create( + string $pharFilePath, + int $pharFlags = 0, + ?string $pharAlias = null, + bool $enableParallelization = false, + ): self { // Ensure the parent directory of the PHAR file exists as `new \Phar()` does not create it and would fail // otherwise. FS::mkdir(dirname($pharFilePath)); @@ -96,6 +100,7 @@ public static function create(string $pharFilePath, int $pharFlags = 0, ?string return new self( new Phar($pharFilePath, $pharFlags, $pharAlias), $pharFilePath, + $enableParallelization, ); } @@ -451,14 +456,15 @@ private function processContents(array $files): array $mapFile = $this->mapFile; $compactors = $this->compactors; $cwd = getcwd(); + $enableParallelization = $this->enableParallelization; - $processFile = static function (string $file) use ($cwd, $mapFile, $compactors): array { + $processFile = static function (string $file) use ($cwd, $mapFile, $compactors, $enableParallelization): array { chdir($cwd); // Keep the fully qualified call here since this function may be executed without the right autoloading // mechanism \KevinGH\Box\register_aliases(); - if (true === ParallelizationSettings::isParallelProcessingEnabled()) { + if ($enableParallelization) { \KevinGH\Box\register_error_handler(); } @@ -471,7 +477,7 @@ private function processContents(array $files): array return [$local, $processedContents, $compactors->getScoperSymbolsRegistry()]; }; - if ($this->scoper instanceof NullScoper || false === ParallelizationSettings::isParallelProcessingEnabled()) { + if ($this->scoper instanceof NullScoper || !$enableParallelization) { return array_map($processFile, $files); } diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 72c1a2349..949a655c3 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -39,7 +39,6 @@ use KevinGH\Box\Console\MessageRenderer; use KevinGH\Box\Constants; use KevinGH\Box\MapFile; -use KevinGH\Box\Parallelization\ParallelizationSettings; use KevinGH\Box\Phar\CompressionAlgorithm; use KevinGH\Box\Phar\SigningAlgorithm; use KevinGH\Box\RequirementChecker\DecodedComposerJson; @@ -192,8 +191,9 @@ public function execute(IO $io): int check_php_settings($io); - if ($io->getTypedOption(self::NO_PARALLEL_PROCESSING_OPTION)->asBoolean()) { - ParallelizationSettings::disableParallelProcessing(); + $enableParallelization = $io->getTypedOption(self::NO_PARALLEL_PROCESSING_OPTION)->asBoolean(); + + if ($enableParallelization) { $io->writeln( '[debug] Disabled parallel processing', OutputInterface::VERBOSITY_DEBUG, @@ -224,7 +224,7 @@ public function execute(IO $io): int $restoreLimit = bump_open_file_descriptor_limit(2048, $io); try { - $box = $this->createPhar($config, $logger, $io, $debug); + $box = $this->createPhar($config, $logger, $io, $debug, $enableParallelization); } finally { $restoreLimit(); } @@ -244,10 +244,11 @@ private function createPhar( Configuration $config, CompilerLogger $logger, IO $io, + bool $enableParallelization, bool $debug, ): Box { $tmpOutputPath = $config->getTmpOutputPath(); - $box = Box::create($tmpOutputPath); + $box = Box::create($tmpOutputPath, enableParallelization: $enableParallelization); $composerOrchestrator = new ComposerOrchestrator( ComposerProcessFactory::create( $config->getComposerBin(), diff --git a/src/Constants.php b/src/Constants.php index 036758689..8fa753326 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -21,7 +21,4 @@ final class Constants public const MEMORY_LIMIT = 'BOX_MEMORY_LIMIT'; public const ALLOW_XDEBUG = 'BOX_ALLOW_XDEBUG'; public const BIN = 'BOX_BIN'; - - /** @internal */ - public const NO_PARALLEL_PROCESSING = '_BOX_NO_PARALLEL_PROCESSING'; } diff --git a/src/Parallelization/ParallelizationSettings.php b/src/Parallelization/ParallelizationSettings.php deleted file mode 100644 index d60b9dc1a..000000000 --- a/src/Parallelization/ParallelizationSettings.php +++ /dev/null @@ -1,42 +0,0 @@ - - * 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\Parallelization; - -use KevinGH\Box\Constants; -use KevinGH\Box\NotInstantiable; -use function constant; -use function define; -use function defined; - -/** - * @private - */ -final class ParallelizationSettings -{ - use NotInstantiable; - - public static function disableParallelProcessing(): void - { - if (false === defined(Constants::NO_PARALLEL_PROCESSING)) { - define(Constants::NO_PARALLEL_PROCESSING, true); - } - } - - public static function isParallelProcessingEnabled(): bool - { - return false === defined(Constants::NO_PARALLEL_PROCESSING) - || false === constant(Constants::NO_PARALLEL_PROCESSING); - } -}