1111
1212namespace Symfony \Bundle \FrameworkBundle \Console ;
1313
14+ use Symfony \Component \Console \Output \ConsoleOutputInterface ;
15+ use Symfony \Component \Console \Style \SymfonyStyle ;
16+ use Symfony \Component \Debug \Exception \FatalThrowableError ;
1417use Symfony \Component \DependencyInjection \ContainerAwareInterface ;
1518use Symfony \Component \Console \Application as BaseApplication ;
1619use Symfony \Component \Console \Command \Command ;
@@ -30,6 +33,7 @@ class Application extends BaseApplication
3033{
3134 private $ kernel ;
3235 private $ commandsRegistered = false ;
36+ private $ registrationErrors = array ();
3337
3438 /**
3539 * Constructor.
@@ -70,9 +74,25 @@ public function doRun(InputInterface $input, OutputInterface $output)
7074
7175 $ this ->setDispatcher ($ this ->kernel ->getContainer ()->get ('event_dispatcher ' ));
7276
77+ if ($ this ->registrationErrors ) {
78+ $ this ->renderRegistrationErrors ($ input , $ output );
79+ }
80+
7381 return parent ::doRun ($ input , $ output );
7482 }
7583
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+
7696 /**
7797 * {@inheritdoc}
7898 */
@@ -138,7 +158,13 @@ protected function registerCommands()
138158
139159 foreach ($ this ->kernel ->getBundles () as $ bundle ) {
140160 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+ }
142168 }
143169 }
144170
@@ -149,9 +175,30 @@ protected function registerCommands()
149175 if ($ container ->hasParameter ('console.command.ids ' )) {
150176 foreach ($ container ->getParameter ('console.command.ids ' ) as $ id ) {
151177 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+ }
153185 }
154186 }
155187 }
156188 }
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+ }
157204}
0 commit comments