8
8
9
9
namespace inhere \librarys \console ;
10
10
11
- use inhere \librarys \io \Input as BaseInput ;
12
-
13
11
/**
14
12
* Class Input
15
13
* @package inhere\librarys\console
14
+ * e.g:
15
+ * ./bin/app image/packTask test -d -s=df --debug=true
16
+ * php bin/cli.php start test -d -s=df --debug=true
16
17
*/
17
- class Input extends BaseInput
18
+ class Input
18
19
{
20
+ /**
21
+ * @var @resource
22
+ */
19
23
protected $ inputStream = STDIN ;
20
24
25
+ /**
26
+ * Input data
27
+ * @var array
28
+ */
29
+ protected $ data = [];
30
+
31
+ /**
32
+ * the script name
33
+ * e.g `./bin/app` OR `bin/cli.php`
34
+ * @var string
35
+ */
21
36
public static $ scriptName ;
22
37
38
+ /**
39
+ * the script name
40
+ * e.g `image/packTask` OR `start`
41
+ * @var string
42
+ */
43
+ public static $ command ;
44
+
45
+ public function __construct ($ parseArgv = true , $ fixServer = false , $ fillToGlobal = false )
46
+ {
47
+ if ($ parseArgv ) {
48
+ $ this ->data = self ::parseGlobalArgv ($ fixServer , $ fillToGlobal );
49
+ }
50
+ }
51
+
23
52
/**
24
53
* @return string
25
54
*/
@@ -28,6 +57,20 @@ public function read()
28
57
return trim (fgets ($ this ->inputStream ));
29
58
}
30
59
60
+ /**
61
+ * @param null|string $name
62
+ * @param mixed $default
63
+ * @return mixed
64
+ */
65
+ public function get ($ name =null , $ default = null )
66
+ {
67
+ if (null === $ name ) {
68
+ return $ this ->data ;
69
+ }
70
+
71
+ return isset ($ this ->data [$ name ]) ? $ this ->data [$ name ] : $ default ;
72
+ }
73
+
31
74
/**
32
75
* @param $key
33
76
* @param bool $default
@@ -41,7 +84,31 @@ public function getBool($key, $default = false)
41
84
return $ default ;
42
85
}
43
86
44
- return in_array ($ value , ['0 ' , 0 , 'false ' ], true ) ? false : true ;
87
+ return in_array ($ value , ['0 ' , 0 , 'false ' , false ], true ) ? false : true ;
88
+ }
89
+
90
+ /**
91
+ * @return string
92
+ */
93
+ public function getScriptName ()
94
+ {
95
+ return self ::$ scriptName ;
96
+ }
97
+
98
+ /**
99
+ * @return string
100
+ */
101
+ public function getCommand ()
102
+ {
103
+ return self ::$ command ;
104
+ }
105
+
106
+ /**
107
+ * @return string
108
+ */
109
+ public function getData ()
110
+ {
111
+ return $ this ->data ;
45
112
}
46
113
47
114
/**
@@ -52,44 +119,64 @@ public function getInputStream()
52
119
return $ this ->inputStream ;
53
120
}
54
121
55
-
56
122
/**
57
- * @param array $argv
58
123
*/
59
- public static function parseConsoleArgs ( $ argv )
124
+ public static function parseGlobalArgv ( $ fixServer = false , $ fillToGlobal = false )
60
125
{
61
- // fixed: '/home' is not equals to '/home/'
62
- if ( isset ( $ _SERVER [ ' REQUEST_URI ' ])) {
63
- $ _SERVER [ ' REQUEST_URI ' ] = rtrim ( $ _SERVER [ ' REQUEST_URI ' ], ' / ' ) ;
64
- }
126
+ // ./bin/app image/packTask start test -d -s=df --debug=true
127
+ // php bin/cli.php image/packTask start test -d -s=df --debug=true
128
+ global $ argv ;
129
+ $ args = $ argv ;
65
130
66
- // fixed: PHP_SELF = 'index.php', it is should be '/index.php'
67
- if (isset ($ _SERVER ['PHP_SELF ' ])) {
68
- $ _SERVER ['PHP_SELF ' ] = '/ ' . ltrim ($ _SERVER ['PHP_SELF ' ],'/ ' );
131
+ if ($ args [0 ] === 'php ' ) {
132
+ array_shift ($ args );
69
133
}
70
134
71
- self ::$ scriptName = array_shift ($ argv );
135
+ self ::$ scriptName = array_shift ($ args );
136
+
137
+ if ($ fixServer ) {
138
+ // fixed: '/home' is not equals to '/home/'
139
+ if (isset ($ _SERVER ['REQUEST_URI ' ])) {
140
+ $ _SERVER ['REQUEST_URI ' ] = rtrim ($ _SERVER ['REQUEST_URI ' ],'/ ' );
141
+ }
72
142
73
- // $_SERVER[' PHP_SELF'] = self::$scriptName;
74
- $ _SERVER ['SERVER_PROTOCOL ' ] = ' HTTP/1.1 ' ;
75
- $ _SERVER ['REQUEST_METHOD ' ] = 'GET ' ;
76
- $ _SERVER [ ' REQUEST_URI ' ] = ' / ' ;
143
+ // fixed: PHP_SELF = 'index.php', it is should be '/index.php'
144
+ if ( isset ( $ _SERVER ['PHP_SELF ' ])) {
145
+ $ _SERVER ['PHP_SELF ' ] = '/ ' . ltrim ( $ _SERVER [ ' PHP_SELF ' ], ' / ' ) ;
146
+ }
77
147
78
- if ( isset ($ argv [0 ]) && strpos ($ argv [0 ], '= ' ) === false ) {
79
- $ path = trim (array_shift ($ argv ), '/ ' );
80
148
81
- $ _SERVER ['REQUEST_URI ' ] .= $ path ;
149
+ // $_SERVER['PHP_SELF'] = self::$scriptName;
150
+ $ _SERVER ['SERVER_PROTOCOL ' ] = 'HTTP/1.1 ' ;
151
+ $ _SERVER ['REQUEST_METHOD ' ] = 'GET ' ;
152
+ $ _SERVER ['REQUEST_URI ' ] = '/ ' ;
82
153
}
83
154
155
+ // collect command
156
+ if ( isset ($ args [0 ]) && strpos ($ args [0 ], '= ' ) === false ) {
157
+ self ::$ command = trim (array_shift ($ args ), '/ ' );
158
+
159
+ if ($ fixServer ) {
160
+ $ _SERVER ['REQUEST_URI ' ] .= self ::$ command ;
161
+ }
162
+ }
163
+
164
+ $ data = [];
165
+
84
166
// parse query params
85
167
// ./bin/app image/packTask start test -d -s=df --debug=true
86
168
// parse to
87
169
// ./bin/app image/packTask?start&test&d&s=df&debug=true
88
- if ($ argv ) {
89
- $ url = preg_replace ('/&[-]+/ ' , '& ' , implode ('& ' ,$ argv ));
170
+ if ($ args ) {
171
+ $ url = preg_replace ('/&[-]+/ ' , '& ' , implode ('& ' ,$ args ));
90
172
91
173
parse_str ($ url , $ data );
92
- $ _REQUEST = $ _GET = $ data ;
174
+
175
+ if ($ fillToGlobal ) {
176
+ $ _REQUEST = $ _GET = $ data ;
177
+ }
93
178
}
179
+
180
+ return $ data ;
94
181
}
95
182
}
0 commit comments