| Total Complexity | 111 |
| Total Lines | 689 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
Complex classes like Console often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Console, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 49 | class Console |
||
| 50 | { |
||
| 51 | |||
| 52 | protected $app; |
||
| 53 | |||
| 54 | /** @var Command[] */ |
||
| 55 | protected $commands = []; |
||
| 56 | |||
| 57 | protected $wantHelps = false; |
||
| 58 | |||
| 59 | protected $catchExceptions = true; |
||
| 60 | protected $autoExit = true; |
||
| 61 | protected $definition; |
||
| 62 | protected $defaultCommand = 'list'; |
||
| 63 | |||
| 64 | protected $defaultCommands = [ |
||
| 65 | 'help' => Help::class, |
||
| 66 | 'list' => Lists::class, |
||
| 67 | 'build' => Build::class, |
||
| 68 | 'clear' => Clear::class, |
||
| 69 | 'make:command' => MakeCommand::class, |
||
| 70 | 'make:controller' => Controller::class, |
||
| 71 | 'make:model' => Model::class, |
||
| 72 | 'make:middleware' => Middleware::class, |
||
| 73 | 'make:validate' => Validate::class, |
||
| 74 | 'make:event' => Event::class, |
||
| 75 | 'make:listener' => Listener::class, |
||
| 76 | 'make:subscribe' => Subscribe::class, |
||
| 77 | 'optimize:config' => Config::class, |
||
| 78 | 'optimize:schema' => Schema::class, |
||
| 79 | 'optimize:route' => Route::class, |
||
| 80 | 'optimize:facade' => Facade::class, |
||
| 81 | 'run' => RunServer::class, |
||
| 82 | 'version' => Version::class, |
||
| 83 | 'route:list' => RouteList::class, |
||
| 84 | 'service:discover' => ServiceDiscover::class, |
||
| 85 | 'vendor:publish' => VendorPublish::class, |
||
| 86 | ]; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 启动器 |
||
| 90 | * @var array |
||
| 91 | */ |
||
| 92 | protected static $startCallbacks = []; |
||
| 93 | |||
| 94 | public function __construct(App $app) |
||
| 95 | { |
||
| 96 | $this->app = $app; |
||
| 97 | |||
| 98 | $this->app->initialize(); |
||
| 99 | |||
| 100 | $user = $this->app->config->get('console.user'); |
||
| 101 | |||
| 102 | if ($user) { |
||
| 103 | $this->setUser($user); |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->definition = $this->getDefaultInputDefinition(); |
||
| 107 | |||
| 108 | //加载指令 |
||
| 109 | $this->loadCommands(); |
||
| 110 | |||
| 111 | $this->start(); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * 添加初始化器 |
||
| 116 | * @param Closure $callback |
||
| 117 | */ |
||
| 118 | public static function starting(Closure $callback): void |
||
| 119 | { |
||
| 120 | static::$startCallbacks[] = $callback; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * 清空启动器 |
||
| 125 | */ |
||
| 126 | public static function flushStartCallbacks(): void |
||
| 127 | { |
||
| 128 | static::$startCallbacks = []; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * 启动 |
||
| 133 | */ |
||
| 134 | protected function start(): void |
||
| 135 | { |
||
| 136 | foreach (static::$startCallbacks as $callback) { |
||
| 137 | $callback($this); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * 设置执行用户 |
||
| 143 | * @param $user |
||
| 144 | */ |
||
| 145 | protected function setUser(string $user): void |
||
| 146 | { |
||
| 147 | if (extension_loaded('posix')) { |
||
| 148 | $user = posix_getpwnam($user); |
||
| 149 | |||
| 150 | if (!empty($user)) { |
||
| 151 | posix_setuid($user['uid']); |
||
| 152 | posix_setgid($user['gid']); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * 加载指令 |
||
| 159 | * @access protected |
||
| 160 | */ |
||
| 161 | protected function loadCommands(): void |
||
| 162 | { |
||
| 163 | $commands = $this->app->config->get('console.commands', []); |
||
| 164 | $commands = array_merge($this->defaultCommands, $commands); |
||
| 165 | |||
| 166 | $this->addCommands($commands); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @access public |
||
| 171 | * @param string $command |
||
|
1 ignored issue
–
show
|
|||
| 172 | * @param array $parameters |
||
|
1 ignored issue
–
show
|
|||
| 173 | * @param string $driver |
||
|
1 ignored issue
–
show
|
|||
| 174 | * @return Output|Buffer |
||
| 175 | */ |
||
| 176 | public function call(string $command, array $parameters = [], string $driver = 'buffer') |
||
| 177 | { |
||
| 178 | array_unshift($parameters, $command); |
||
| 179 | |||
| 180 | $input = new Input($parameters); |
||
| 181 | $output = new Output($driver); |
||
| 182 | |||
| 183 | $this->setCatchExceptions(false); |
||
| 184 | $this->find($command)->run($input, $output); |
||
| 185 | |||
| 186 | return $output; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * 执行当前的指令 |
||
| 191 | * @access public |
||
| 192 | * @return int |
||
| 193 | * @throws \Exception |
||
| 194 | * @api |
||
| 195 | */ |
||
| 196 | public function run() |
||
| 197 | { |
||
| 198 | $input = new Input(); |
||
| 199 | $output = new Output(); |
||
| 200 | |||
| 201 | $this->configureIO($input, $output); |
||
| 202 | |||
| 203 | try { |
||
| 204 | $exitCode = $this->doRun($input, $output); |
||
| 205 | } catch (\Exception $e) { |
||
| 206 | if (!$this->catchExceptions) { |
||
| 207 | throw $e; |
||
| 208 | } |
||
| 209 | |||
| 210 | $output->renderException($e); |
||
| 211 | |||
| 212 | $exitCode = $e->getCode(); |
||
| 213 | if (is_numeric($exitCode)) { |
||
| 214 | $exitCode = (int) $exitCode; |
||
| 215 | if (0 === $exitCode) { |
||
| 216 | $exitCode = 1; |
||
| 217 | } |
||
| 218 | } else { |
||
| 219 | $exitCode = 1; |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | if ($this->autoExit) { |
||
| 224 | if ($exitCode > 255) { |
||
| 225 | $exitCode = 255; |
||
| 226 | } |
||
| 227 | |||
| 228 | exit($exitCode); |
||
| 229 | } |
||
| 230 | |||
| 231 | return $exitCode; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * 执行指令 |
||
| 236 | * @access public |
||
| 237 | * @param Input $input |
||
|
1 ignored issue
–
show
|
|||
| 238 | * @param Output $output |
||
|
1 ignored issue
–
show
|
|||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | public function doRun(Input $input, Output $output) |
||
| 242 | { |
||
| 243 | if (true === $input->hasParameterOption(['--version', '-V'])) { |
||
| 244 | $output->writeln($this->getLongVersion()); |
||
| 245 | |||
| 246 | return 0; |
||
| 247 | } |
||
| 248 | |||
| 249 | $name = $this->getCommandName($input); |
||
| 250 | |||
| 251 | if (true === $input->hasParameterOption(['--help', '-h'])) { |
||
| 252 | if (!$name) { |
||
| 253 | $name = 'help'; |
||
| 254 | $input = new Input(['help']); |
||
| 255 | } else { |
||
| 256 | $this->wantHelps = true; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | if (!$name) { |
||
| 261 | $name = $this->defaultCommand; |
||
| 262 | $input = new Input([$this->defaultCommand]); |
||
| 263 | } |
||
| 264 | |||
| 265 | $command = $this->find($name); |
||
| 266 | |||
| 267 | return $this->doRunCommand($command, $input, $output); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * 设置输入参数定义 |
||
| 272 | * @access public |
||
| 273 | * @param InputDefinition $definition |
||
|
1 ignored issue
–
show
|
|||
| 274 | */ |
||
| 275 | public function setDefinition(InputDefinition $definition): void |
||
| 276 | { |
||
| 277 | $this->definition = $definition; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * 获取输入参数定义 |
||
| 282 | * @access public |
||
| 283 | * @return InputDefinition The InputDefinition instance |
||
| 284 | */ |
||
| 285 | public function getDefinition(): InputDefinition |
||
| 286 | { |
||
| 287 | return $this->definition; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets the help message. |
||
| 292 | * @access public |
||
| 293 | * @return string A help message. |
||
| 294 | */ |
||
| 295 | public function getHelp(): string |
||
| 296 | { |
||
| 297 | return $this->getLongVersion(); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * 是否捕获异常 |
||
| 302 | * @access public |
||
| 303 | * @param bool $boolean |
||
|
1 ignored issue
–
show
|
|||
| 304 | * @api |
||
| 305 | */ |
||
| 306 | public function setCatchExceptions(bool $boolean): void |
||
| 307 | { |
||
| 308 | $this->catchExceptions = $boolean; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * 是否自动退出 |
||
| 313 | * @access public |
||
| 314 | * @param bool $boolean |
||
|
1 ignored issue
–
show
|
|||
| 315 | * @api |
||
| 316 | */ |
||
| 317 | public function setAutoExit(bool $boolean): void |
||
| 318 | { |
||
| 319 | $this->autoExit = $boolean; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * 获取完整的版本号 |
||
| 324 | * @access public |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getLongVersion(): string |
||
| 328 | { |
||
| 329 | if ($this->app->version()) { |
||
| 330 | return sprintf('version <comment>%s</comment>', $this->app->version()); |
||
| 331 | } |
||
| 332 | |||
| 333 | return '<info>Console Tool</info>'; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * 添加指令集 |
||
| 338 | * @access public |
||
| 339 | * @param array $commands |
||
|
1 ignored issue
–
show
|
|||
| 340 | */ |
||
| 341 | public function addCommands(array $commands): void |
||
| 342 | { |
||
| 343 | foreach ($commands as $key => $command) { |
||
| 344 | if (is_subclass_of($command, Command::class)) { |
||
| 345 | // 注册指令 |
||
| 346 | $this->addCommand($command, is_numeric($key) ? '' : $key); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * 添加一个指令 |
||
| 353 | * @access public |
||
| 354 | * @param string|Command $command 指令对象或者指令类名 |
||
|
1 ignored issue
–
show
|
|||
| 355 | * @param string $name 指令名 留空则自动获取 |
||
|
1 ignored issue
–
show
|
|||
| 356 | * @return Command|void |
||
| 357 | */ |
||
| 358 | public function addCommand($command, string $name = '') |
||
| 359 | { |
||
| 360 | if ($name) { |
||
| 361 | $this->commands[$name] = $command; |
||
| 362 | return; |
||
| 363 | } |
||
| 364 | |||
| 365 | if (is_string($command)) { |
||
| 366 | $command = $this->app->invokeClass($command); |
||
| 367 | } |
||
| 368 | |||
| 369 | $command->setConsole($this); |
||
| 370 | |||
| 371 | if (!$command->isEnabled()) { |
||
| 372 | $command->setConsole(null); |
||
| 373 | return; |
||
| 374 | } |
||
| 375 | |||
| 376 | $command->setApp($this->app); |
||
| 377 | |||
| 378 | if (null === $command->getDefinition()) { |
||
| 379 | throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command))); |
||
| 380 | } |
||
| 381 | |||
| 382 | $this->commands[$command->getName()] = $command; |
||
| 383 | |||
| 384 | foreach ($command->getAliases() as $alias) { |
||
| 385 | $this->commands[$alias] = $command; |
||
| 386 | } |
||
| 387 | |||
| 388 | return $command; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * 获取指令 |
||
| 393 | * @access public |
||
| 394 | * @param string $name 指令名称 |
||
|
1 ignored issue
–
show
|
|||
| 395 | * @return Command |
||
| 396 | * @throws InvalidArgumentException |
||
| 397 | */ |
||
| 398 | public function getCommand(string $name): Command |
||
| 399 | { |
||
| 400 | if (!isset($this->commands[$name])) { |
||
| 401 | throw new InvalidArgumentException(sprintf('The command "%s" does not exist.', $name)); |
||
| 402 | } |
||
| 403 | |||
| 404 | $command = $this->commands[$name]; |
||
| 405 | |||
| 406 | if (is_string($command)) { |
||
| 407 | $command = $this->app->invokeClass($command); |
||
| 408 | /** @var Command $command */ |
||
| 409 | $command->setConsole($this); |
||
| 410 | $command->setApp($this->app); |
||
| 411 | } |
||
| 412 | |||
| 413 | if ($this->wantHelps) { |
||
| 414 | $this->wantHelps = false; |
||
| 415 | |||
| 416 | /** @var HelpCommand $helpCommand */ |
||
| 417 | $helpCommand = $this->getCommand('help'); |
||
| 418 | $helpCommand->setCommand($command); |
||
| 419 | |||
| 420 | return $helpCommand; |
||
| 421 | } |
||
| 422 | |||
| 423 | return $command; |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * 某个指令是否存在 |
||
| 428 | * @access public |
||
| 429 | * @param string $name 指令名称 |
||
|
1 ignored issue
–
show
|
|||
| 430 | * @return bool |
||
| 431 | */ |
||
| 432 | public function hasCommand(string $name): bool |
||
| 433 | { |
||
| 434 | return isset($this->commands[$name]); |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * 获取所有的命名空间 |
||
| 439 | * @access public |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | public function getNamespaces(): array |
||
| 443 | { |
||
| 444 | $namespaces = []; |
||
| 445 | foreach ($this->commands as $command) { |
||
| 446 | if (is_string($command)) { |
||
| 447 | $namespaces[] = $command; |
||
| 448 | } else { |
||
| 449 | $namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName())); |
||
| 450 | |||
| 451 | foreach ($command->getAliases() as $alias) { |
||
| 452 | $namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias)); |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | return array_values(array_unique(array_filter($namespaces))); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * 查找注册命名空间中的名称或缩写。 |
||
| 462 | * @access public |
||
| 463 | * @param string $namespace |
||
|
1 ignored issue
–
show
|
|||
| 464 | * @return string |
||
| 465 | * @throws InvalidArgumentException |
||
| 466 | */ |
||
| 467 | public function findNamespace(string $namespace): string |
||
| 468 | { |
||
| 469 | $allNamespaces = $this->getNamespaces(); |
||
| 470 | $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { |
||
| 471 | return preg_quote($matches[1]) . '[^:]*'; |
||
| 472 | }, $namespace); |
||
| 473 | $namespaces = preg_grep('{^' . $expr . '}', $allNamespaces); |
||
| 474 | |||
| 475 | if (empty($namespaces)) { |
||
| 476 | $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); |
||
| 477 | |||
| 478 | if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) { |
||
| 479 | if (1 == count($alternatives)) { |
||
| 480 | $message .= "\n\nDid you mean this?\n "; |
||
| 481 | } else { |
||
| 482 | $message .= "\n\nDid you mean one of these?\n "; |
||
| 483 | } |
||
| 484 | |||
| 485 | $message .= implode("\n ", $alternatives); |
||
| 486 | } |
||
| 487 | |||
| 488 | throw new InvalidArgumentException($message); |
||
| 489 | } |
||
| 490 | |||
| 491 | $exact = in_array($namespace, $namespaces, true); |
||
| 492 | if (count($namespaces) > 1 && !$exact) { |
||
| 493 | throw new InvalidArgumentException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces)))); |
||
| 494 | } |
||
| 495 | |||
| 496 | return $exact ? $namespace : reset($namespaces); |
||
| 497 | } |
||
| 498 | |||
| 499 | /** |
||
| 500 | * 查找指令 |
||
| 501 | * @access public |
||
| 502 | * @param string $name 名称或者别名 |
||
|
1 ignored issue
–
show
|
|||
| 503 | * @return Command |
||
| 504 | * @throws InvalidArgumentException |
||
| 505 | */ |
||
| 506 | public function find(string $name): Command |
||
| 507 | { |
||
| 508 | $allCommands = array_keys($this->commands); |
||
| 509 | |||
| 510 | $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { |
||
| 511 | return preg_quote($matches[1]) . '[^:]*'; |
||
| 512 | }, $name); |
||
| 513 | |||
| 514 | $commands = preg_grep('{^' . $expr . '}', $allCommands); |
||
| 515 | |||
| 516 | if (empty($commands) || count(preg_grep('{^' . $expr . '$}', $commands)) < 1) { |
||
| 517 | if (false !== $pos = strrpos($name, ':')) { |
||
| 518 | $this->findNamespace(substr($name, 0, $pos)); |
||
| 519 | } |
||
| 520 | |||
| 521 | $message = sprintf('Command "%s" is not defined.', $name); |
||
| 522 | |||
| 523 | if ($alternatives = $this->findAlternatives($name, $allCommands)) { |
||
| 524 | if (1 == count($alternatives)) { |
||
| 525 | $message .= "\n\nDid you mean this?\n "; |
||
| 526 | } else { |
||
| 527 | $message .= "\n\nDid you mean one of these?\n "; |
||
| 528 | } |
||
| 529 | $message .= implode("\n ", $alternatives); |
||
| 530 | } |
||
| 531 | |||
| 532 | throw new InvalidArgumentException($message); |
||
| 533 | } |
||
| 534 | |||
| 535 | $exact = in_array($name, $commands, true); |
||
| 536 | if (count($commands) > 1 && !$exact) { |
||
| 537 | $suggestions = $this->getAbbreviationSuggestions(array_values($commands)); |
||
| 538 | |||
| 539 | throw new InvalidArgumentException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions)); |
||
| 540 | } |
||
| 541 | |||
| 542 | return $this->getCommand($exact ? $name : reset($commands)); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * 获取所有的指令 |
||
| 547 | * @access public |
||
| 548 | * @param string $namespace 命名空间 |
||
|
1 ignored issue
–
show
|
|||
| 549 | * @return Command[] |
||
| 550 | * @api |
||
| 551 | */ |
||
| 552 | public function all(string $namespace = null): array |
||
| 553 | { |
||
| 554 | if (null === $namespace) { |
||
| 555 | return $this->commands; |
||
| 556 | } |
||
| 557 | |||
| 558 | $commands = []; |
||
| 559 | foreach ($this->commands as $name => $command) { |
||
| 560 | if ($this->extractNamespace($name, substr_count($namespace, ':') + 1) === $namespace) { |
||
| 561 | $commands[$name] = $command; |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | return $commands; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * 配置基于用户的参数和选项的输入和输出实例。 |
||
| 570 | * @access protected |
||
| 571 | * @param Input $input 输入实例 |
||
|
1 ignored issue
–
show
|
|||
| 572 | * @param Output $output 输出实例 |
||
|
1 ignored issue
–
show
|
|||
| 573 | */ |
||
| 574 | protected function configureIO(Input $input, Output $output): void |
||
| 575 | { |
||
| 576 | if (true === $input->hasParameterOption(['--ansi'])) { |
||
| 577 | $output->setDecorated(true); |
||
| 578 | } elseif (true === $input->hasParameterOption(['--no-ansi'])) { |
||
| 579 | $output->setDecorated(false); |
||
| 580 | } |
||
| 581 | |||
| 582 | if (true === $input->hasParameterOption(['--no-interaction', '-n'])) { |
||
| 583 | $input->setInteractive(false); |
||
| 584 | } |
||
| 585 | |||
| 586 | if (true === $input->hasParameterOption(['--quiet', '-q'])) { |
||
| 587 | $output->setVerbosity(Output::VERBOSITY_QUIET); |
||
| 588 | } elseif ($input->hasParameterOption('-vvv') || $input->hasParameterOption('--verbose=3') || $input->getParameterOption('--verbose') === 3) { |
||
| 589 | $output->setVerbosity(Output::VERBOSITY_DEBUG); |
||
| 590 | } elseif ($input->hasParameterOption('-vv') || $input->hasParameterOption('--verbose=2') || $input->getParameterOption('--verbose') === 2) { |
||
| 591 | $output->setVerbosity(Output::VERBOSITY_VERY_VERBOSE); |
||
| 592 | } elseif ($input->hasParameterOption('-v') || $input->hasParameterOption('--verbose=1') || $input->hasParameterOption('--verbose') || $input->getParameterOption('--verbose')) { |
||
| 593 | $output->setVerbosity(Output::VERBOSITY_VERBOSE); |
||
| 594 | } |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * 执行指令 |
||
| 599 | * @access protected |
||
| 600 | * @param Command $command 指令实例 |
||
|
1 ignored issue
–
show
|
|||
| 601 | * @param Input $input 输入实例 |
||
|
1 ignored issue
–
show
|
|||
| 602 | * @param Output $output 输出实例 |
||
|
1 ignored issue
–
show
|
|||
| 603 | * @return int |
||
| 604 | * @throws \Exception |
||
| 605 | */ |
||
| 606 | protected function doRunCommand(Command $command, Input $input, Output $output) |
||
| 607 | { |
||
| 608 | return $command->run($input, $output); |
||
| 609 | } |
||
| 610 | |||
| 611 | /** |
||
| 612 | * 获取指令的基础名称 |
||
| 613 | * @access protected |
||
| 614 | * @param Input $input |
||
|
1 ignored issue
–
show
|
|||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | protected function getCommandName(Input $input): string |
||
| 618 | { |
||
| 619 | return $input->getFirstArgument() ?: ''; |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * 获取默认输入定义 |
||
| 624 | * @access protected |
||
| 625 | * @return InputDefinition |
||
| 626 | */ |
||
| 627 | protected function getDefaultInputDefinition(): InputDefinition |
||
| 628 | { |
||
| 629 | return new InputDefinition([ |
||
| 630 | new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'), |
||
| 631 | new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'), |
||
| 632 | new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this console version'), |
||
| 633 | new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'), |
||
| 634 | new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'), |
||
| 635 | new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'), |
||
| 636 | new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'), |
||
| 637 | new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'), |
||
| 638 | ]); |
||
| 639 | } |
||
| 640 | |||
| 641 | /** |
||
| 642 | * 获取可能的建议 |
||
| 643 | * @access private |
||
| 644 | * @param array $abbrevs |
||
|
1 ignored issue
–
show
|
|||
| 645 | * @return string |
||
| 646 | */ |
||
| 647 | private function getAbbreviationSuggestions(array $abbrevs): string |
||
| 648 | { |
||
| 649 | return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : ''); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * 返回命名空间部分 |
||
| 654 | * @access public |
||
| 655 | * @param string $name 指令 |
||
|
1 ignored issue
–
show
|
|||
| 656 | * @param int $limit 部分的命名空间的最大数量 |
||
|
1 ignored issue
–
show
|
|||
| 657 | * @return string |
||
| 658 | */ |
||
| 659 | public function extractNamespace(string $name, int $limit = 0): string |
||
| 660 | { |
||
| 661 | $parts = explode(':', $name); |
||
| 662 | array_pop($parts); |
||
| 663 | |||
| 664 | return implode(':', 0 === $limit ? $parts : array_slice($parts, 0, $limit)); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * 查找可替代的建议 |
||
| 669 | * @access private |
||
| 670 | * @param string $name |
||
|
1 ignored issue
–
show
|
|||
| 671 | * @param array|\Traversable $collection |
||
|
1 ignored issue
–
show
|
|||
| 672 | * @return array |
||
| 673 | */ |
||
| 674 | private function findAlternatives(string $name, $collection): array |
||
| 716 | } |
||
| 717 | |||
| 718 | /** |
||
| 719 | * 返回所有的命名空间 |
||
| 720 | * @access private |
||
| 721 | * @param string $name |
||
|
1 ignored issue
–
show
|
|||
| 722 | * @return array |
||
| 723 | */ |
||
| 724 | private function extractAllNamespaces(string $name): array |
||
| 725 | { |
||
| 726 | $parts = explode(':', $name, -1); |
||
| 727 | $namespaces = []; |
||
| 728 | |||
| 729 | foreach ($parts as $part) { |
||
| 730 | if (count($namespaces)) { |
||
| 731 | $namespaces[] = end($namespaces) . ':' . $part; |
||
| 732 | } else { |
||
| 733 | $namespaces[] = $part; |
||
| 734 | } |
||
| 735 | } |
||
| 736 | |||
| 737 | return $namespaces; |
||
| 738 | } |
||
| 739 | |||
| 740 | } |
||
| 741 |