4141class App
4242{
4343 /** @var self */
44- public static $ i ;
44+ public static $ global ;
4545
4646 private const COMMAND_CONFIG = [
4747 'desc ' => '' ,
@@ -55,7 +55,7 @@ class App
5555 /**
5656 * @var array
5757 */
58- private $ metas = [
58+ protected $ params = [
5959 'name ' => 'My application ' ,
6060 'desc ' => 'My command line application ' ,
6161 'version ' => '0.2.1 '
@@ -96,6 +96,18 @@ class App
9696 */
9797 private $ keyWidth = 12 ;
9898
99+ /**
100+ * @return static
101+ */
102+ public static function global (): self
103+ {
104+ if (!self ::$ global ) {
105+ throw new RuntimeException ('please create global app by new App() ' );
106+ }
107+
108+ return self ::$ global ;
109+ }
110+
99111 /**
100112 * Class constructor.
101113 *
@@ -105,15 +117,17 @@ class App
105117 public function __construct (array $ config = [], array $ argv = null )
106118 {
107119 // save self
108- self ::$ i = $ this ;
120+ if (!self ::$ global ) {
121+ self ::$ global = $ this ;
122+ }
109123
110124 // get current dir
111125 $ this ->pwd = (string )getcwd ();
112126
113127 // parse cli argv
114128 $ argv = $ argv ?? $ _SERVER ['argv ' ];
115129 if ($ config ) {
116- $ this ->setMetas ($ config );
130+ $ this ->setParams ($ config );
117131 }
118132
119133 // get script file
@@ -325,6 +339,11 @@ public function addCommand(string $command, callable $handler, $config = null):
325339
326340 $ this ->commands [$ command ] = $ handler ;
327341
342+ // no config
343+ if (!$ config ) {
344+ return ;
345+ }
346+
328347 if (is_string ($ config )) {
329348 $ desc = trim ($ config );
330349 $ config = self ::COMMAND_CONFIG ;
@@ -344,18 +363,21 @@ public function addCommand(string $command, callable $handler, $config = null):
344363 *
345364 * @throws InvalidArgumentException
346365 */
347- public function commands (array $ commands ): void
366+ public function addCommands (array $ commands ): void
348367 {
349368 foreach ($ commands as $ command => $ handler ) {
350- $ desc = '' ;
369+ $ conf = [];
370+ $ name = is_string ($ command ) ? $ command : '' ;
371+
372+ if (is_array ($ handler ) && isset ($ handler ['handler ' ])) {
373+ $ conf = $ handler ;
374+ $ name = $ conf ['name ' ] ?? $ name ;
351375
352- if (is_array ($ handler )) {
353- $ conf = array_values ($ handler );
354- $ handler = $ conf [0 ];
355- $ desc = $ conf [1 ] ?? '' ;
376+ $ handler = $ conf ['handler ' ];
377+ unset($ conf ['name ' ], $ conf ['handler ' ]);
356378 }
357379
358- $ this ->addCommand ($ command , $ handler , $ desc );
380+ $ this ->addCommand ($ name , $ handler , $ conf );
359381 }
360382 }
361383
@@ -373,8 +395,8 @@ public function displayHelp(string $err = ''): void
373395 }
374396
375397 // help
376- $ desc = ucfirst ($ this ->metas ['desc ' ]);
377- if ($ ver = $ this ->metas ['version ' ]) {
398+ $ desc = ucfirst ($ this ->params ['desc ' ]);
399+ if ($ ver = $ this ->params ['version ' ]) {
378400 $ desc .= "(<red>v $ ver</red>) " ;
379401 }
380402
@@ -386,9 +408,10 @@ public function displayHelp(string $err = ''): void
386408 ksort ($ data );
387409
388410 foreach ($ data as $ command => $ item ) {
389- $ command = str_pad ($ command , $ this ->keyWidth , ' ' );
390- $ desc = $ item ['desc ' ] ? ucfirst ($ item ['desc ' ]) : 'No description for the command ' ;
391- $ help .= " <green> $ command</green> $ desc \n" ;
411+ $ command = str_pad ($ command , $ this ->keyWidth );
412+
413+ $ desc = $ item ['desc ' ] ? ucfirst ($ item ['desc ' ]) : 'No description for the command ' ;
414+ $ help .= " <green> $ command</green> $ desc \n" ;
392415 }
393416
394417 $ help .= "\nFor command usage please run: <cyan> $ script COMMAND -h</cyan> " ;
@@ -642,17 +665,56 @@ public function getPwd(): string
642665
643666 /**
644667 * @return array
668+ * @deprecated please use getParams();
645669 */
646670 public function getMetas (): array
647671 {
648- return $ this ->metas ;
672+ return $ this ->getParams ();
673+ }
674+
675+ /**
676+ * @param array $params
677+ *
678+ * @deprecated please use setParams()
679+ */
680+ public function setMetas (array $ params ): void
681+ {
682+ $ this ->setParams ($ params );
683+ }
684+
685+ /**
686+ * @param string $key
687+ * @param null $default
688+ *
689+ * @return mixed|string|null
690+ */
691+ public function getParam (string $ key , $ default = null )
692+ {
693+ return $ this ->params [$ key ] ?? $ default ;
694+ }
695+
696+ /**
697+ * @param string $key
698+ * @param mixed $val
699+ */
700+ public function setParam (string $ key , $ val ): void
701+ {
702+ $ this ->params [$ key ] = $ val ;
703+ }
704+
705+ /**
706+ * @return array
707+ */
708+ public function getParams (): array
709+ {
710+ return $ this ->params ;
649711 }
650712
651713 /**
652- * @param array $metas
714+ * @param array $params
653715 */
654- public function setMetas (array $ metas ): void
716+ public function setParams (array $ params ): void
655717 {
656- $ this ->metas = array_merge ($ this ->metas , $ metas );
718+ $ this ->params = array_merge ($ this ->params , $ params );
657719 }
658720}
0 commit comments