|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Symplify\EasyCodingStandard\Parallel\CommandLine; |
| 5 | + |
| 6 | +final class WorkerCommandLineFactory |
| 7 | +{ |
| 8 | + /** |
| 9 | + * @var string |
| 10 | + */ |
| 11 | + private const OPTION_DASHES = '--'; |
| 12 | + /** |
| 13 | + * @param array<string, bool|string|null> $workerOptionValues option name => value, mirrored to the worker process |
| 14 | + * @param string[] $paths |
| 15 | + */ |
| 16 | + public function create(string $baseScript, string $workerCommandName, ?string $projectConfigFile, array $workerOptionValues, array $paths, string $identifier, int $port): string |
| 17 | + { |
| 18 | + $processCommandArray = [escapeshellarg(\PHP_BINARY), escapeshellarg($baseScript), $workerCommandName]; |
| 19 | + if ($projectConfigFile !== null) { |
| 20 | + $processCommandArray[] = '--config'; |
| 21 | + $processCommandArray[] = escapeshellarg($projectConfigFile); |
| 22 | + } |
| 23 | + foreach ($this->mirrorCommandOptions($workerOptionValues) as $processCommandOption) { |
| 24 | + $processCommandArray[] = $processCommandOption; |
| 25 | + } |
| 26 | + // for TCP local server |
| 27 | + $processCommandArray[] = '--port'; |
| 28 | + $processCommandArray[] = (string) $port; |
| 29 | + $processCommandArray[] = '--identifier'; |
| 30 | + $processCommandArray[] = escapeshellarg($identifier); |
| 31 | + foreach ($paths as $path) { |
| 32 | + $processCommandArray[] = escapeshellarg($path); |
| 33 | + } |
| 34 | + // set json output |
| 35 | + $processCommandArray[] = '--output-format'; |
| 36 | + $processCommandArray[] = escapeshellarg('json'); |
| 37 | + return implode(' ', $processCommandArray); |
| 38 | + } |
| 39 | + /** |
| 40 | + * @param array<string, bool|string|null> $workerOptionValues |
| 41 | + * @return string[] |
| 42 | + */ |
| 43 | + private function mirrorCommandOptions(array $workerOptionValues): array |
| 44 | + { |
| 45 | + $processCommandOptions = []; |
| 46 | + foreach ($workerOptionValues as $optionName => $optionValue) { |
| 47 | + // skip clutter |
| 48 | + if ($optionValue === null) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + if (is_bool($optionValue)) { |
| 52 | + if ($optionValue) { |
| 53 | + $processCommandOptions[] = self::OPTION_DASHES . $optionName; |
| 54 | + } |
| 55 | + continue; |
| 56 | + } |
| 57 | + if ($optionName === 'memory-limit') { |
| 58 | + // does not accept -1 as value without assign |
| 59 | + $processCommandOptions[] = '--' . $optionName . '=' . $optionValue; |
| 60 | + } else { |
| 61 | + $processCommandOptions[] = self::OPTION_DASHES . $optionName; |
| 62 | + $processCommandOptions[] = escapeshellarg($optionValue); |
| 63 | + } |
| 64 | + } |
| 65 | + return $processCommandOptions; |
| 66 | + } |
| 67 | +} |
0 commit comments