@@ -102,15 +102,15 @@ discard block |
||
102 | 102 | * @return array The commands and their description. |
103 | 103 | */ |
104 | 104 | public static function commands(){ |
105 | - $results = []; |
|
106 | - foreach(static::$commands as $name => $cmd){ |
|
105 | + $results = []; |
|
106 | + foreach(static::$commands as $name => $cmd){ |
|
107 | 107 | $results[] = [ |
108 | 108 | 'name' => $name, |
109 | 109 | 'params' => preg_replace('/:(\w+)/','[$1]',implode(' ',$cmd[0])), |
110 | 110 | 'description' => $cmd[2], |
111 | 111 | ]; |
112 | - } |
|
113 | - return $results; |
|
112 | + } |
|
113 | + return $results; |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | /** |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | if($args) { |
123 | 123 | $_args = $args; |
124 | 124 | static::$file = basename(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:__FILE__); |
125 | - } else { |
|
125 | + } else { |
|
126 | 126 | $_args = $_SERVER['argv']; |
127 | 127 | static::$file = basename(array_shift($_args)); |
128 | 128 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | if ($segment[0]==':'){ |
144 | 144 | // Extract paramenter |
145 | 145 | if (isset(static::$arguments[$_idx])){ |
146 | - $pars_vector[] = static::$arguments[$_idx]; |
|
146 | + $pars_vector[] = static::$arguments[$_idx]; |
|
147 | 147 | } else return static::triggerError("Command [".$command."] needs more parameters"); |
148 | 148 | } else { |
149 | 149 | // Match command |
@@ -159,15 +159,15 @@ discard block |
||
159 | 159 | } |
160 | 160 | } |
161 | 161 | |
162 | - /** |
|
163 | - * Prints a message to the console with color formatting. |
|
164 | - * @param string $message The html-like encoded message |
|
165 | - * @return void |
|
166 | - */ |
|
167 | - public static function write($message){ |
|
162 | + /** |
|
163 | + * Prints a message to the console with color formatting. |
|
164 | + * @param string $message The html-like encoded message |
|
165 | + * @return void |
|
166 | + */ |
|
167 | + public static function write($message){ |
|
168 | 168 | if( preg_match('~<[^>]+>~',$message)) { |
169 | - // Use preg_replace_callback for fast regex matches navigation |
|
170 | - echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
169 | + // Use preg_replace_callback for fast regex matches navigation |
|
170 | + echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
171 | 171 | static::write($m[1]); |
172 | 172 | $color = strtoupper(trim($m[2])); |
173 | 173 | if( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
@@ -177,37 +177,37 @@ discard block |
||
177 | 177 | $back_color = array_pop(static::$color_stack) ?: static::$color_stack[]='NORMAL'; |
178 | 178 | if( isset(static::$shell_colors[$back_color]) ) echo static::$shell_colors[$back_color]; |
179 | 179 | static::write($m[4]); |
180 | - },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
|
180 | + },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
|
181 | 181 | } else { |
182 | - echo strtr($message,["&BR;"=>PHP_EOL]); |
|
182 | + echo strtr($message,["&BR;"=>PHP_EOL]); |
|
183 | 183 | } |
184 | - } |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Like CLI::write, but appends a newline at the end. |
|
188 | - * @param string $message The html-like encoded message |
|
189 | - * @return void |
|
190 | - */ |
|
191 | - public static function writeln($message){ |
|
192 | - static::write($message . PHP_EOL); |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Set output ANSI color |
|
197 | - * @param string $color The color name constant. |
|
198 | - * @return void |
|
199 | - */ |
|
200 | - public static function color($color){ |
|
201 | - if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Edit a temporary block of text with $EDITOR (or nano as fallback) |
|
206 | - * @param string $text The initial text of the document. |
|
207 | - * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
|
208 | - * @return string The edited contents |
|
209 | - */ |
|
210 | - public static function edit($text,$filename=''){ |
|
186 | + /** |
|
187 | + * Like CLI::write, but appends a newline at the end. |
|
188 | + * @param string $message The html-like encoded message |
|
189 | + * @return void |
|
190 | + */ |
|
191 | + public static function writeln($message){ |
|
192 | + static::write($message . PHP_EOL); |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Set output ANSI color |
|
197 | + * @param string $color The color name constant. |
|
198 | + * @return void |
|
199 | + */ |
|
200 | + public static function color($color){ |
|
201 | + if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Edit a temporary block of text with $EDITOR (or nano as fallback) |
|
206 | + * @param string $text The initial text of the document. |
|
207 | + * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
|
208 | + * @return string The edited contents |
|
209 | + */ |
|
210 | + public static function edit($text,$filename=''){ |
|
211 | 211 | $EDITOR = getenv('EDITOR')?:'nano'; |
212 | 212 | $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename,'/','_'); |
213 | 213 | file_put_contents($tmp, $text); |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | // Standard Help Message |
225 | 225 | CLI::help(function(){ |
226 | 226 | echo 'Usage: ', CLI::name(),' [commands]', PHP_EOL, |
227 | - 'Commands:',PHP_EOL; |
|
227 | + 'Commands:',PHP_EOL; |
|
228 | 228 | foreach( CLI::commands() as $cmd ){ |
229 | 229 | echo "\t", $cmd['name'], ' ' ,$cmd['params'], PHP_EOL; |
230 | 230 | if($cmd['description']) |
@@ -21,18 +21,18 @@ discard block |
||
21 | 21 | protected static $error = null; |
22 | 22 | |
23 | 23 | protected static $shell_colors = [ |
24 | - 'BLACK' =>"\033[0;30m", 'DARKGRAY' =>"\033[1;30m", |
|
25 | - 'BLUE' =>"\033[0;34m", 'LIGHTBLUE' =>"\033[1;34m", |
|
26 | - 'GREEN' =>"\033[0;32m", 'LIGHTGREEN' =>"\033[1;32m", |
|
27 | - 'CYAN' =>"\033[0;36m", 'LIGHTCYAN' =>"\033[1;36m", |
|
28 | - 'RED' =>"\033[0;31m", 'LIGHTRED' =>"\033[1;31m", |
|
29 | - 'PURPLE' =>"\033[0;35m", 'LIGHTPURPLE' =>"\033[1;35m", |
|
30 | - 'BROWN' =>"\033[0;33m", 'YELLOW' =>"\033[1;33m", |
|
31 | - 'LIGHTGRAY' =>"\033[0;37m", 'WHITE' =>"\033[1;37m", |
|
32 | - 'NORMAL' =>"\033[0;37m", 'B' =>"\033[1m", |
|
33 | - 'ERROR' =>"\033[1;31m", 'INFO' =>"\033[0;36m", |
|
34 | - 'I' =>"\033[0;30;104m", 'IB' =>"\033[1;30;104m", |
|
35 | - 'U' =>"\033[4m", 'D' =>"\033[2m", |
|
24 | + 'BLACK' =>"\033[0;30m", 'DARKGRAY' =>"\033[1;30m", |
|
25 | + 'BLUE' =>"\033[0;34m", 'LIGHTBLUE' =>"\033[1;34m", |
|
26 | + 'GREEN' =>"\033[0;32m", 'LIGHTGREEN' =>"\033[1;32m", |
|
27 | + 'CYAN' =>"\033[0;36m", 'LIGHTCYAN' =>"\033[1;36m", |
|
28 | + 'RED' =>"\033[0;31m", 'LIGHTRED' =>"\033[1;31m", |
|
29 | + 'PURPLE' =>"\033[0;35m", 'LIGHTPURPLE' =>"\033[1;35m", |
|
30 | + 'BROWN' =>"\033[0;33m", 'YELLOW' =>"\033[1;33m", |
|
31 | + 'LIGHTGRAY' =>"\033[0;37m", 'WHITE' =>"\033[1;37m", |
|
32 | + 'NORMAL' =>"\033[0;37m", 'B' =>"\033[1m", |
|
33 | + 'ERROR' =>"\033[1;31m", 'INFO' =>"\033[0;36m", |
|
34 | + 'I' =>"\033[0;30;104m", 'IB' =>"\033[1;30;104m", |
|
35 | + 'U' =>"\033[4m", 'D' =>"\033[2m", |
|
36 | 36 | ]; |
37 | 37 | protected static $color_stack = ['NORMAL']; |
38 | 38 | |
@@ -42,16 +42,16 @@ discard block |
||
42 | 42 | * @param string $command The command route, use ":" before a parameter for extraction. |
43 | 43 | * @param callable $callback The callback to be binded to the route. |
44 | 44 | */ |
45 | - public static function on($command,callable $callback,$description=''){ |
|
46 | - $parts = preg_split('/\s+/',$command); |
|
47 | - static::$commands[array_shift($parts)] = [$parts,$callback,$description]; |
|
45 | + public static function on($command, callable $callback, $description = '') { |
|
46 | + $parts = preg_split('/\s+/', $command); |
|
47 | + static::$commands[array_shift($parts)] = [$parts, $callback, $description]; |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
51 | 51 | * Bind a callback to the "help" route. |
52 | 52 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
53 | 53 | */ |
54 | - public static function help(callable $callback = null){ |
|
54 | + public static function help(callable $callback = null) { |
|
55 | 55 | $callback |
56 | 56 | ? is_callable($callback) && static::$help = $callback |
57 | 57 | : static::$help && call_user_func(static::$help); |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * Bind a callback when an error occurs. |
62 | 62 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
63 | 63 | */ |
64 | - public static function error(callable $callback = null){ |
|
64 | + public static function error(callable $callback = null) { |
|
65 | 65 | $callback |
66 | 66 | ? is_callable($callback) && static::$error = $callback |
67 | 67 | : static::$error && call_user_func(static::$error); |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * Returns the script name. |
72 | 72 | * @return string |
73 | 73 | */ |
74 | - public static function name(){ |
|
74 | + public static function name() { |
|
75 | 75 | return static::$file; |
76 | 76 | } |
77 | 77 | |
@@ -79,8 +79,8 @@ discard block |
||
79 | 79 | * Triggers an error and exit |
80 | 80 | * @param string $message |
81 | 81 | */ |
82 | - protected static function triggerError($message){ |
|
83 | - is_callable(static::$error) && call_user_func(static::$error,$message); |
|
82 | + protected static function triggerError($message) { |
|
83 | + is_callable(static::$error) && call_user_func(static::$error, $message); |
|
84 | 84 | exit -1; |
85 | 85 | } |
86 | 86 | |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | * @param mixed $default The default value if parameter is omitted. (if a callable it will be evaluated) |
91 | 91 | * @return mixed |
92 | 92 | */ |
93 | - public static function input($key=null,$default=null){ |
|
94 | - return $key ? (isset(static::$options[$key]) ? static::$options[$key] : (is_callable($default)?call_user_func($default):$default)) : static::$options; |
|
93 | + public static function input($key = null, $default = null) { |
|
94 | + return $key ? (isset(static::$options[$key]) ? static::$options[$key] : (is_callable($default) ? call_user_func($default) : $default)) : static::$options; |
|
95 | 95 | } |
96 | 96 | |
97 | 97 | /** |
@@ -101,12 +101,12 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @return array The commands and their description. |
103 | 103 | */ |
104 | - public static function commands(){ |
|
104 | + public static function commands() { |
|
105 | 105 | $results = []; |
106 | - foreach(static::$commands as $name => $cmd){ |
|
106 | + foreach (static::$commands as $name => $cmd) { |
|
107 | 107 | $results[] = [ |
108 | 108 | 'name' => $name, |
109 | - 'params' => preg_replace('/:(\w+)/','[$1]',implode(' ',$cmd[0])), |
|
109 | + 'params' => preg_replace('/:(\w+)/', '[$1]', implode(' ', $cmd[0])), |
|
110 | 110 | 'description' => $cmd[2], |
111 | 111 | ]; |
112 | 112 | } |
@@ -118,40 +118,40 @@ discard block |
||
118 | 118 | * @param string[] $args The arguments array. |
119 | 119 | * @return boolean True if route was correctly dispatched. |
120 | 120 | */ |
121 | - public static function run($args=null){ |
|
122 | - if($args) { |
|
121 | + public static function run($args = null) { |
|
122 | + if ($args) { |
|
123 | 123 | $_args = $args; |
124 | - static::$file = basename(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:__FILE__); |
|
124 | + static::$file = basename(isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : __FILE__); |
|
125 | 125 | } else { |
126 | 126 | $_args = $_SERVER['argv']; |
127 | 127 | static::$file = basename(array_shift($_args)); |
128 | 128 | } |
129 | - foreach($_args as $e) if(strpos($e,'-')===0) { |
|
130 | - $h = explode('=',$e); |
|
131 | - static::$options[ltrim(current($h),'-')] = isset($h[1])?$h[1]:true; |
|
129 | + foreach ($_args as $e) if (strpos($e, '-') === 0) { |
|
130 | + $h = explode('=', $e); |
|
131 | + static::$options[ltrim(current($h), '-')] = isset($h[1]) ? $h[1] : true; |
|
132 | 132 | } else { |
133 | 133 | static::$arguments[] = $e; |
134 | 134 | } |
135 | 135 | |
136 | - if(isset(static::$arguments[0])){ |
|
136 | + if (isset(static::$arguments[0])) { |
|
137 | 137 | $command = array_shift(static::$arguments); |
138 | 138 | if (empty(static::$commands[$command])) |
139 | 139 | return static::triggerError("Unknown command [".$command."]."); |
140 | 140 | $cmd = static::$commands[$command]; |
141 | 141 | $pars_vector = []; |
142 | 142 | foreach ($cmd[0] as $_idx => $segment) { |
143 | - if ($segment[0]==':'){ |
|
143 | + if ($segment[0] == ':') { |
|
144 | 144 | // Extract paramenter |
145 | - if (isset(static::$arguments[$_idx])){ |
|
145 | + if (isset(static::$arguments[$_idx])) { |
|
146 | 146 | $pars_vector[] = static::$arguments[$_idx]; |
147 | 147 | } else return static::triggerError("Command [".$command."] needs more parameters"); |
148 | 148 | } else { |
149 | 149 | // Match command |
150 | - if (empty(static::$arguments[$_idx]) || $segment!=static::$arguments[$_idx]) |
|
150 | + if (empty(static::$arguments[$_idx]) || $segment != static::$arguments[$_idx]) |
|
151 | 151 | return static::triggerError("Command [".$command."] is incomplete."); |
152 | 152 | } |
153 | 153 | } |
154 | - call_user_func_array($cmd[1],$pars_vector); |
|
154 | + call_user_func_array($cmd[1], $pars_vector); |
|
155 | 155 | return true; |
156 | 156 | } else { |
157 | 157 | static::help(); |
@@ -164,22 +164,22 @@ discard block |
||
164 | 164 | * @param string $message The html-like encoded message |
165 | 165 | * @return void |
166 | 166 | */ |
167 | - public static function write($message){ |
|
168 | - if( preg_match('~<[^>]+>~',$message)) { |
|
167 | + public static function write($message) { |
|
168 | + if (preg_match('~<[^>]+>~', $message)) { |
|
169 | 169 | // Use preg_replace_callback for fast regex matches navigation |
170 | - echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
170 | + echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm', function($m) { |
|
171 | 171 | static::write($m[1]); |
172 | 172 | $color = strtoupper(trim($m[2])); |
173 | - if( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
173 | + if (isset(static::$shell_colors[$color])) echo static::$shell_colors[$color]; |
|
174 | 174 | static::$color_stack[] = $color; |
175 | 175 | static::write($m[3]); |
176 | 176 | array_pop(static::$color_stack); |
177 | - $back_color = array_pop(static::$color_stack) ?: static::$color_stack[]='NORMAL'; |
|
178 | - if( isset(static::$shell_colors[$back_color]) ) echo static::$shell_colors[$back_color]; |
|
177 | + $back_color = array_pop(static::$color_stack) ?: static::$color_stack[] = 'NORMAL'; |
|
178 | + if (isset(static::$shell_colors[$back_color])) echo static::$shell_colors[$back_color]; |
|
179 | 179 | static::write($m[4]); |
180 | - },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
|
180 | + },strtr($message, ["\n"=>"&BR;"])), ["&BR;"=>PHP_EOL]); |
|
181 | 181 | } else { |
182 | - echo strtr($message,["&BR;"=>PHP_EOL]); |
|
182 | + echo strtr($message, ["&BR;"=>PHP_EOL]); |
|
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
@@ -188,8 +188,8 @@ discard block |
||
188 | 188 | * @param string $message The html-like encoded message |
189 | 189 | * @return void |
190 | 190 | */ |
191 | - public static function writeln($message){ |
|
192 | - static::write($message . PHP_EOL); |
|
191 | + public static function writeln($message) { |
|
192 | + static::write($message.PHP_EOL); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | /** |
@@ -197,8 +197,8 @@ discard block |
||
197 | 197 | * @param string $color The color name constant. |
198 | 198 | * @return void |
199 | 199 | */ |
200 | - public static function color($color){ |
|
201 | - if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
200 | + public static function color($color) { |
|
201 | + if (isset(static::$shell_colors[$color])) echo static::$shell_colors[$color]; |
|
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
@@ -207,9 +207,9 @@ discard block |
||
207 | 207 | * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
208 | 208 | * @return string The edited contents |
209 | 209 | */ |
210 | - public static function edit($text,$filename=''){ |
|
211 | - $EDITOR = getenv('EDITOR')?:'nano'; |
|
212 | - $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename,'/','_'); |
|
210 | + public static function edit($text, $filename = '') { |
|
211 | + $EDITOR = getenv('EDITOR') ?: 'nano'; |
|
212 | + $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename, '/', '_'); |
|
213 | 213 | file_put_contents($tmp, $text); |
214 | 214 | passthru("$EDITOR $tmp"); |
215 | 215 | $result = file_get_contents($tmp); |
@@ -222,17 +222,17 @@ discard block |
||
222 | 222 | |
223 | 223 | |
224 | 224 | // Standard Help Message |
225 | -CLI::help(function(){ |
|
226 | - echo 'Usage: ', CLI::name(),' [commands]', PHP_EOL, |
|
227 | - 'Commands:',PHP_EOL; |
|
228 | - foreach( CLI::commands() as $cmd ){ |
|
229 | - echo "\t", $cmd['name'], ' ' ,$cmd['params'], PHP_EOL; |
|
230 | - if($cmd['description']) |
|
231 | - echo "\t\t- ", str_replace("\n","\n\t\t ",$cmd['description']), PHP_EOL, PHP_EOL; |
|
225 | +CLI::help(function() { |
|
226 | + echo 'Usage: ', CLI::name(), ' [commands]', PHP_EOL, |
|
227 | + 'Commands:', PHP_EOL; |
|
228 | + foreach (CLI::commands() as $cmd) { |
|
229 | + echo "\t", $cmd['name'], ' ', $cmd['params'], PHP_EOL; |
|
230 | + if ($cmd['description']) |
|
231 | + echo "\t\t- ", str_replace("\n", "\n\t\t ", $cmd['description']), PHP_EOL, PHP_EOL; |
|
232 | 232 | } |
233 | 233 | }); |
234 | 234 | |
235 | 235 | // Standard Error Message |
236 | -CLI::error(function($message){ |
|
237 | - echo 'Error: ',$message,PHP_EOL; |
|
236 | +CLI::error(function($message) { |
|
237 | + echo 'Error: ', $message, PHP_EOL; |
|
238 | 238 | }); |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | * @param string $command The command route, use ":" before a parameter for extraction. |
43 | 43 | * @param callable $callback The callback to be binded to the route. |
44 | 44 | */ |
45 | - public static function on($command,callable $callback,$description=''){ |
|
45 | + public static function on($command,callable $callback,$description='') { |
|
46 | 46 | $parts = preg_split('/\s+/',$command); |
47 | 47 | static::$commands[array_shift($parts)] = [$parts,$callback,$description]; |
48 | 48 | } |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | * Bind a callback to the "help" route. |
52 | 52 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
53 | 53 | */ |
54 | - public static function help(callable $callback = null){ |
|
54 | + public static function help(callable $callback = null) { |
|
55 | 55 | $callback |
56 | 56 | ? is_callable($callback) && static::$help = $callback |
57 | 57 | : static::$help && call_user_func(static::$help); |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * Bind a callback when an error occurs. |
62 | 62 | * @param callable $callback The callback to be binded to the route. If omitted triggers the callback. |
63 | 63 | */ |
64 | - public static function error(callable $callback = null){ |
|
64 | + public static function error(callable $callback = null) { |
|
65 | 65 | $callback |
66 | 66 | ? is_callable($callback) && static::$error = $callback |
67 | 67 | : static::$error && call_user_func(static::$error); |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * Returns the script name. |
72 | 72 | * @return string |
73 | 73 | */ |
74 | - public static function name(){ |
|
74 | + public static function name() { |
|
75 | 75 | return static::$file; |
76 | 76 | } |
77 | 77 | |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | * Triggers an error and exit |
80 | 80 | * @param string $message |
81 | 81 | */ |
82 | - protected static function triggerError($message){ |
|
82 | + protected static function triggerError($message) { |
|
83 | 83 | is_callable(static::$error) && call_user_func(static::$error,$message); |
84 | 84 | exit -1; |
85 | 85 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @param mixed $default The default value if parameter is omitted. (if a callable it will be evaluated) |
91 | 91 | * @return mixed |
92 | 92 | */ |
93 | - public static function input($key=null,$default=null){ |
|
93 | + public static function input($key=null,$default=null) { |
|
94 | 94 | return $key ? (isset(static::$options[$key]) ? static::$options[$key] : (is_callable($default)?call_user_func($default):$default)) : static::$options; |
95 | 95 | } |
96 | 96 | |
@@ -101,9 +101,9 @@ discard block |
||
101 | 101 | * |
102 | 102 | * @return array The commands and their description. |
103 | 103 | */ |
104 | - public static function commands(){ |
|
104 | + public static function commands() { |
|
105 | 105 | $results = []; |
106 | - foreach(static::$commands as $name => $cmd){ |
|
106 | + foreach(static::$commands as $name => $cmd) { |
|
107 | 107 | $results[] = [ |
108 | 108 | 'name' => $name, |
109 | 109 | 'params' => preg_replace('/:(\w+)/','[$1]',implode(' ',$cmd[0])), |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | * @param string[] $args The arguments array. |
119 | 119 | * @return boolean True if route was correctly dispatched. |
120 | 120 | */ |
121 | - public static function run($args=null){ |
|
121 | + public static function run($args=null) { |
|
122 | 122 | if($args) { |
123 | 123 | $_args = $args; |
124 | 124 | static::$file = basename(isset($_SERVER['PHP_SELF'])?$_SERVER['PHP_SELF']:__FILE__); |
@@ -133,22 +133,26 @@ discard block |
||
133 | 133 | static::$arguments[] = $e; |
134 | 134 | } |
135 | 135 | |
136 | - if(isset(static::$arguments[0])){ |
|
136 | + if(isset(static::$arguments[0])) { |
|
137 | 137 | $command = array_shift(static::$arguments); |
138 | - if (empty(static::$commands[$command])) |
|
139 | - return static::triggerError("Unknown command [".$command."]."); |
|
138 | + if (empty(static::$commands[$command])) { |
|
139 | + return static::triggerError("Unknown command [".$command."]."); |
|
140 | + } |
|
140 | 141 | $cmd = static::$commands[$command]; |
141 | 142 | $pars_vector = []; |
142 | 143 | foreach ($cmd[0] as $_idx => $segment) { |
143 | - if ($segment[0]==':'){ |
|
144 | + if ($segment[0]==':') { |
|
144 | 145 | // Extract paramenter |
145 | - if (isset(static::$arguments[$_idx])){ |
|
146 | + if (isset(static::$arguments[$_idx])) { |
|
146 | 147 | $pars_vector[] = static::$arguments[$_idx]; |
147 | - } else return static::triggerError("Command [".$command."] needs more parameters"); |
|
148 | + } else { |
|
149 | + return static::triggerError("Command [".$command."] needs more parameters"); |
|
150 | + } |
|
148 | 151 | } else { |
149 | 152 | // Match command |
150 | - if (empty(static::$arguments[$_idx]) || $segment!=static::$arguments[$_idx]) |
|
151 | - return static::triggerError("Command [".$command."] is incomplete."); |
|
153 | + if (empty(static::$arguments[$_idx]) || $segment!=static::$arguments[$_idx]) { |
|
154 | + return static::triggerError("Command [".$command."] is incomplete."); |
|
155 | + } |
|
152 | 156 | } |
153 | 157 | } |
154 | 158 | call_user_func_array($cmd[1],$pars_vector); |
@@ -164,18 +168,22 @@ discard block |
||
164 | 168 | * @param string $message The html-like encoded message |
165 | 169 | * @return void |
166 | 170 | */ |
167 | - public static function write($message){ |
|
171 | + public static function write($message) { |
|
168 | 172 | if( preg_match('~<[^>]+>~',$message)) { |
169 | 173 | // Use preg_replace_callback for fast regex matches navigation |
170 | - echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m){ |
|
174 | + echo strtr(preg_replace_callback('~^(.*)<([^>]+)>(.+)</\2>(.*)$~USm',function($m) { |
|
171 | 175 | static::write($m[1]); |
172 | 176 | $color = strtoupper(trim($m[2])); |
173 | - if( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
177 | + if( isset(static::$shell_colors[$color]) ) { |
|
178 | + echo static::$shell_colors[$color]; |
|
179 | + } |
|
174 | 180 | static::$color_stack[] = $color; |
175 | 181 | static::write($m[3]); |
176 | 182 | array_pop(static::$color_stack); |
177 | 183 | $back_color = array_pop(static::$color_stack) ?: static::$color_stack[]='NORMAL'; |
178 | - if( isset(static::$shell_colors[$back_color]) ) echo static::$shell_colors[$back_color]; |
|
184 | + if( isset(static::$shell_colors[$back_color]) ) { |
|
185 | + echo static::$shell_colors[$back_color]; |
|
186 | + } |
|
179 | 187 | static::write($m[4]); |
180 | 188 | },strtr($message,["\n"=>"&BR;"])),["&BR;"=>PHP_EOL]); |
181 | 189 | } else { |
@@ -188,7 +196,7 @@ discard block |
||
188 | 196 | * @param string $message The html-like encoded message |
189 | 197 | * @return void |
190 | 198 | */ |
191 | - public static function writeln($message){ |
|
199 | + public static function writeln($message) { |
|
192 | 200 | static::write($message . PHP_EOL); |
193 | 201 | } |
194 | 202 | |
@@ -197,8 +205,10 @@ discard block |
||
197 | 205 | * @param string $color The color name constant. |
198 | 206 | * @return void |
199 | 207 | */ |
200 | - public static function color($color){ |
|
201 | - if ( isset(static::$shell_colors[$color]) ) echo static::$shell_colors[$color]; |
|
208 | + public static function color($color) { |
|
209 | + if ( isset(static::$shell_colors[$color]) ) { |
|
210 | + echo static::$shell_colors[$color]; |
|
211 | + } |
|
202 | 212 | } |
203 | 213 | |
204 | 214 | /** |
@@ -207,7 +217,7 @@ discard block |
||
207 | 217 | * @param string $filename The (fake) filename passed to the editor (for syntax highlighting hint). |
208 | 218 | * @return string The edited contents |
209 | 219 | */ |
210 | - public static function edit($text,$filename=''){ |
|
220 | + public static function edit($text,$filename='') { |
|
211 | 221 | $EDITOR = getenv('EDITOR')?:'nano'; |
212 | 222 | $tmp = tempnam(sys_get_temp_dir(), "E-").strtr($filename,'/','_'); |
213 | 223 | file_put_contents($tmp, $text); |
@@ -222,17 +232,18 @@ discard block |
||
222 | 232 | |
223 | 233 | |
224 | 234 | // Standard Help Message |
225 | -CLI::help(function(){ |
|
235 | +CLI::help(function() { |
|
226 | 236 | echo 'Usage: ', CLI::name(),' [commands]', PHP_EOL, |
227 | 237 | 'Commands:',PHP_EOL; |
228 | - foreach( CLI::commands() as $cmd ){ |
|
238 | + foreach( CLI::commands() as $cmd ) { |
|
229 | 239 | echo "\t", $cmd['name'], ' ' ,$cmd['params'], PHP_EOL; |
230 | - if($cmd['description']) |
|
231 | - echo "\t\t- ", str_replace("\n","\n\t\t ",$cmd['description']), PHP_EOL, PHP_EOL; |
|
240 | + if($cmd['description']) { |
|
241 | + echo "\t\t- ", str_replace("\n","\n\t\t ",$cmd['description']), PHP_EOL, PHP_EOL; |
|
242 | + } |
|
232 | 243 | } |
233 | 244 | }); |
234 | 245 | |
235 | 246 | // Standard Error Message |
236 | -CLI::error(function($message){ |
|
247 | +CLI::error(function($message) { |
|
237 | 248 | echo 'Error: ',$message,PHP_EOL; |
238 | 249 | }); |