1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\DependencyInjection\CompilerPass; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\Annotations\CompilerPass; |
6
|
|
|
use BrainExe\Core\Console\ProxyCommand; |
7
|
|
|
use Symfony\Component\Console\Command\Command; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
14
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @CompilerPass |
18
|
|
|
*/ |
19
|
|
|
class ConsoleCompilerPass implements CompilerPassInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
const TAG = 'console'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
3 |
|
public function process(ContainerBuilder $container) |
28
|
|
|
{ |
29
|
3 |
|
$console = $container->getDefinition('Console'); |
30
|
3 |
|
$console->addMethodCall('setAutoExit', [false]); |
31
|
|
|
|
32
|
3 |
|
$taggedServices = $container->findTaggedServiceIds(self::TAG); |
33
|
|
|
|
34
|
3 |
|
$commands = []; |
35
|
3 |
|
foreach (array_keys($taggedServices) as $serviceId) { |
36
|
|
|
/** @var Command $command */ |
37
|
3 |
|
$command = $container->get($serviceId); |
38
|
|
|
|
39
|
3 |
|
$proxyService = new Definition(ProxyCommand::class, [ |
40
|
3 |
|
new Reference('service_container'), |
41
|
3 |
|
new Reference('Console'), |
42
|
3 |
|
$serviceId, |
43
|
3 |
|
$command->getName(), |
44
|
3 |
|
$command->getDescription(), |
45
|
3 |
|
$command->getAliases(), |
46
|
3 |
|
$this->formatDefinition($command->getDefinition()) |
47
|
|
|
]); |
48
|
3 |
|
$commands[] = $proxyService; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$console->addMethodCall('addCommands', [$commands]); |
52
|
3 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param InputDefinition $definition |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
3 |
|
private function formatDefinition(InputDefinition $definition) |
59
|
|
|
{ |
60
|
3 |
|
$arguments = []; |
61
|
|
|
|
62
|
3 |
|
foreach ($definition->getArguments() as $argument) { |
63
|
2 |
|
$mode = $this->getArgumentMode($argument); |
64
|
|
|
|
65
|
2 |
|
$arguments[] = new Definition( |
66
|
2 |
|
InputArgument::class, |
67
|
2 |
|
[$argument->getName(), $mode, $argument->getDescription(), $argument->getDefault()] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
foreach ($definition->getOptions() as $option) { |
72
|
2 |
|
$mode = $this->getOptionMode($option); |
73
|
|
|
|
74
|
2 |
|
$arguments[] = new Definition( |
75
|
2 |
|
InputOption::class, |
76
|
|
|
[ |
77
|
2 |
|
$option->getName(), |
78
|
2 |
|
$option->getShortcut(), |
79
|
2 |
|
$mode, |
80
|
2 |
|
$option->getDescription(), |
81
|
2 |
|
$option->getDefault() ?: null |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return $arguments; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param InputArgument $argument |
91
|
|
|
* @return int |
92
|
|
|
*/ |
93
|
2 |
|
private function getArgumentMode(InputArgument $argument) |
94
|
|
|
{ |
95
|
2 |
|
$mode = 0; |
96
|
2 |
|
if ($argument->isRequired()) { |
97
|
2 |
|
$mode |= InputArgument::REQUIRED; |
98
|
|
|
} |
99
|
2 |
|
if ($argument->isArray()) { |
100
|
|
|
$mode |= InputArgument::IS_ARRAY; |
101
|
|
|
} |
102
|
2 |
|
if (!$argument->isRequired()) { |
103
|
2 |
|
$mode |= InputArgument::OPTIONAL; |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return $mode; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param InputOption $option |
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
2 |
|
private function getOptionMode(InputOption $option) |
114
|
|
|
{ |
115
|
2 |
|
$mode = 0; |
116
|
2 |
|
if ($option->isArray()) { |
117
|
|
|
$mode |= InputOption::VALUE_IS_ARRAY; |
118
|
|
|
} |
119
|
2 |
|
if ($option->isValueOptional()) { |
120
|
|
|
$mode |= InputOption::VALUE_OPTIONAL; |
121
|
|
|
} |
122
|
2 |
|
if ($option->isValueRequired()) { |
123
|
|
|
$mode |= InputOption::VALUE_REQUIRED; |
124
|
|
|
} |
125
|
2 |
|
if (!$mode) { |
126
|
2 |
|
$mode = InputOption::VALUE_NONE; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $mode; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|