11<?php
2+ /**
3+ * The file contains Console class
4+ *
5+ * PHP version 5
6+ *
7+ * @category Library
8+ * @package Cherry
9+ * @author Temuri Takalandze <[email protected] > 10+ * @license https://github.com/cherry-framework/console/blob/master/LICENSE MIT
11+ * @link https://github.com/cherry-framework/console
12+ */
213
314namespace Cherry \Console ;
415
16+ use Cherry \Console \Input \ArgvInput ;
17+ use Cherry \Console \Output \Output ;
18+
19+ /**
20+ * CLI Console for Cherry-project
21+ *
22+ * @category Library
23+ * @package Cherry
24+ * @author Temuri Takalandze <[email protected] > 25+ * @license https://github.com/cherry-framework/console/blob/master/LICENSE MIT
26+ * @link https://github.com/cherry-framework/console
27+ */
528class Console
629{
730 private $ _argvInput ;
31+ private $ _output ;
32+
833 private $ _argv ;
34+ private $ _argvParsed ;
35+
36+ /**
37+ * Console constructor.
38+ */
39+ public function __construct ()
40+ {
41+ $ this ->_argvInput = new ArgvInput ();
42+ $ this ->_output = new Output ();
43+
44+ $ this ->_argv = $ this ->_argvInput ->getArgv ();
45+ $ this ->_argvParsed = $ this ->_argvInput ->getArgvParsed ();
46+
47+ $ this ->_run ();
48+ }
49+
50+ /**
51+ * Run the Console.
52+ *
53+ * @return void
54+ */
55+ private function _run ()
56+ {
57+ $ argvInput = $ this ->_argvInput ;
58+
59+ $ argv = $ this ->_argv ;
60+
61+ if ($ argvInput ->getArgvCount () == 0
62+ || $ argvInput ->getBoolean ('help ' )
63+ || $ argvInput ->getBoolean ('h ' )
64+ ) {
65+ $ this ->_printHelp ();
66+ return ;
67+ }
68+
69+ //Call action
70+ $ this ->_call ($ argv [0 ]);
71+ }
972
10- public function __construct (ArgvInput $ argvInput )
73+ /**
74+ * Call method of this class.
75+ *
76+ * @param string $method Method name
77+ *
78+ * @return void
79+ */
80+ private function _call ($ method )
1181 {
12- $ this ->_argvInput = $ argvInput ;
13- $ this ->_argv = $ argvInput ->getArgv ();
82+ $ method = "_ {$ method }" ;
83+
84+ if (method_exists ($ this , $ method )) {
85+ $ this ->{$ method }();
86+ } else {
87+ $ this ->_printHelp ();
88+ }
89+ }
90+
91+ /**
92+ * Print console help message.
93+ *
94+ * @return void
95+ */
96+ private function _printHelp ()
97+ {
98+ $ hello = <<<EOF
99+ Welcome to
100+ ____ _
101+ / ___| |__ ___ _ __ _ __ _ _
102+ | | | '_ \ / _ \ '__| '__| | | |
103+ | |___| | | | __/ | | | | |_| |
104+ \____|_| |_|\___|_| |_| \__, |
105+ |___/
106+
107+ Console
108+ ----------------------------------
109+
110+ help, --help, -h - Show help.
111+
112+ server [option] [arguments] - Start PHP Development server.
113+ [option] - Server start options
114+ run - Start server on 127.0.0.1:8000
115+ start - Start server on given address:port
116+ [argument] - Additional arguments for option "start" address:port (127.0.0.1:8000)
117+
118+ EOF ;
119+
120+ print $ this ->_output
121+ ->text ($ hello );
122+ }
123+
124+ /**
125+ * Run PHP Development server from CLI
126+ *
127+ * @return void
128+ */
129+ private function _server ()
130+ {
131+ $ argv = $ this ->_argv ;
132+
133+ $ server = null ;
134+
135+ if (isset ($ argv [1 ]) && $ argv [1 ] == 'run ' ) {
136+ $ server = "127.0.0.1:8000 " ;
137+ } else if (isset ($ argv [1 ]) && $ argv [1 ] == 'start ' ) {
138+ if (isset ($ argv [2 ])) {
139+ $ server = $ argv [2 ];
140+ } else {
141+ $ this ->_printHelp ();
142+ return ;
143+ }
144+ } else {
145+ $ this ->_printHelp ();
146+ return ;
147+ }
148+
149+ $ info = <<<EOF
150+
151+ {$ this ->_output ->success ("Started Cherry Server on http:// {$ server }" )}
152+ // Quit the server with Ctrl + C.
153+ EOF ;
154+
155+ print $ this ->_output
156+ ->text ($ info );
14157
15- var_dump ( $ this -> _argv );
158+ echo exec ( " php -S { $ server } -t " . dirname ( dirname ( __DIR__ )) );
16159 }
17160}
0 commit comments