11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Console ;
13
13
14
+ use Symfony \Component \Console \Output \ConsoleOutputInterface ;
15
+ use Symfony \Component \Console \Style \SymfonyStyle ;
16
+ use Symfony \Component \Debug \Exception \FatalThrowableError ;
14
17
use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
15
18
use Symfony \Component \Console \Application as BaseApplication ;
16
19
use Symfony \Component \Console \Command \Command ;
@@ -30,6 +33,7 @@ class Application extends BaseApplication
30
33
{
31
34
private $ kernel ;
32
35
private $ commandsRegistered = false ;
36
+ private $ registrationErrors = array ();
33
37
34
38
/**
35
39
* Constructor.
@@ -70,9 +74,25 @@ public function doRun(InputInterface $input, OutputInterface $output)
70
74
71
75
$ this ->setDispatcher ($ this ->kernel ->getContainer ()->get ('event_dispatcher ' ));
72
76
77
+ if ($ this ->registrationErrors ) {
78
+ $ this ->renderRegistrationErrors ($ input , $ output );
79
+ }
80
+
73
81
return parent ::doRun ($ input , $ output );
74
82
}
75
83
84
+ /**
85
+ * {@inheritdoc}
86
+ */
87
+ protected function doRunCommand (Command $ command , InputInterface $ input , OutputInterface $ output )
88
+ {
89
+ if ($ this ->registrationErrors ) {
90
+ $ this ->renderRegistrationErrors ($ input , $ output );
91
+ }
92
+
93
+ return parent ::doRunCommand ($ command , $ input , $ output );
94
+ }
95
+
76
96
/**
77
97
* {@inheritdoc}
78
98
*/
@@ -138,7 +158,13 @@ protected function registerCommands()
138
158
139
159
foreach ($ this ->kernel ->getBundles () as $ bundle ) {
140
160
if ($ bundle instanceof Bundle) {
141
- $ bundle ->registerCommands ($ this );
161
+ try {
162
+ $ bundle ->registerCommands ($ this );
163
+ } catch (\Exception $ e ) {
164
+ $ this ->registrationErrors [] = $ e ;
165
+ } catch (\Throwable $ e ) {
166
+ $ this ->registrationErrors [] = new FatalThrowableError ($ e );
167
+ }
142
168
}
143
169
}
144
170
@@ -149,9 +175,30 @@ protected function registerCommands()
149
175
if ($ container ->hasParameter ('console.command.ids ' )) {
150
176
foreach ($ container ->getParameter ('console.command.ids ' ) as $ id ) {
151
177
if (false !== $ id ) {
152
- $ this ->add ($ container ->get ($ id ));
178
+ try {
179
+ $ this ->add ($ container ->get ($ id ));
180
+ } catch (\Exception $ e ) {
181
+ $ this ->registrationErrors [] = $ e ;
182
+ } catch (\Throwable $ e ) {
183
+ $ this ->registrationErrors [] = new FatalThrowableError ($ e );
184
+ }
153
185
}
154
186
}
155
187
}
156
188
}
189
+
190
+ private function renderRegistrationErrors (InputInterface $ input , OutputInterface $ output )
191
+ {
192
+ if ($ output instanceof ConsoleOutputInterface) {
193
+ $ output = $ output ->getErrorOutput ();
194
+ }
195
+
196
+ (new SymfonyStyle ($ input , $ output ))->warning ('Some commands could not be registered. ' );
197
+
198
+ foreach ($ this ->registrationErrors as $ error ) {
199
+ $ this ->doRenderException ($ error , $ output );
200
+ }
201
+
202
+ $ this ->registrationErrors = array ();
203
+ }
157
204
}
0 commit comments