humbug /
box
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the box project. |
||
| 7 | * |
||
| 8 | * (c) Kevin Herrera <[email protected]> |
||
| 9 | * Théo Fidry <[email protected]> |
||
| 10 | * |
||
| 11 | * This source file is subject to the MIT license that is bundled |
||
| 12 | * with this source code in the file LICENSE. |
||
| 13 | */ |
||
| 14 | |||
| 15 | namespace KevinGH\Box\Parallelization; |
||
| 16 | |||
| 17 | use Amp\Cancellation; |
||
| 18 | use Amp\Parallel\Worker\Task; |
||
| 19 | use Amp\Sync\Channel; |
||
| 20 | use Fidry\FileSystem\FS; |
||
| 21 | use Humbug\PhpScoper\Symbol\SymbolsRegistry; |
||
| 22 | use KevinGH\Box\Compactor\Compactors; |
||
| 23 | use KevinGH\Box\MapFile; |
||
| 24 | use function array_map; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @private |
||
| 28 | */ |
||
| 29 | final readonly class ProcessFileTask implements Task |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @param string[] $filePaths |
||
| 33 | */ |
||
| 34 | public function __construct( |
||
| 35 | private array $filePaths, |
||
| 36 | private string $cwd, |
||
| 37 | private MapFile $mapFile, |
||
| 38 | private Compactors $compactors, |
||
| 39 | ) { |
||
| 40 | } |
||
| 41 | |||
| 42 | public function run(Channel $channel, Cancellation $cancellation): TaskResult |
||
| 43 | { |
||
| 44 | chdir($this->cwd); |
||
| 45 | |||
| 46 | $mapFile = $this->mapFile; |
||
| 47 | $compactors = $this->compactors; |
||
| 48 | |||
| 49 | $processFile = static function (string $file) use ($mapFile, $compactors): array { |
||
| 50 | $contents = FS::getFileContents($file); |
||
| 51 | |||
| 52 | $local = $mapFile($file); |
||
| 53 | |||
| 54 | $processedContents = $compactors->compact($local, $contents); |
||
| 55 | |||
| 56 | return [$local, $processedContents, $compactors->getScoperSymbolsRegistry()]; |
||
| 57 | }; |
||
| 58 | |||
| 59 | $tuples = array_map($processFile, $this->filePaths); |
||
| 60 | |||
| 61 | $filesWithContents = []; |
||
| 62 | $symbolRegistries = []; |
||
| 63 | |||
| 64 | foreach ($tuples as [$local, $processedContents, $symbolRegistry]) { |
||
| 65 | $filesWithContents[] = [$local, $processedContents]; |
||
| 66 | $symbolRegistries[] = $symbolRegistry; |
||
| 67 | } |
||
| 68 | |||
| 69 | return new TaskResult( |
||
| 70 | $filesWithContents, |
||
| 71 | SymbolsRegistry::createFromRegistries(array_filter($symbolRegistries)), |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | } |
||
| 75 |