|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2017-08-15 |
| 6 | + * Time: 10:51 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Toolkit\Cli; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class LiteApp - A lite CLI Application |
| 13 | + * @package Inhere\Console |
| 14 | + */ |
| 15 | +class LiteApp |
| 16 | +{ |
| 17 | + /**************************************************************************** |
| 18 | + * simple cli support |
| 19 | + ****************************************************************************/ |
| 20 | + |
| 21 | + /** |
| 22 | + * parse from `name=val var2=val2` |
| 23 | + * @var array |
| 24 | + */ |
| 25 | + private $args = []; |
| 26 | + |
| 27 | + /** |
| 28 | + * parse from `--name=val --var2=val2 -d` |
| 29 | + * @var array |
| 30 | + */ |
| 31 | + private $opts = []; |
| 32 | + |
| 33 | + /** @var string */ |
| 34 | + private $script = ''; |
| 35 | + |
| 36 | + /** @var string */ |
| 37 | + private $command = ''; |
| 38 | + |
| 39 | + /** |
| 40 | + * user add commands |
| 41 | + * @var array |
| 42 | + */ |
| 43 | + private $commands = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * description message for the command |
| 47 | + * @var array |
| 48 | + */ |
| 49 | + private $messages = []; |
| 50 | + |
| 51 | + /** |
| 52 | + * @param bool $exit |
| 53 | + * @throws \InvalidArgumentException |
| 54 | + */ |
| 55 | + public function run(bool $exit = true) |
| 56 | + { |
| 57 | + $this->parseCliArgv(); |
| 58 | + |
| 59 | + if (isset($this->args[0])) { |
| 60 | + $this->command = $this->args[0]; |
| 61 | + unset($this->args[0]); |
| 62 | + } |
| 63 | + |
| 64 | + $this->dispatch($exit); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param bool $exit |
| 69 | + * @throws \InvalidArgumentException |
| 70 | + */ |
| 71 | + public function dispatch(bool $exit = true) |
| 72 | + { |
| 73 | + if (!$command = $this->command) { |
| 74 | + $this->showCommands(); |
| 75 | + } |
| 76 | + |
| 77 | + $status = 0; |
| 78 | + |
| 79 | + try { |
| 80 | + if (isset($this->commands[$command])) { |
| 81 | + $status = $this->runHandler($command, $this->commands[$command]); |
| 82 | + } else { |
| 83 | + $this->showCommands("The command {$command} not exists!"); |
| 84 | + } |
| 85 | + } catch (\Throwable $e) { |
| 86 | + $status = $this->handleException($e); |
| 87 | + } |
| 88 | + |
| 89 | + if ($exit) { |
| 90 | + $this->stop($status); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param int $code |
| 96 | + */ |
| 97 | + public function stop($code = 0) |
| 98 | + { |
| 99 | + exit((int)$code); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @param string $command |
| 104 | + * @param $handler |
| 105 | + * @return mixed |
| 106 | + * @throws \InvalidArgumentException |
| 107 | + */ |
| 108 | + public function runHandler(string $command, $handler) |
| 109 | + { |
| 110 | + if (\is_string($handler)) { |
| 111 | + // function name |
| 112 | + if (\function_exists($handler)) { |
| 113 | + return $handler($this); |
| 114 | + } |
| 115 | + |
| 116 | + if (\class_exists($handler)) { |
| 117 | + $handler = new $handler; |
| 118 | + |
| 119 | + // $handler->execute() |
| 120 | + if (\method_exists($handler, 'execute')) { |
| 121 | + return $handler->execute($this); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // a \Closure OR $handler->__invoke() |
| 127 | + if (\method_exists($handler, '__invoke')) { |
| 128 | + return $handler($this); |
| 129 | + } |
| 130 | + |
| 131 | + throw new \InvalidArgumentException("Invalid handler of the command: $command"); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @param \Throwable $e |
| 136 | + * @return int |
| 137 | + */ |
| 138 | + protected function handleException(\Throwable $e): int |
| 139 | + { |
| 140 | + $code = $e->getCode() !== 0 ? $e->getCode() : 133; |
| 141 | + |
| 142 | + printf( |
| 143 | + "Exception(%d): %s\nFile: %s(Line %d)\nTrace:\n%s\n", |
| 144 | + $code, |
| 145 | + $e->getMessage(), |
| 146 | + $e->getFile(), |
| 147 | + $e->getLine(), |
| 148 | + $e->getTraceAsString() |
| 149 | + ); |
| 150 | + |
| 151 | + return $code; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * parseCliArgv |
| 156 | + */ |
| 157 | + public function parseCliArgv() |
| 158 | + { |
| 159 | + /** @var array $argv */ |
| 160 | + $argv = $_SERVER['argv']; |
| 161 | + $this->script = array_shift($argv); |
| 162 | + |
| 163 | + foreach ($argv as $key => $value) { |
| 164 | + // opts |
| 165 | + if (strpos($value, '-') === 0) { |
| 166 | + $value = trim($value, '-'); |
| 167 | + |
| 168 | + if (!$value) { |
| 169 | + continue; |
| 170 | + } |
| 171 | + |
| 172 | + if (strpos($value, '=')) { |
| 173 | + list($n, $v) = explode('=', $value); |
| 174 | + $this->opts[$n] = $v; |
| 175 | + } else { |
| 176 | + $this->opts[$value] = true; |
| 177 | + } |
| 178 | + } elseif (strpos($value, '=')) { |
| 179 | + list($n, $v) = explode('=', $value); |
| 180 | + $this->args[$n] = $v; |
| 181 | + } else { |
| 182 | + $this->args[] = $value; |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @param string $command |
| 189 | + * @param string|\Closure $handler |
| 190 | + * @param string $description |
| 191 | + * @throws \InvalidArgumentException |
| 192 | + */ |
| 193 | + public function addCommand(string $command, $handler, $description = '') |
| 194 | + { |
| 195 | + if (!$command || !$handler) { |
| 196 | + throw new \InvalidArgumentException('Invalid arguments'); |
| 197 | + } |
| 198 | + |
| 199 | + $this->commands[$command] = $handler; |
| 200 | + $this->messages[$command] = trim($description); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * @param array $commands |
| 205 | + * @throws \InvalidArgumentException |
| 206 | + */ |
| 207 | + public function commands(array $commands) |
| 208 | + { |
| 209 | + foreach ($commands as $command => $handler) { |
| 210 | + $des = ''; |
| 211 | + |
| 212 | + if (\is_array($handler)) { |
| 213 | + $conf = array_values($handler); |
| 214 | + $handler = $conf[0]; |
| 215 | + $des = $conf[1] ?? ''; |
| 216 | + } |
| 217 | + |
| 218 | + $this->addCommand($command, $handler, $des); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + /**************************************************************************** |
| 223 | + * helper methods |
| 224 | + ****************************************************************************/ |
| 225 | + |
| 226 | + /** |
| 227 | + * @param string $err |
| 228 | + */ |
| 229 | + public function showCommands($err = '') |
| 230 | + { |
| 231 | + if ($err) { |
| 232 | + echo Color::render("<red>ERROR</red>: $err\n\n"); |
| 233 | + } |
| 234 | + |
| 235 | + $commandWidth = 12; |
| 236 | + $help = "Welcome to the Lite Console Application.\n\n<comment>Available Commands:</comment>\n"; |
| 237 | + |
| 238 | + foreach ($this->messages as $command => $desc) { |
| 239 | + $command = str_pad($command, $commandWidth, ' '); |
| 240 | + $desc = $desc ?: 'No description for the command'; |
| 241 | + $help .= " $command $desc\n"; |
| 242 | + } |
| 243 | + |
| 244 | + echo Color::render($help) . PHP_EOL; |
| 245 | + exit(0); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * @param string $name |
| 250 | + * @param mixed $default |
| 251 | + * @return mixed|null |
| 252 | + */ |
| 253 | + public function getArg($name, $default = null) |
| 254 | + { |
| 255 | + return $this->args[$name] ?? $default; |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * @param string $name |
| 260 | + * @param mixed $default |
| 261 | + * @return mixed|null |
| 262 | + */ |
| 263 | + public function getOpt($name, $default = null) |
| 264 | + { |
| 265 | + return $this->opts[$name] ?? $default; |
| 266 | + } |
| 267 | + |
| 268 | + /**************************************************************************** |
| 269 | + * getter/setter methods |
| 270 | + ****************************************************************************/ |
| 271 | + |
| 272 | + /** |
| 273 | + * @return array |
| 274 | + */ |
| 275 | + public function getArgs(): array |
| 276 | + { |
| 277 | + return $this->args; |
| 278 | + } |
| 279 | + |
| 280 | + /** |
| 281 | + * @param array $args |
| 282 | + */ |
| 283 | + public function setArgs(array $args) |
| 284 | + { |
| 285 | + $this->args = $args; |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * @return array |
| 290 | + */ |
| 291 | + public function getOpts(): array |
| 292 | + { |
| 293 | + return $this->opts; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * @param array $opts |
| 298 | + */ |
| 299 | + public function setOpts(array $opts) |
| 300 | + { |
| 301 | + $this->opts = $opts; |
| 302 | + } |
| 303 | + |
| 304 | + /** |
| 305 | + * @return string |
| 306 | + */ |
| 307 | + public function getScript(): string |
| 308 | + { |
| 309 | + return $this->script; |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * @param string $script |
| 314 | + */ |
| 315 | + public function setScript(string $script) |
| 316 | + { |
| 317 | + $this->script = $script; |
| 318 | + } |
| 319 | + |
| 320 | + /** |
| 321 | + * @return string |
| 322 | + */ |
| 323 | + public function getCommand(): string |
| 324 | + { |
| 325 | + return $this->command; |
| 326 | + } |
| 327 | + |
| 328 | + /** |
| 329 | + * @param string $command |
| 330 | + */ |
| 331 | + public function setCommand(string $command) |
| 332 | + { |
| 333 | + $this->command = $command; |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * @return array |
| 338 | + */ |
| 339 | + public function getCommands(): array |
| 340 | + { |
| 341 | + return $this->commands; |
| 342 | + } |
| 343 | + |
| 344 | + /** |
| 345 | + * @param array $commands |
| 346 | + */ |
| 347 | + public function setCommands(array $commands) |
| 348 | + { |
| 349 | + $this->commands = $commands; |
| 350 | + } |
| 351 | + |
| 352 | + /** |
| 353 | + * @return array |
| 354 | + */ |
| 355 | + public function getMessages(): array |
| 356 | + { |
| 357 | + return $this->messages; |
| 358 | + } |
| 359 | + |
| 360 | + /** |
| 361 | + * @param array $messages |
| 362 | + */ |
| 363 | + public function setMessages(array $messages) |
| 364 | + { |
| 365 | + $this->messages = $messages; |
| 366 | + } |
| 367 | + |
| 368 | +} |
0 commit comments