88
99namespace Inhere \Console ;
1010
11+ use ErrorException ;
1112use Inhere \Console \Component \ErrorHandler ;
1213use Inhere \Console \Component \Style \Style ;
1314use Inhere \Console \Contract \ApplicationInterface ;
1920use Inhere \Console \Traits \ApplicationHelpTrait ;
2021use Inhere \Console \Traits \InputOutputAwareTrait ;
2122use Inhere \Console \Traits \SimpleEventTrait ;
23+ use InvalidArgumentException ;
24+ use Throwable ;
2225use Toolkit \PhpUtil \PhpHelper ;
26+ use function array_keys ;
27+ use function array_merge ;
28+ use function error_get_last ;
29+ use function header ;
30+ use function in_array ;
31+ use function is_int ;
32+ use function memory_get_usage ;
33+ use function microtime ;
34+ use function register_shutdown_function ;
35+ use function set_error_handler ;
36+ use function set_exception_handler ;
37+ use function trim ;
38+ use const PHP_SAPI ;
2339
2440/**
2541 * Class AbstractApplication
@@ -94,7 +110,7 @@ abstract class AbstractApplication implements ApplicationInterface
94110 * @param array $config
95111 * @param Input $input
96112 * @param Output $output
97- * @throws \ InvalidArgumentException
113+ * @throws InvalidArgumentException
98114 */
99115 public function __construct (array $ config = [], Input $ input = null , Output $ output = null )
100116 {
@@ -109,14 +125,14 @@ public function __construct(array $config = [], Input $input = null, Output $out
109125 }
110126
111127 /**
112- * @throws \ InvalidArgumentException
128+ * @throws InvalidArgumentException
113129 */
114130 protected function init (): void
115131 {
116132 $ this ->stats = [
117- 'startTime ' => \ microtime (1 ),
133+ 'startTime ' => microtime (1 ),
118134 'endTime ' => 0 ,
119- 'startMemory ' => \ memory_get_usage (),
135+ 'startMemory ' => memory_get_usage (),
120136 'endMemory ' => 0 ,
121137 ];
122138
@@ -141,7 +157,7 @@ public static function getGlobalOptions(): array
141157 public function addGlobalOptions (array $ options ): void
142158 {
143159 if ($ options ) {
144- self ::$ globalOptions = \ array_merge (self ::$ globalOptions , $ options );
160+ self ::$ globalOptions = array_merge (self ::$ globalOptions , $ options );
145161 }
146162 }
147163
@@ -167,11 +183,11 @@ protected function beforeRun(): void
167183 * run application
168184 * @param bool $exit
169185 * @return int|mixed
170- * @throws \ InvalidArgumentException
186+ * @throws InvalidArgumentException
171187 */
172188 public function run (bool $ exit = true )
173189 {
174- $ command = \ trim ($ this ->input ->getCommand (), $ this ->delimiter );
190+ $ command = trim ($ this ->input ->getCommand (), $ this ->delimiter );
175191
176192 $ this ->prepareRun ();
177193
@@ -187,20 +203,20 @@ public function run(bool $exit = true)
187203 // do run ...
188204 try {
189205 $ result = $ this ->dispatch ($ command );
190- } catch (\ Throwable $ e ) {
206+ } catch (Throwable $ e ) {
191207 $ this ->fire (self ::ON_RUN_ERROR , $ e , $ this );
192208 $ result = $ e ->getCode () === 0 ? $ e ->getLine () : $ e ->getCode ();
193209 $ this ->handleException ($ e );
194210 }
195211
196- $ this ->stats ['endTime ' ] = \ microtime (1 );
212+ $ this ->stats ['endTime ' ] = microtime (1 );
197213
198214 // call 'onAfterRun' service, if it is registered.
199215 $ this ->fire (self ::ON_AFTER_RUN , $ this );
200216 $ this ->afterRun ();
201217
202218 if ($ exit ) {
203- $ this ->stop (\ is_int ($ result ) ? $ result : 0 );
219+ $ this ->stop (is_int ($ result ) ? $ result : 0 );
204220 }
205221
206222 return $ result ;
@@ -256,8 +272,8 @@ public function subRun(string $command, InputInterface $input, OutputInterface $
256272 protected function runtimeCheck (): void
257273 {
258274 // check env
259- if (!\ in_array (\ PHP_SAPI , ['cli ' , 'phpdbg ' , 'cli-server ' ], true )) {
260- \ header ('HTTP/1.1 403 Forbidden ' );
275+ if (!in_array (PHP_SAPI , ['cli ' , 'phpdbg ' , 'cli-server ' ], true )) {
276+ header ('HTTP/1.1 403 Forbidden ' );
261277 exit (" 403 Forbidden \n\n"
262278 . " current environment is CLI. \n"
263279 . " :( Sorry! Run this script is only allowed in the terminal environment! \n,You are not allowed to access this file. \n" );
@@ -266,14 +282,14 @@ protected function runtimeCheck(): void
266282
267283 /**
268284 * register error handle
269- * @throws \ InvalidArgumentException
285+ * @throws InvalidArgumentException
270286 */
271287 protected function registerErrorHandle (): void
272288 {
273- \ set_error_handler ([$ this , 'handleError ' ]);
274- \ set_exception_handler ([$ this , 'handleException ' ]);
275- \ register_shutdown_function (function () {
276- if ($ e = \ error_get_last ()) {
289+ set_error_handler ([$ this , 'handleError ' ]);
290+ set_exception_handler ([$ this , 'handleException ' ]);
291+ register_shutdown_function (function () {
292+ if ($ e = error_get_last ()) {
277293 $ this ->handleError ($ e ['type ' ], $ e ['message ' ], $ e ['file ' ], $ e ['line ' ]);
278294 }
279295 });
@@ -285,18 +301,18 @@ protected function registerErrorHandle(): void
285301 * @param string $str
286302 * @param string $file
287303 * @param int $line
288- * @throws \ InvalidArgumentException
304+ * @throws InvalidArgumentException
289305 */
290306 public function handleError (int $ num , string $ str , string $ file , int $ line ): void
291307 {
292- $ this ->handleException (new \ ErrorException ($ str , 0 , $ num , $ file , $ line ));
308+ $ this ->handleException (new ErrorException ($ str , 0 , $ num , $ file , $ line ));
293309 $ this ->stop (-1 );
294310 }
295311
296312 /**
297313 * Running exception handling
298- * @param \ Throwable $e
299- * @throws \ InvalidArgumentException
314+ * @param Throwable $e
315+ * @throws InvalidArgumentException
300316 */
301317 public function handleException ($ e ): void
302318 {
@@ -428,7 +444,7 @@ public function getRootPath(): string
428444 */
429445 public function getInternalCommands (): array
430446 {
431- return \ array_keys (static ::$ internalCommands );
447+ return array_keys (static ::$ internalCommands );
432448 }
433449
434450 /**
@@ -470,7 +486,7 @@ public function getRouter(): Router
470486 public function setConfig (array $ config ): void
471487 {
472488 if ($ config ) {
473- $ this ->config = \ array_merge ($ this ->config , $ config );
489+ $ this ->config = array_merge ($ this ->config , $ config );
474490 }
475491 }
476492
0 commit comments