-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathInvokeSubCommand.php
More file actions
62 lines (52 loc) · 1.89 KB
/
InvokeSubCommand.php
File metadata and controls
62 lines (52 loc) · 1.89 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
<?php
declare(strict_types=1);
namespace Php\Pie\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozart\Assert\Assert;
use function array_combine;
use function array_filter;
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
class InvokeSubCommand
{
public function __construct(private readonly OutputInterface $output)
{
}
/** @param array<array-key, mixed> $subCommandInput */
public function __invoke(
Command $command,
array $subCommandInput,
InputInterface $originalCommandInput,
OutputFormatter|null $formatter = null,
): int {
$originalSuppliedOptions = array_filter($originalCommandInput->getOptions());
$installForProjectInput = new ArrayInput(array_merge(
$subCommandInput,
array_combine(
array_map(static fn ($key) => '--' . $key, array_keys($originalSuppliedOptions)),
array_values($originalSuppliedOptions),
),
));
$application = $command->getApplication();
Assert::notNull($application);
if ($formatter instanceof OutputFormatter) {
$oldFormatter = $this->output->getFormatter();
$this->output->setFormatter($formatter);
}
try {
$result = $application->doRun($installForProjectInput, $this->output);
} finally {
if ($formatter instanceof OutputFormatter) {
$this->output->setFormatter($oldFormatter);
}
}
return $result;
}
}