diff --git a/src/Box.php b/src/Box.php index 04581a4d0..39d3fb747 100644 --- a/src/Box.php +++ b/src/Box.php @@ -70,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([]); @@ -86,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)); @@ -95,6 +100,7 @@ public static function create(string $pharFilePath, int $pharFlags = 0, ?string return new self( new Phar($pharFilePath, $pharFlags, $pharAlias), $pharFilePath, + $enableParallelization, ); } @@ -450,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 === \KevinGH\Box\is_parallel_processing_enabled()) { + if ($enableParallelization) { \KevinGH\Box\register_error_handler(); } @@ -470,7 +477,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 || !$enableParallelization) { return array_map($processFile, $files); } diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 871b8f32a..11c817eb9 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -64,7 +64,6 @@ use function implode; use function is_callable; use function is_string; -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; @@ -192,8 +191,9 @@ public function execute(IO $io): int PhpSettingsChecker::check($io); - if ($io->getTypedOption(self::NO_PARALLEL_PROCESSING_OPTION)->asBoolean()) { - disable_parallel_processing(); + $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 = OpenFileDescriptorLimiter::bumpLimit(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/functions.php b/src/functions.php index a328137e8..1ff33245f 100644 --- a/src/functions.php +++ b/src/functions.php @@ -22,9 +22,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 is_float; use function is_int; @@ -168,24 +165,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 *