1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: inhere
5+ * Date: 2017-08-15
6+ * Time: 10:51
7+ */
8+
9+ namespace inhere \console ;
10+
11+ /**
12+ * Class LiteApp
13+ */
14+ class LiteApp
15+ {
16+
17+ ///////////////////////////////////////////////////////////////////
18+ /// simple cli support
19+ ///////////////////////////////////////////////////////////////////
20+
21+ private $ args = [];
22+ private $ opts = [];
23+ private $ script = '' ;
24+ private $ command = '' ;
25+
26+ private $ commands = [];
27+ private $ messages = [];
28+
29+ /**
30+ * @param bool $exit
31+ */
32+ public function dispatchCli ($ exit = true )
33+ {
34+ $ this ->parseCliArgv ();
35+
36+ if (isset ($ this ->args [0 ])) {
37+ $ this ->command = $ this ->args [0 ];
38+ unset($ this ->args [0 ]);
39+ }
40+
41+ if (!$ command = $ this ->command ) {
42+ $ this ->showCommands ();
43+ }
44+
45+ $ status = 0 ;
46+
47+ try {
48+ if (isset ($ this ->commands [$ command ])) {
49+ $ status = $ this ->runHandler ($ command , $ this ->commands [$ command ]);
50+ } else {
51+ $ this ->showCommands ("The command {$ command } not exists! " );
52+ }
53+ } catch (\Throwable $ e ) {
54+ $ text = sprintf (
55+ "Exception(%d): %s \nFile: %s(Line %d) \nTrace: \n%s \n" ,
56+ $ e ->getCode (),
57+ $ e ->getMessage (),
58+ $ e ->getFile (),
59+ $ e ->getLine (),
60+ $ e ->getTraceAsString ()
61+ );
62+ exit ($ text );
63+ }
64+
65+ if ($ exit ) {
66+ exit ((int )$ status );
67+ }
68+ }
69+
70+ /**
71+ * @param $command
72+ * @param $handler
73+ * @return mixed
74+ */
75+ public function runHandler ($ command , $ handler )
76+ {
77+ if (is_string ($ handler )) {
78+ // function name
79+ if (function_exists ($ handler )) {
80+ return $ handler ($ this );
81+ }
82+
83+ if (class_exists ($ handler )) {
84+ $ handler = new $ handler ;
85+
86+ // $handler->execute()
87+ if (method_exists ($ handler , 'execute ' )) {
88+ return $ handler ->execute ($ this );
89+ }
90+ }
91+ }
92+
93+ // a \Closure OR $handler->__invoke()
94+ if (method_exists ($ handler , '__invoke ' )) {
95+ return $ handler ($ this );
96+ }
97+
98+ throw new \InvalidArgumentException ("Invalid handler of the command: $ command " );
99+ }
100+
101+ /**
102+ * parseCliArgv
103+ */
104+ public function parseCliArgv ()
105+ {
106+ /** @var array $argv */
107+ $ argv = $ _SERVER ['argv ' ];
108+ $ this ->script = array_shift ($ argv );
109+
110+ foreach ($ argv as $ key => $ value ) {
111+ // opts
112+ if (strpos ($ value , '- ' ) === 0 ) {
113+ $ value = trim ($ value , '- ' );
114+
115+ if (!$ value ) {
116+ continue ;
117+ }
118+
119+ if (strpos ($ value , '= ' )) {
120+ list ($ n , $ v ) = explode ('= ' , $ value );
121+ $ this ->opts [$ n ] = $ v ;
122+ } else {
123+ $ this ->opts [$ value ] = true ;
124+ }
125+ } else {
126+ if (strpos ($ value , '= ' )) {
127+ list ($ n , $ v ) = explode ('= ' , $ value );
128+ $ this ->args [$ n ] = $ v ;
129+ } else {
130+ $ this ->args [] = $ value ;
131+ }
132+ }
133+ }
134+ }
135+
136+ /**
137+ * @param string $command
138+ * @param string|\Closure $handler
139+ * @param string $desc
140+ */
141+ public function addCommand ($ command , $ handler , $ desc = '' )
142+ {
143+ if (!$ command || !$ handler ) {
144+ throw new \InvalidArgumentException ('Invalid arguments ' );
145+ }
146+
147+ $ this ->commands [$ command ] = $ handler ;
148+ $ this ->messages [$ command ] = trim ($ desc );
149+ }
150+
151+ /**
152+ * @param string $err
153+ */
154+ public function showCommands ($ err = '' )
155+ {
156+ if ($ err ) {
157+ echo "ERROR: $ err \n\n" ;
158+ }
159+
160+ $ help = "Available Commands: \n" ;
161+
162+ foreach ($ this ->messages as $ command => $ desc ) {
163+ $ command = str_pad ($ command , 18 , ' ' );
164+ $ desc = $ desc ?: 'No description for the command ' ;
165+ $ help .= " $ command $ desc \n" ;
166+ }
167+
168+ echo $ help . PHP_EOL ;
169+ exit (0 );
170+ }
171+
172+ }
0 commit comments