1+ <?php
2+
3+ /**
4+ * CodeMommy TaskPHP
5+ * @author Candison November <www.kandisheng.com>
6+ */
7+
8+ namespace CodeMommy \TaskPHP ;
9+
10+ class Task
11+ {
12+ private static $ task = array ();
13+ private static $ config = array ();
14+
15+ /**
16+ * Get Command
17+ * @return null
18+ */
19+ private static function getCommand ()
20+ {
21+ if (isset ($ _SERVER ['argv ' ][1 ])) {
22+ return $ _SERVER ['argv ' ][1 ];
23+ }
24+ return null ;
25+ }
26+
27+ /**
28+ * Default Task
29+ */
30+ private static function taskHelp ()
31+ {
32+ if (!empty (self ::$ config )) {
33+ echo (sprintf ('%s %s%s ' , self ::$ config ['title ' ], self ::$ config ['version ' ], PHP_EOL ));
34+ self ::line ();
35+ }
36+ echo (sprintf ('Tasks are:%s ' , PHP_EOL ));
37+ self ::line ();
38+ ksort (self ::$ task );
39+ foreach (self ::$ task as $ value ) {
40+ echo (sprintf ('%s - %s%s ' , $ value [0 ], $ value [1 ], PHP_EOL ));
41+ }
42+ }
43+
44+ /**
45+ * Get Parameter
46+ *
47+ * @param $number
48+ * @param string $default
49+ *
50+ * @return string
51+ */
52+ public static function getParameter ($ number , $ default = '' )
53+ {
54+ array_shift ($ _SERVER ['argv ' ]);
55+ array_shift ($ _SERVER ['argv ' ]);
56+ if (isset ($ _SERVER ['argv ' ][$ number ])) {
57+ return $ _SERVER ['argv ' ][$ number ];
58+ }
59+ return $ default ;
60+ }
61+
62+ /**
63+ * Line
64+ */
65+ public static function line ()
66+ {
67+ echo (sprintf ('--------------------%s ' , PHP_EOL ));
68+ }
69+
70+ /**
71+ * Config
72+ *
73+ * @param $title
74+ * @param $version
75+ */
76+ public static function config ($ title , $ version )
77+ {
78+ self ::$ config ['title ' ] = $ title ;
79+ self ::$ config ['version ' ] = $ version ;
80+ }
81+
82+ /**
83+ * Add
84+ *
85+ * @param $command
86+ * @param $about
87+ * @param $function
88+ */
89+ public static function add ($ command , $ about , $ function )
90+ {
91+ self ::$ task [strtolower ($ command )] = array ($ command , $ about , $ function );
92+ }
93+
94+ /**
95+ * Run
96+ */
97+ public static function run ()
98+ {
99+ self ::add ('help ' , 'Help ' , '' );
100+ $ command = strtolower (self ::getCommand ());
101+ if ($ command == 'help ' ) {
102+ self ::taskHelp ();
103+ return ;
104+ }
105+ if (isset (self ::$ task [$ command ])) {
106+ $ command = self ::$ task [$ command ][2 ];
107+ $ command ();
108+ return ;
109+ }
110+ self ::taskHelp ();
111+ }
112+ }
0 commit comments