-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathChildCommandFactory.php
More file actions
107 lines (95 loc) · 2.78 KB
/
ChildCommandFactory.php
File metadata and controls
107 lines (95 loc) · 2.78 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
<?php
/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <office@webmozarts.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Webmozarts\Console\Parallelization\Input;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Webmozart\Assert\Assert;
use function array_filter;
use function array_map;
use function Safe\getcwd;
use function sprintf;
/**
* @internal
*/
final readonly class ChildCommandFactory
{
/**
* @param list<string> $phpExecutable
*/
public function __construct(
public array $phpExecutable,
private string $scriptPath,
private string $commandName,
private InputDefinition $commandDefinition,
) {
self::validateScriptPath($scriptPath);
}
/**
* @return list<string>
*/
public function createChildCommand(InputInterface $input): array
{
return [...$this->createBaseCommand($input), ...$this->getForwardedOptions($input)];
}
/**
* @return array<int, string>
*/
private function createBaseCommand(
InputInterface $input
): array {
return array_filter([
//...$this->phpExecutable,
$this->scriptPath,
$this->commandName,
...array_map(strval(...), self::getArguments($input)),
'--child',
]);
}
/**
* @return list<string>
*/
private function getForwardedOptions(InputInterface $input): array
{
// Forward all the options except for "processes" to the children
// this way the children can inherit the options such as env
// or no-debug.
return InputOptionsSerializer::serialize(
$this->commandDefinition,
$input,
ParallelizationInput::OPTIONS,
);
}
/**
* @return list<string|bool|int|float|null|array<string|bool|int|float|null>>
*/
private static function getArguments(InputInterface $input): array
{
$arguments = RawInput::getRawArguments($input);
// Remove the item: we do not want it to be passed to child processes
// ever.
unset(
$arguments['command'],
$arguments[ParallelizationInput::ITEM_ARGUMENT],
);
return array_values($arguments);
}
private static function validateScriptPath(string $scriptPath): void
{
Assert::fileExists(
$scriptPath,
sprintf(
'The script file could not be found at the path "%s" (working directory: %s)',
$scriptPath,
getcwd(),
),
);
}
}