3636use Symfony \Component \Console \Question \Question ;
3737use Symfony \Component \Console \Style \SymfonyStyle ;
3838use Symfony \Component \Finder \Finder ;
39+ use Throwable ;
3940
4041use function basename ;
4142use function dirname ;
@@ -78,6 +79,8 @@ public function getSetup(): Setup
7879
7980 protected function execute (InputInterface $ input , OutputInterface $ output ): int
8081 {
82+ $ output ->setVerbosity ($ this ->getSetup ()->getVerbosity ());
83+
8184 $ console = $ this ->styleFactory ->factory ($ input , $ output );
8285 $ console ->title ('Welcome to the PHP Library Starter Kit! ' );
8386 $ console ->block (
@@ -89,12 +92,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8992
9093 $ this ->registerInterruptHandler ($ console );
9194
92- if (!$ this ->confirmStart ($ console )) {
93- return 0 ;
94- }
95+ try {
96+ if (!$ this ->confirmStart ($ console )) {
97+ return 0 ;
98+ }
9599
96- $ this ->askQuestions ($ console );
97- $ this ->setup ->run ($ console , $ this ->answers );
100+ $ this ->askQuestions ($ console );
101+ $ this ->setup ->run ($ console , $ this ->answers );
102+ } catch (Throwable $ throwable ) {
103+ return $ this ->handleException ($ throwable , $ console );
104+ }
98105
99106 $ console ->success ([
100107 sprintf ('Congratulations! Your project, %s, is ready! ' , (string ) $ this ->answers ->packageName ),
@@ -173,6 +180,30 @@ private function registerInterruptHandler(SymfonyStyle $console): void
173180 // phpcs:enable
174181 }
175182
183+ private function handleException (Throwable $ throwable , SymfonyStyle $ console ): int
184+ {
185+ $ errorMessages = [
186+ $ throwable ->getMessage (),
187+ sprintf ('At line %d in %s ' , $ throwable ->getLine (), $ throwable ->getFile ()),
188+ ];
189+
190+ if ($ console ->getVerbosity () === OutputInterface::VERBOSITY_DEBUG ) {
191+ $ errorMessages [] = $ throwable ->getTraceAsString ();
192+ }
193+
194+ $ console ->error ($ errorMessages );
195+
196+ $ console ->block ([
197+ 'Oops! I encountered an error. ' ,
198+ 'Please go here and click the "New issue" button to report this error: '
199+ . 'https://github.com/ramsey/php-library-starter-kit/issues ' ,
200+ ]);
201+
202+ $ console ->newLine ();
203+
204+ return (int ) $ throwable ->getCode () ?: 1 ;
205+ }
206+
176207 public static function newApplication (): Application
177208 {
178209 return self ::$ application ?? new Application ();
@@ -186,7 +217,7 @@ public static function start(Event $event): void
186217 $ projectName = (string ) preg_replace ('/[^a-z0-9]/ ' , '- ' , $ projectName );
187218
188219 $ project = new Project ($ projectName , $ appPath );
189- $ setup = new Setup ($ project , $ event , new Filesystem (), new Finder ());
220+ $ setup = new Setup ($ project , $ event , new Filesystem (), new Finder (), self :: determineVerbosityLevel ( $ event ) );
190221
191222 $ command = new self ($ setup );
192223
@@ -196,4 +227,17 @@ public static function start(Event $event): void
196227
197228 $ application ->run (new StringInput ('starter-kit ' ));
198229 }
230+
231+ public static function determineVerbosityLevel (Event $ event ): int
232+ {
233+ if ($ event ->getIO ()->isDebug ()) {
234+ return OutputInterface::VERBOSITY_DEBUG ;
235+ } elseif ($ event ->getIO ()->isVeryVerbose ()) {
236+ return OutputInterface::VERBOSITY_VERY_VERBOSE ;
237+ } elseif ($ event ->getIO ()->isVerbose ()) {
238+ return OutputInterface::VERBOSITY_VERBOSE ;
239+ }
240+
241+ return OutputInterface::VERBOSITY_NORMAL ;
242+ }
199243}
0 commit comments