From db2c6a0adcc300accdb1e15012aaae24071cec86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Fri, 15 Dec 2023 19:19:07 +0100 Subject: [PATCH] WIp --- src/Box.php | 35 ++++++++++++------- src/Console/Command/Compile.php | 8 +++-- src/Filesystem/File.php | 9 +++++ src/Filesystem/LocalPharFile.php | 24 +++++++++++++ src/Parallelization/ParallelFileProcessor.php | 5 +-- src/Parallelization/ProcessFileTask.php | 11 +++--- src/Parallelization/TaskResult.php | 7 ++-- tests/Parallelization/TaskResultTest.php | 17 ++++----- 8 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 src/Filesystem/File.php create mode 100644 src/Filesystem/LocalPharFile.php diff --git a/src/Box.php b/src/Box.php index 29de662d0..68d5f2745 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\Filesystem\LocalPharFile; use KevinGH\Box\Parallelization\ParallelFileProcessor; use KevinGH\Box\Parallelization\ParallelizationDecider; use KevinGH\Box\Phar\CompressionAlgorithm; @@ -47,6 +48,7 @@ use function openssl_pkey_get_details; use function openssl_pkey_get_private; use function sprintf; +use function strval; /** * Box is a utility class to generate a PHAR. @@ -62,7 +64,7 @@ final class Box implements Countable private bool $buffering = false; /** - * @var array Relative file path as key and file contents as value + * @var array */ private array $bufferedFiles = []; @@ -132,8 +134,11 @@ public function endBuffering(?callable $dumpAutoload): void } try { - foreach ($this->bufferedFiles as $file => $contents) { - FS::dumpFile($file, $contents); + foreach ($this->bufferedFiles as $file) { + FS::dumpFile( + $file->path, + $file->contents, + ); } if (null !== $dumpAutoload) { @@ -294,7 +299,7 @@ public function addFiles(array $files, bool $binary): void { Assert::true($this->buffering, 'Cannot add files if the buffering has not started.'); - $files = array_map('strval', $files); + $files = array_map(strval(...), $files); if ($binary) { foreach ($files as $file) { @@ -304,8 +309,8 @@ public function addFiles(array $files, bool $binary): void return; } - foreach ($this->processContents($files) as [$file, $contents]) { - $this->bufferedFiles[$file] = $contents; + foreach ($this->processContents($files) as $file) { + $this->bufferedFiles[$file->path] = $file; } } @@ -328,7 +333,12 @@ public function addFile(string $file, ?string $contents = null, bool $binary = f $local = ($this->mapFile)($file); - $this->bufferedFiles[$local] = $binary ? $contents : $this->compactors->compact($local, $contents); + $this->bufferedFiles[$local] = new LocalPharFile( + $local, + $binary ? + $contents : + $this->compactors->compact($local, $contents), + ); return $local; } @@ -445,8 +455,7 @@ public function signUsingKey(string $key, ?string $password): void /** * @param string[] $files * - * @return array array of tuples where the first element is the local file path (path inside the PHAR) and the - * second element is the processed contents + * @return LocalPharFile[] */ private function processContents(array $files): array { @@ -472,7 +481,7 @@ private function processContents(array $files): array /** * @param string[] $files * - * @return list + * @return LocalPharFile[] */ private static function processFilesSynchronously( array $files, @@ -480,17 +489,17 @@ private static function processFilesSynchronously( MapFile $mapFile, Compactors $compactors, ): array { - $processFile = static function (string $file) use ($mapFile, $compactors): array { + $processFile = static function (string $file) use ($mapFile, $compactors): LocalPharFile { $contents = FS::getFileContents($file); $local = $mapFile($file); $processedContents = $compactors->compact($local, $contents); - return [ + return new LocalPharFile( $local, $processedContents, - ]; + ); }; return array_map($processFile, $files); diff --git a/src/Console/Command/Compile.php b/src/Console/Command/Compile.php index 260c5d597..e44cf3450 100644 --- a/src/Console/Command/Compile.php +++ b/src/Console/Command/Compile.php @@ -601,16 +601,18 @@ private static function registerStub( return; } - if (null !== ($stub = $config->getStubPath())) { + $stubPath = $config->getStubPath(); + + if (null !== $stubPath) { $logger->log( CompilerLogger::QUESTION_MARK_PREFIX, sprintf( 'Using stub file: %s', - $stub, + $stubPath, ), ); - $box->registerStub($stub); + $box->registerStub($stubPath); return; } diff --git a/src/Filesystem/File.php b/src/Filesystem/File.php new file mode 100644 index 000000000..908e058cf --- /dev/null +++ b/src/Filesystem/File.php @@ -0,0 +1,9 @@ +path; + } + + public function getContents(): string + { + return $this->contents; + } +} \ No newline at end of file diff --git a/src/Parallelization/ParallelFileProcessor.php b/src/Parallelization/ParallelFileProcessor.php index eda020b98..c4b098f8e 100644 --- a/src/Parallelization/ParallelFileProcessor.php +++ b/src/Parallelization/ParallelFileProcessor.php @@ -18,6 +18,7 @@ use Amp\Parallel\Worker\TaskFailureThrowable; use Amp\Parallel\Worker\WorkerPool; use KevinGH\Box\Compactor\Compactors; +use KevinGH\Box\Filesystem\LocalPharFile; use KevinGH\Box\MapFile; use function Amp\Future\await; use function Amp\Parallel\Worker\workerPool; @@ -35,7 +36,7 @@ final class ParallelFileProcessor * * @throws TaskFailureThrowable * - * @return list + * @return LocalPharFile[] */ public static function processFilesInParallel( array $filePaths, @@ -55,7 +56,7 @@ public static function processFilesInParallel( $compactors->registerSymbolsRegistry($result->symbolsRegistry); - return $result->filesWithContents; + return $result->localPharFiles; } /** diff --git a/src/Parallelization/ProcessFileTask.php b/src/Parallelization/ProcessFileTask.php index daf2060f3..87c0eaef2 100644 --- a/src/Parallelization/ProcessFileTask.php +++ b/src/Parallelization/ProcessFileTask.php @@ -20,6 +20,7 @@ use Fidry\FileSystem\FS; use Humbug\PhpScoper\Symbol\SymbolsRegistry; use KevinGH\Box\Compactor\Compactors; +use KevinGH\Box\Filesystem\LocalPharFile; use KevinGH\Box\MapFile; use function array_map; @@ -53,21 +54,21 @@ public function run(Channel $channel, Cancellation $cancellation): TaskResult $processedContents = $compactors->compact($local, $contents); - return [$local, $processedContents, $compactors->getScoperSymbolsRegistry()]; + return [new LocalPharFile($local, $processedContents), $compactors->getScoperSymbolsRegistry()]; }; $tuples = array_map($processFile, $this->filePaths); - $filesWithContents = []; + $localPharFiles = []; $symbolRegistries = []; - foreach ($tuples as [$local, $processedContents, $symbolRegistry]) { - $filesWithContents[] = [$local, $processedContents]; + foreach ($tuples as [$localPharFile, $symbolRegistry]) { + $localPharFiles[] = $localPharFile; $symbolRegistries[] = $symbolRegistry; } return new TaskResult( - $filesWithContents, + $localPharFiles, SymbolsRegistry::createFromRegistries(array_filter($symbolRegistries)), ); } diff --git a/src/Parallelization/TaskResult.php b/src/Parallelization/TaskResult.php index 5c58f536b..9e18c9bd4 100644 --- a/src/Parallelization/TaskResult.php +++ b/src/Parallelization/TaskResult.php @@ -15,6 +15,7 @@ namespace KevinGH\Box\Parallelization; use Humbug\PhpScoper\Symbol\SymbolsRegistry; +use KevinGH\Box\Filesystem\LocalPharFile; use function array_merge; /** @@ -31,7 +32,7 @@ public static function aggregate(array $results): self $symbolsRegistries = []; foreach ($results as $result) { - $filesWithContents[] = $result->filesWithContents; + $filesWithContents[] = $result->localPharFiles; $symbolsRegistries[] = $result->symbolsRegistry; } @@ -42,10 +43,10 @@ public static function aggregate(array $results): self } /** - * @param list $filesWithContents + * @param LocalPharFile[] $localPharFiles */ public function __construct( - public array $filesWithContents, + public array $localPharFiles, public SymbolsRegistry $symbolsRegistry, ) { } diff --git a/tests/Parallelization/TaskResultTest.php b/tests/Parallelization/TaskResultTest.php index c1f64078d..86eb04112 100644 --- a/tests/Parallelization/TaskResultTest.php +++ b/tests/Parallelization/TaskResultTest.php @@ -15,6 +15,7 @@ namespace KevinGH\Box\Parallelization; use Humbug\PhpScoper\Symbol\SymbolsRegistry; +use KevinGH\Box\Filesystem\LocalPharFile; use PhpParser\Node\Name\FullyQualified; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -64,25 +65,25 @@ public static function resultsProvider(): iterable [ new TaskResult( [ - ['fileA', 'contentA'], - ['fileB', 'contentB'], + new LocalPharFile('fileA', 'contentA'), + new LocalPharFile('fileB', 'contentB'), ], $symbolsRegistry1, ), new TaskResult( [ - ['fileC', 'contentC'], - ['fileD', 'contentD'], + new LocalPharFile('fileC', 'contentC'), + new LocalPharFile('fileD', 'contentD'), ], $symbolsRegistry2, ), ], new TaskResult( [ - ['fileA', 'contentA'], - ['fileB', 'contentB'], - ['fileC', 'contentC'], - ['fileD', 'contentD'], + new LocalPharFile('fileA', 'contentA'), + new LocalPharFile('fileB', 'contentB'), + new LocalPharFile('fileC', 'contentC'), + new LocalPharFile('fileD', 'contentD'), ], $expectedSymbolsRegistry, ),