99namespace Inhere \Console \Base ;
1010
1111use Inhere \Console \IO \Input ;
12+ use Inhere \Console \IO \InputInterface ;
1213use Inhere \Console \IO \Output ;
1314use Inhere \Console \Components \Style \Highlighter ;
15+ use Inhere \Console \IO \OutputInterface ;
1416use Inhere \Console \Traits \InputOutputAwareTrait ;
1517use Inhere \Console \Traits \SimpleEventTrait ;
1618use Inhere \Console \Components \Style \Style ;
@@ -111,7 +113,8 @@ protected function init()
111113 ];
112114
113115 $ this ->commandName = $ this ->input ->getCommand ();
114- set_exception_handler ([$ this , 'handleException ' ]);
116+
117+ $ this ->registerErrorHandle ();
115118 }
116119
117120 /**
@@ -214,6 +217,21 @@ public function stop($code = 0)
214217 exit ((int )$ code );
215218 }
216219
220+ /**
221+ * @param string $command
222+ * @param InputInterface $input
223+ * @param OutputInterface $output
224+ * @return int|mixed
225+ */
226+ public function subRun (string $ command , InputInterface $ input , OutputInterface $ output )
227+ {
228+ $ app = clone $ this ;
229+ $ app ->setInput ($ input );
230+ $ app ->setOutput ($ output );
231+
232+ return $ app ->dispatch ($ command );
233+ }
234+
217235 /**********************************************************
218236 * helper method for the application
219237 **********************************************************/
@@ -232,43 +250,67 @@ protected function runtimeCheck()
232250 }
233251 }
234252
253+ /**
254+ * register error handle
255+ */
256+ protected function registerErrorHandle ()
257+ {
258+ set_error_handler ([$ this , 'handleError ' ]);
259+ set_exception_handler ([$ this , 'handleException ' ]);
260+
261+ register_shutdown_function (function () {
262+ if ($ e = error_get_last ()) {
263+ $ this ->handleError ($ e ['type ' ], $ e ['message ' ], $ e ['file ' ], $ e ['line ' ]);
264+ }
265+ });
266+ }
267+
268+ /**
269+ * 运行异常处理
270+ * @param int $num
271+ * @param string $str
272+ * @param string $file
273+ * @param int $line
274+ */
275+ public function handleError (int $ num , string $ str , string $ file , int $ line )
276+ {
277+ $ this ->handleException (new \ErrorException ($ str , 0 , $ num , $ file , $ line ));
278+ $ this ->stop (-1 );
279+ }
280+
235281 /**
236282 * 运行异常处理
237283 * @param \Exception|\Throwable $e
238284 */
239285 public function handleException ($ e )
240286 {
241287 $ class = \get_class ($ e );
242- $ type = $ e instanceof \Error ? 'Error ' : 'Exception ' ;
243- $ title = ":( OO ... An $ type Occurred! " ;
244288 $ this ->logError ($ e );
245289
246290 // open debug, throw exception
247291 if ($ this ->isDebug ()) {
248292 $ tpl = <<<ERR
249- <danger> $ title </danger >
293+ \n <error> Error </error> <mga>%s</mga >
250294
251- Message <magenta>%s</magenta>
252- At File <cyan>%s</cyan> line <cyan>%d</cyan>
295+ At File <cyan>%s</cyan> line <bold>%d</bold>
253296Exception $ class
254- Catch by %s() \n
255- Code Trace: \n%s \n
297+ <comment>Code View:</comment> \n\n %s
298+ <comment> Code Trace:</comment> \n \n%s \n
256299ERR ;
300+ $ line = $ e ->getLine ();
301+ $ file = $ e ->getFile ();
302+ $ snippet = Highlighter::create ()->highlightSnippet (file_get_contents ($ file ), $ line , 3 , 3 );
257303 $ message = sprintf (
258304 $ tpl ,
259305 // $e->getCode(),
260306 $ e ->getMessage (),
261- $ file = $ e ->getFile (),
262- $ line = $ e ->getLine (),
263- __METHOD__ ,
264- $ e ->getTraceAsString ()
307+ $ file ,
308+ $ line ,
309+ // __METHOD__,
310+ $ snippet ,
311+ str_replace ('): ' , "): \n - " , $ e ->getTraceAsString ())
265312 );
266313
267- $ source = file_get_contents ($ file );
268- $ hl = Highlighter::create ();
269- $ snippet = $ hl ->highlightSnippet ($ source , $ line , 3 , 3 );
270- $ message .= "\nCode View: \n$ snippet " ;
271-
272314 if ($ this ->meta ['hideRootPath ' ] && ($ rootPath = $ this ->meta ['rootPath ' ])) {
273315 $ message = str_replace ($ rootPath , '{ROOT} ' , $ message );
274316 }
@@ -539,6 +581,19 @@ protected function getRealCommandName(string $name): string
539581 return $ this ->commandAliases [$ name ] ?? $ name ;
540582 }
541583
584+ /**
585+ * @param string $name
586+ * @return mixed|null
587+ */
588+ public function findCommand (string $ name )
589+ {
590+ if (isset ($ this ->commands [$ name ])) {
591+ return $ this ->commands [$ name ];
592+ }
593+
594+ return $ this ->controllers [$ name ] ?? null ;
595+ }
596+
542597 /**********************************************************
543598 * getter/setter methods
544599 **********************************************************/
0 commit comments