77class boot
88{
99 protected $ currentCommand = null ;
10+ protected $ commands = [];
1011 function __construct ($ commads )
1112 {
1213 if (php_sapi_name () === 'cli ' ) {
13- $ this ->currentCommand = $ this ->register ($ commads );
14+ $ this ->currentCommand = $ commads ;
15+ $ this ->commands = $ this ->register ($ commads );
1416 $ this ->run ();
1517 } else {
1618 echo 'This script can only be run from the command line ' ;
1719 }
1820 }
1921 function register ($ commads )
2022 {
21- if ($ commads [1 ] == 'serve ' or $ commads [1 ] == 'start ' and !isset ($ commads [2 ])) {
23+ if (( $ commads [1 ] == 'serve ' or $ commads [1 ] == 'start ' ) and !isset ($ commads [2 ])) {
2224 echo CliColor::color ("Dev Server Started Successfully. \n" , 'b_black,f_green ' );
2325 system ('php -S localhost:4000 -t public ' );
2426 } elseif ($ commads [1 ] == 'serve ' or $ commads [1 ] == 'start ' and isset ($ commads [2 ])) {
27+ echo CliColor::color ("Dev Server Started Successfully. \n" , 'b_black,f_green ' );
2528 system ("php -S localhost: {$ commads [2 ]} -t public " );
2629 }
2730 // register command
28- $ shortOptions = "c:h " ;
29- $ longOptions = ['controller: ' , 'help ' ];
31+ $ shortOptions = "c:h::m: " ;
32+ $ longOptions = ['controller: ' , 'help ' , ' middleware ' ];
3033 return getopt ($ shortOptions , $ longOptions );
3134 }
3235 function help ()
3336 {
3437 // help command
35- print "Help goes here \n" ;
38+ $ help = "Usage: php dev [command] [options] \n\n" ;
39+ $ help .= "Commands: \n" ;
40+ $ help .= " serve, start [port] Start the development server \n" ;
41+ $ help .= " -c [name] [editor command] Create a new controller \n" ;
42+ $ help .= " -h Display this help message \n" ;
43+ print $ help ;
3644 }
3745 function run ()
3846 {
39- $ command = $ this ->currentCommand ;
47+ $ command = $ this ->commands ;
48+ $ command_array = $ this ->currentCommand ;
4049 if (isset ($ command ['h ' ])) {
4150 $ this ->help ();
4251 }
43- if (isset ($ command ['c ' ])) {
52+ if (isset ($ command ['c ' ]) and isset ($ command_array [3 ])) {
53+ $ this ->makeController ($ command ['c ' ], $ command_array [3 ]);
54+ }
55+ if (isset ($ command ['c ' ]) and !isset ($ command_array [3 ])) {
4456 $ this ->makeController ($ command ['c ' ]);
4557 }
58+ if (isset ($ command ['m ' ]) and isset ($ command_array [3 ])) {
59+ $ this ->makeMiddleware ($ command ['m ' ], $ command_array [3 ]);
60+ }
61+ if (isset ($ command ['m ' ]) and !isset ($ command_array [3 ])) {
62+ $ this ->makeMiddleware ($ command ['m ' ]);
63+ }
4664 }
47- function makeController ($ name )
65+ function makeController ($ name, $ command = null )
4866 {
49- print ("Controller {$ name } created successfully \n" );
5067 $ file = ucfirst ($ name );
5168 $ content = "<?php \n\nnamespace app\web\controller; \n\nclass {$ file }\n{ \n\tfunction index() \n\t{ \n\t\t# code... \n\t} \n} " ;
5269 file_put_contents (__DIR__ . "/../../app/web/controller/ {$ file }.php " , $ content );
53- echo "Controller created successfully \n" ;
70+ echo "{$ file } created successfully \n" ;
71+ if ($ command ) system ($ command . ' ' . __DIR__ . "/../../app/web/controller/ {$ file }.php " );
72+ }
73+ function makeMiddleware ($ name , $ command = null )
74+ {
75+ $ file = ucfirst ($ name );
76+ $ content = "<?php \n\nnamespace app\web\middleware; \n\nclass {$ file }\n{ \n\tfunction handle() \n\t{ \n\t\t# code... \n\t} \n} " ;
77+ file_put_contents (__DIR__ . "/../../app/web/middleware/ {$ file }.php " , $ content );
78+ echo "{$ file } created successfully \n" ;
79+ if ($ command ) system ($ command . ' ' . __DIR__ . "/../../app/web/middleware/ {$ file }.php " );
5480 }
55- }
81+ }
0 commit comments