-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWorkerCommandLineFactory.php
More file actions
169 lines (134 loc) · 5.23 KB
/
Copy pathWorkerCommandLineFactory.php
File metadata and controls
169 lines (134 loc) · 5.23 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace Symplify\EasyCodingStandard\Parallel\CommandLine;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symplify\EasyCodingStandard\Parallel\Exception\ParallelShouldNotHappenException;
use Symplify\EasyCodingStandard\Parallel\Reflection\CommandFromReflectionFactory;
/**
* @api
*/
final class WorkerCommandLineFactory
{
private const string OPTION_DASHES = '--';
/**
* These options are not relevant for nested worker command line.
*
* @var string[]
*/
private const array EXCLUDED_OPTION_NAMES = ['output-format'];
private readonly CommandFromReflectionFactory $commandFromReflectionFactory;
public function __construct()
{
$this->commandFromReflectionFactory = new CommandFromReflectionFactory();
}
/**
* @param class-string<Command> $mainCommandClass
*/
public function create(
string $baseScript,
string $mainCommandClass,
string $workerCommandName,
string $pathsOptionName,
?string $projectConfigFile,
InputInterface $input,
string $identifier,
int $port
): string {
$commandArguments = array_slice($_SERVER['argv'], 1);
$args = array_merge([PHP_BINARY, $baseScript], $commandArguments);
$mainCommand = $this->commandFromReflectionFactory->create($mainCommandClass);
if ($mainCommand->getName() === null) {
$errorMessage = sprintf('The command name for "%s" is missing', $mainCommand::class);
throw new ParallelShouldNotHappenException($errorMessage);
}
$mainCommandName = $mainCommand->getName();
$processCommandArray = [];
foreach ($args as $arg) {
// skip command name
if ($arg === $mainCommandName) {
break;
}
$processCommandArray[] = escapeshellarg((string) $arg);
}
$processCommandArray[] = $workerCommandName;
if ($projectConfigFile !== null) {
$processCommandArray[] = '--config';
$processCommandArray[] = escapeshellarg($projectConfigFile);
}
$mainCommandOptionNames = $this->getCommandOptionNames($mainCommand);
$processCommandOptions = $this->mirrorCommandOptions($input, $mainCommandOptionNames);
$processCommandArray = array_merge($processCommandArray, $processCommandOptions);
// for TCP local server
$processCommandArray[] = '--port';
$processCommandArray[] = $port;
$processCommandArray[] = '--identifier';
$processCommandArray[] = escapeshellarg($identifier);
/** @var string[] $paths */
$paths = (array) $input->getArgument($pathsOptionName);
foreach ($paths as $path) {
$processCommandArray[] = escapeshellarg($path);
}
// set json output
$processCommandArray[] = '--output-format';
$processCommandArray[] = escapeshellarg('json');
// explicitly disable colors, breaks json_decode() otherwise
// @see https://github.com/symfony/symfony/issues/1238
$processCommandArray[] = '--no-ansi';
return implode(' ', $processCommandArray);
}
/**
* @return string[]
*/
private function getCommandOptionNames(Command $command): array
{
$inputDefinition = $command->getDefinition();
$optionNames = [];
foreach ($inputDefinition->getOptions() as $inputOption) {
$optionNames[] = $inputOption->getName();
}
return $optionNames;
}
/**
* Keeps all options that are allowed in check command options
*
* @param string[] $mainCommandOptionNames
* @return string[]
*/
private function mirrorCommandOptions(InputInterface $input, array $mainCommandOptionNames): array
{
$processCommandOptions = [];
foreach ($mainCommandOptionNames as $mainCommandOptionName) {
if ($this->shouldSkipOption($input, $mainCommandOptionName)) {
continue;
}
/** @var bool|string|null $optionValue */
$optionValue = $input->getOption($mainCommandOptionName);
// skip clutter
if ($optionValue === null) {
continue;
}
if (is_bool($optionValue)) {
if ($optionValue) {
$processCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName;
}
continue;
}
if ($mainCommandOptionName === 'memory-limit') {
// symfony/console does not accept -1 as value without assign
$processCommandOptions[] = '--' . $mainCommandOptionName . '=' . $optionValue;
} else {
$processCommandOptions[] = self::OPTION_DASHES . $mainCommandOptionName;
$processCommandOptions[] = escapeshellarg($optionValue);
}
}
return $processCommandOptions;
}
private function shouldSkipOption(InputInterface $input, string $optionName): bool
{
if (! $input->hasOption($optionName)) {
return true;
}
return in_array($optionName, self::EXCLUDED_OPTION_NAMES, true);
}
}