-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWorkerCommandLineFactory.php
More file actions
86 lines (69 loc) · 2.59 KB
/
Copy pathWorkerCommandLineFactory.php
File metadata and controls
86 lines (69 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace Symplify\EasyCodingStandard\Parallel\CommandLine;
final class WorkerCommandLineFactory
{
private const string OPTION_DASHES = '--';
/**
* @param array<string, bool|string|null> $workerOptionValues option name => value, mirrored to the worker process
* @param string[] $paths
*/
public function create(
string $baseScript,
string $workerCommandName,
?string $projectConfigFile,
array $workerOptionValues,
array $paths,
string $identifier,
int $port
): string {
$processCommandArray = [escapeshellarg(PHP_BINARY), escapeshellarg($baseScript), $workerCommandName];
if ($projectConfigFile !== null) {
$processCommandArray[] = '--config';
$processCommandArray[] = escapeshellarg($projectConfigFile);
}
foreach ($this->mirrorCommandOptions($workerOptionValues) as $processCommandOption) {
$processCommandArray[] = $processCommandOption;
}
// for TCP local server
$processCommandArray[] = '--port';
$processCommandArray[] = (string) $port;
$processCommandArray[] = '--identifier';
$processCommandArray[] = escapeshellarg($identifier);
foreach ($paths as $path) {
$processCommandArray[] = escapeshellarg($path);
}
// set json output
$processCommandArray[] = '--output-format';
$processCommandArray[] = escapeshellarg('json');
return implode(' ', $processCommandArray);
}
/**
* @param array<string, bool|string|null> $workerOptionValues
* @return string[]
*/
private function mirrorCommandOptions(array $workerOptionValues): array
{
$processCommandOptions = [];
foreach ($workerOptionValues as $optionName => $optionValue) {
// skip clutter
if ($optionValue === null) {
continue;
}
if (is_bool($optionValue)) {
if ($optionValue) {
$processCommandOptions[] = self::OPTION_DASHES . $optionName;
}
continue;
}
if ($optionName === 'memory-limit') {
// does not accept -1 as value without assign
$processCommandOptions[] = '--' . $optionName . '=' . $optionValue;
} else {
$processCommandOptions[] = self::OPTION_DASHES . $optionName;
$processCommandOptions[] = escapeshellarg($optionValue);
}
}
return $processCommandOptions;
}
}