41
41
class App
42
42
{
43
43
/** @var self */
44
- public static $ i ;
44
+ public static $ global ;
45
45
46
46
private const COMMAND_CONFIG = [
47
47
'desc ' => '' ,
@@ -55,7 +55,7 @@ class App
55
55
/**
56
56
* @var array
57
57
*/
58
- private $ metas = [
58
+ protected $ params = [
59
59
'name ' => 'My application ' ,
60
60
'desc ' => 'My command line application ' ,
61
61
'version ' => '0.2.1 '
@@ -96,6 +96,18 @@ class App
96
96
*/
97
97
private $ keyWidth = 12 ;
98
98
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
+
99
111
/**
100
112
* Class constructor.
101
113
*
@@ -105,15 +117,17 @@ class App
105
117
public function __construct (array $ config = [], array $ argv = null )
106
118
{
107
119
// save self
108
- self ::$ i = $ this ;
120
+ if (!self ::$ global ) {
121
+ self ::$ global = $ this ;
122
+ }
109
123
110
124
// get current dir
111
125
$ this ->pwd = (string )getcwd ();
112
126
113
127
// parse cli argv
114
128
$ argv = $ argv ?? $ _SERVER ['argv ' ];
115
129
if ($ config ) {
116
- $ this ->setMetas ($ config );
130
+ $ this ->setParams ($ config );
117
131
}
118
132
119
133
// get script file
@@ -325,6 +339,11 @@ public function addCommand(string $command, callable $handler, $config = null):
325
339
326
340
$ this ->commands [$ command ] = $ handler ;
327
341
342
+ // no config
343
+ if (!$ config ) {
344
+ return ;
345
+ }
346
+
328
347
if (is_string ($ config )) {
329
348
$ desc = trim ($ config );
330
349
$ config = self ::COMMAND_CONFIG ;
@@ -344,18 +363,21 @@ public function addCommand(string $command, callable $handler, $config = null):
344
363
*
345
364
* @throws InvalidArgumentException
346
365
*/
347
- public function commands (array $ commands ): void
366
+ public function addCommands (array $ commands ): void
348
367
{
349
368
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 ;
351
375
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 ' ]);
356
378
}
357
379
358
- $ this ->addCommand ($ command , $ handler , $ desc );
380
+ $ this ->addCommand ($ name , $ handler , $ conf );
359
381
}
360
382
}
361
383
@@ -373,8 +395,8 @@ public function displayHelp(string $err = ''): void
373
395
}
374
396
375
397
// 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 ' ]) {
378
400
$ desc .= "(<red>v $ ver</red>) " ;
379
401
}
380
402
@@ -386,9 +408,10 @@ public function displayHelp(string $err = ''): void
386
408
ksort ($ data );
387
409
388
410
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" ;
392
415
}
393
416
394
417
$ help .= "\nFor command usage please run: <cyan> $ script COMMAND -h</cyan> " ;
@@ -642,17 +665,56 @@ public function getPwd(): string
642
665
643
666
/**
644
667
* @return array
668
+ * @deprecated please use getParams();
645
669
*/
646
670
public function getMetas (): array
647
671
{
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 ;
649
711
}
650
712
651
713
/**
652
- * @param array $metas
714
+ * @param array $params
653
715
*/
654
- public function setMetas (array $ metas ): void
716
+ public function setParams (array $ params ): void
655
717
{
656
- $ this ->metas = array_merge ($ this ->metas , $ metas );
718
+ $ this ->params = array_merge ($ this ->params , $ params );
657
719
}
658
720
}
0 commit comments