1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Db3v4l\Console; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
|
|
|
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Command\ListCommand; |
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
|
|
|
10
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
|
|
|
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
|
|
|
13
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError; |
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
|
|
|
15
|
|
|
use Symfony\Component\HttpKernel\Bundle\Bundle; |
|
|
|
|
16
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
|
|
|
17
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* An Application that only registers 'dbconsole' commands via a custom command loader |
21
|
|
|
* Sadly we can not easily extend Symfony\Bundle\FrameworkBundle\Console\Application, as it has private members. |
22
|
|
|
* So we reimplement it fully... |
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
class Application extends BaseApplication |
25
|
|
|
{ |
26
|
|
|
private $kernel; |
|
|
|
|
27
|
|
|
private $commandsRegistered = false; |
|
|
|
|
28
|
|
|
private $registrationErrors = []; |
|
|
|
|
29
|
|
|
|
30
|
|
|
public function __construct(KernelInterface $kernel) |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
$this->kernel = $kernel; |
33
|
|
|
|
34
|
|
|
parent::__construct('Symfony', Kernel::VERSION); |
35
|
|
|
|
36
|
|
|
$inputDefinition = $this->getDefinition(); |
37
|
|
|
$inputDefinition->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', $kernel->getEnvironment())); |
38
|
|
|
$inputDefinition->addOption(new InputOption('--no-debug', null, InputOption::VALUE_NONE, 'Switches off debug mode.')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets the Kernel associated with this Console. |
43
|
|
|
* |
44
|
|
|
* @return KernelInterface A KernelInterface instance |
45
|
|
|
*/ |
46
|
|
|
public function getKernel() |
47
|
|
|
{ |
48
|
|
|
return $this->kernel; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
|
|
|
|
54
|
|
|
public function reset() |
55
|
|
|
{ |
56
|
|
|
if ($this->kernel->getContainer()->has('services_resetter')) { |
57
|
|
|
$this->kernel->getContainer()->get('services_resetter')->reset(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
|
|
|
|
62
|
|
|
* Runs the current application. |
63
|
|
|
* |
64
|
|
|
* @return int 0 if everything went fine, or an error code |
65
|
|
|
*/ |
66
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
|
$this->registerCommands(); |
69
|
|
|
|
70
|
|
|
if ($this->registrationErrors) { |
|
|
|
|
71
|
|
|
$this->renderRegistrationErrors($input, $output); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher')); |
75
|
|
|
|
76
|
|
|
return parent::doRun($input, $output); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
|
|
|
|
82
|
|
|
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
83
|
|
|
{ |
84
|
|
|
if (!$command instanceof ListCommand) { |
85
|
|
|
if ($this->registrationErrors) { |
|
|
|
|
86
|
|
|
$this->renderRegistrationErrors($input, $output); |
87
|
|
|
$this->registrationErrors = []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return parent::doRunCommand($command, $input, $output); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$returnCode = parent::doRunCommand($command, $input, $output); |
94
|
|
|
|
95
|
|
|
if ($this->registrationErrors) { |
|
|
|
|
96
|
|
|
$this->renderRegistrationErrors($input, $output); |
97
|
|
|
$this->registrationErrors = []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $returnCode; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
|
|
|
|
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
|
|
|
|
106
|
|
|
public function find($name) |
107
|
|
|
{ |
108
|
|
|
$this->registerCommands(); |
109
|
|
|
|
110
|
|
|
return parent::find($name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
|
|
|
|
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
|
|
|
|
116
|
|
|
public function get($name) |
117
|
|
|
{ |
118
|
|
|
$this->registerCommands(); |
119
|
|
|
|
120
|
|
|
$command = parent::get($name); |
121
|
|
|
|
122
|
|
|
if ($command instanceof ContainerAwareInterface) { |
123
|
|
|
$command->setContainer($this->kernel->getContainer()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $command; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
|
|
|
|
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
|
|
|
|
132
|
|
|
public function all($namespace = null) |
133
|
|
|
{ |
134
|
|
|
$this->registerCommands(); |
135
|
|
|
|
136
|
|
|
return parent::all($namespace); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
|
|
|
|
142
|
|
|
public function getLongVersion() |
143
|
|
|
{ |
144
|
|
|
return parent::getLongVersion().sprintf(' (env: <comment>%s</>, debug: <comment>%s</>)', $this->kernel->getEnvironment(), $this->kernel->isDebug() ? 'true' : 'false'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function add(Command $command) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$this->registerCommands(); |
150
|
|
|
|
151
|
|
|
return parent::add($command); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function registerCommands() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
if ($this->commandsRegistered) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$this->commandsRegistered = true; |
161
|
|
|
|
162
|
|
|
$this->kernel->boot(); |
163
|
|
|
|
164
|
|
|
$container = $this->kernel->getContainer(); |
165
|
|
|
|
166
|
|
|
/// @todo throw if not |
167
|
|
|
if ($container->has('dbconsole.command_loader')) { |
168
|
|
|
$this->setCommandLoader($container->get('dbconsole.command_loader')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($container->hasParameter('dbconsole.command.ids')) { |
172
|
|
|
$lazyCommandIds = $container->hasParameter('dbconsole.lazy_command.ids') ? $container->getParameter('dbconsole.lazy_command.ids') : []; |
173
|
|
|
foreach ($container->getParameter('dbconsole.command.ids') as $id) { |
174
|
|
|
if (!isset($lazyCommandIds[$id])) { |
175
|
|
|
try { |
176
|
|
|
$this->add($container->get($id)); |
177
|
|
|
} catch (\Throwable $e) { |
178
|
|
|
$this->registrationErrors[] = $e; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function renderRegistrationErrors(InputInterface $input, OutputInterface $output) |
|
|
|
|
186
|
|
|
{ |
187
|
|
|
if ($output instanceof ConsoleOutputInterface) { |
188
|
|
|
$output = $output->getErrorOutput(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
(new SymfonyStyle($input, $output))->warning('Some commands could not be registered:'); |
192
|
|
|
|
193
|
|
|
foreach ($this->registrationErrors as $error) { |
194
|
|
|
if (method_exists($this, 'doRenderThrowable')) { |
195
|
|
|
$this->doRenderThrowable($error, $output); |
196
|
|
|
} else { |
197
|
|
|
if (!$error instanceof \Exception) { |
198
|
|
|
$error = new FatalThrowableError($error); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$this->doRenderException($error, $output); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|