1212use inhere \console \io \Output ;
1313use inhere \console \traits \InputOutputTrait ;
1414use inhere \console \traits \SimpleEventStaticTrait ;
15+ use inhere \console \utils \Helper ;
1516
1617/**
1718 * Class AbstractApp
@@ -36,12 +37,14 @@ abstract class AbstractApp
3637 * app config
3738 * @var array
3839 */
39- protected $ config = [
40+ private $ config = [
4041 'env ' => 'pdt ' , // dev test pdt
41- 'debug ' => true ,
42+ 'debug ' => false ,
4243 'name ' => 'My Console ' ,
4344 'version ' => '0.5.1 ' ,
4445 'publishAt ' => '2017.03.24 ' ,
46+ 'rootPath ' => '' ,
47+ 'hideRootPath ' => true ,
4548 'charset ' => 'UTF-8 ' ,
4649 'timeZone ' => 'Asia/Shanghai ' ,
4750 ];
@@ -70,6 +73,11 @@ abstract class AbstractApp
7073 */
7174 private $ commandName ;
7275
76+ /**
77+ * @var bool
78+ */
79+ private $ hideRootPath = true ;
80+
7381 /**
7482 * App constructor.
7583 * @param array $config
@@ -78,13 +86,26 @@ abstract class AbstractApp
7886 */
7987 public function __construct (array $ config = [], Input $ input = null , Output $ output = null )
8088 {
89+ $ this ->runtimeCheck ();
90+ $ this ->setConfig ($ config );
91+
8192 $ this ->input = $ input ?: new Input ();
8293 $ this ->output = $ output ?: new Output ();
8394
84- $ this ->setConfig ($ config );
8595 $ this ->init ();
8696 }
8797
98+ protected function runtimeCheck ()
99+ {
100+ // check env
101+ if (!in_array (PHP_SAPI , ['cli ' , 'cli-server ' ], true )) {
102+ header ('HTTP/1.1 403 Forbidden ' );
103+ exit (" 403 Forbidden \n\n"
104+ . " current environment is CLI. \n"
105+ . " :( Sorry! Run this script is only allowed in the terminal environment! \n,You are not allowed to access this file. \n" );
106+ }
107+ }
108+
88109 protected function init ()
89110 {
90111 $ this ->commandName = $ this ->input ->getCommand ();
@@ -267,15 +288,19 @@ protected function dispatchExHandler($e)
267288 // open debug, throw exception
268289 if ($ this ->isDebug ()) {
269290 $ message = sprintf (
270- "<bold >Exception(%d)</bold >: <red>%s</red> \nCalled At %s, Line: <cyan>%d</cyan> \nCatch the exception by: %s \nCode Trace: \n%s \n" ,
271- $ e ->getCode (),
291+ "<red >Exception</red >: %s \nCalled At %s, Line: <cyan>%d</cyan> \nCatch the exception by: %s \nCode Trace: \n%s \n" ,
292+ // $e->getCode(),
272293 $ e ->getMessage (),
273294 $ e ->getFile (),
274295 $ e ->getLine (),
275296 get_class ($ e ),
276297 $ e ->getTraceAsString ()
277298 );
278299
300+ if ($ this ->config ('hideRootPath ' ) && $ rootPath = $ this ->config ('rootPath ' )) {
301+ $ message = str_replace ($ rootPath , '{ROOT} ' , $ message );
302+ }
303+
279304 $ this ->output ->write ($ message , false );
280305 } else {
281306 // simple output
@@ -348,13 +373,14 @@ public function showHelpInfo($quit = true)
348373 public function showVersionInfo ($ quit = true )
349374 {
350375 $ date = date ('Y-m-d ' );
376+ $ name = $ this ->config ('name ' , 'Console Application ' );
351377 $ version = $ this ->config ('version ' , 'Unknown ' );
352- $ publishAt = $ this ->config [ 'publishAt ' ] ;
378+ $ publishAt = $ this ->config ( 'publishAt ' , ' Unknown ' ) ;
353379 $ phpVersion = PHP_VERSION ;
354380 $ os = PHP_OS ;
355381
356382 $ this ->output ->aList ([
357- "Console Application <info> {$ this -> config [ ' name ' ] }</info> Version <comment> $ version</comment>(publish at $ publishAt) " ,
383+ "Console Application <info> {$ name }</info> Version <comment> $ version</comment>(publish at $ publishAt) " ,
358384 'System ' => "PHP version <info> $ phpVersion</info>, on OS <info> $ os</info>, current Date $ date " ,
359385 ], null , [
360386 'leftChar ' => ''
@@ -510,11 +536,9 @@ public function config($name, $default = null)
510536
511537 // allow get $config['top']['sub'] by 'top.sub'
512538 if (strpos ($ name , '. ' ) > 1 ) {
513- list ( $ topKey , $ subKey ) = explode ('. ' , $ name, 2 );
539+ $ nodes = array_filter ( explode ('. ' , $ name) );
514540
515- if (isset ($ this ->config [$ topKey ], $ this ->config [$ topKey ][$ subKey ])) {
516- return $ this ->config [$ topKey ][$ subKey ];
517- }
541+ return Helper::findValueByNodes ($ this ->config , $ nodes , $ default );
518542 }
519543
520544 return $ this ->config [$ name ] ?? $ default ;
0 commit comments