forked from sculpin/sculpin
/
Application.php
202 lines (171 loc) · 5.9 KB
/
Application.php
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
declare(strict_types=1);
/*
* This file is a part of Sculpin.
*
* (c) Dragonfly Development Inc.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sculpin\Bundle\SculpinBundle\Console;
use Sculpin\Core\Sculpin;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* @author Beau Simensen <beau@dflydev.com>
*/
final class Application extends BaseApplication
{
/**
* @var KernelInterface
*/
private KernelInterface $kernel;
/**
* @var \Throwable[]
*/
private array $registrationErrors = [];
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
date_default_timezone_set(@date_default_timezone_get());
}
parent::__construct(
'Sculpin',
$kernel->getName() . '/' . $kernel->getEnvironment() . ($kernel->isDebug() ? '/debug' : '')
);
$this->getDefinition()->addOption(new InputOption(
'--project-dir',
null,
InputOption::VALUE_REQUIRED,
'The project directory.',
'.'
));
$this->getDefinition()->addOption(new InputOption(
'--env',
'-e',
InputOption::VALUE_REQUIRED,
'The Environment name.',
$kernel->getEnvironment()
));
$this->getDefinition()->addOption(new InputOption(
'--no-debug',
null,
InputOption::VALUE_NONE,
'Switches off debug mode.'
));
$this->getDefinition()->addOption(new InputOption(
'--safe',
null,
InputOption::VALUE_NONE,
'Enable safe mode (no bundles loaded, no kernel booted)'
));
}
/**
* {@inheritDoc}
*/
public function run(InputInterface $input = null, OutputInterface $output = null): int
{
if (null === $output) {
$styles = [
'highlight' => new OutputFormatterStyle('red'),
'warning' => new OutputFormatterStyle('black', 'yellow'),
];
$formatter = new OutputFormatter(false, $styles);
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
}
return parent::run($input, $output);
}
/**
* {@inheritdoc}
*/
public function doRun(InputInterface $input, OutputInterface $output): int
{
if (!$input->hasParameterOption('--safe')) {
// In safe mode enable no commands
$this->registerCommands();
if ($this->registrationErrors) {
$this->renderRegistrationErrors($input, $output);
}
}
$exitCode = parent::doRun($input, $output);
foreach ($this->getMissingSculpinBundlesMessages() as $message) {
$output->writeln($message);
}
return $exitCode;
}
public function getMissingSculpinBundlesMessages(): array
{
$messages = [];
// Display missing bundle to user.
if ($missingBundles = $this->kernel->getMissingSculpinBundles()) {
$messages[] = '';
$messages[] = '<comment>Missing Sculpin Bundles:</comment>';
foreach ($missingBundles as $bundle) {
$messages[] = " * <highlight>$bundle</highlight>";
}
$messages[] = '';
}
return $messages;
}
/**
* Get Kernel
*
* @return KernelInterface
*/
public function getKernel(): KernelInterface
{
return $this->kernel;
}
private function registerCommands(): void
{
$this->kernel->boot();
foreach ($this->kernel->getBundles() as $bundle) {
if ($bundle instanceof Bundle) {
$bundle->registerCommands($this);
}
}
$container = $this->kernel->getContainer();
if ($container->has('console.command_loader')) {
$this->setCommandLoader($container->get('console.command_loader'));
}
if (!$container->hasParameter('console.command.ids')) {
return;
}
$lazyCommandIds = $container->hasParameter('console.lazy_command.ids')
? $container->getParameter('console.lazy_command.ids')
: [];
foreach ($container->getParameter('console.command.ids') as $id) {
if (!isset($lazyCommandIds[$id])) {
try {
$this->add($container->get($id));
} catch (\Exception $e) {
$this->registrationErrors[] = $e;
} catch (\Throwable $e) {
$this->registrationErrors[] = new FatalThrowableError($e);
}
}
}
}
private function renderRegistrationErrors(InputInterface $input, OutputInterface $output): void
{
if ($output instanceof ConsoleOutputInterface) {
$output = $output->getErrorOutput();
}
(new SymfonyStyle($input, $output))->warning('Some commands could not be registered:');
foreach ($this->registrationErrors as $error) {
$this->doRenderException($error, $output);
}
}
}