Command.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace addons\shopro\console;
  3. use think\console\Input;
  4. use think\console\Output;
  5. use think\console\Command as BaseCommand;
  6. class Command extends BaseCommand
  7. {
  8. protected $input = null;
  9. protected $output = null;
  10. protected $commonCb = null;
  11. /**
  12. * 执行帮助命令
  13. */
  14. protected function execute(Input $input, Output $output)
  15. {
  16. $this->input = $input;
  17. $this->output = $output;
  18. if ($this->commonCb && $this->commonCb instanceof \Closure) {
  19. ($this->commonCb)($input, $output);
  20. }
  21. $code = $input->getArgument('code');
  22. $code = $code ?: '1';
  23. $this->choose($code);
  24. }
  25. /**
  26. * 选择要执行的命令
  27. */
  28. public function choose($code)
  29. {
  30. $commands = $this->commands;
  31. $codes = array_column($commands, 'code');
  32. $names = array_column($commands, 'name');
  33. if (!in_array($code, $codes) && !in_array($code, $names)) {
  34. $this->output->writeln("已取消");
  35. return true;
  36. }
  37. $commands = array_column($commands, null, 'code');
  38. $name = isset($commands[$code]) ? $commands[$code]['name'] : $code;
  39. $name = \think\helper\Str::camel($name);
  40. if (method_exists($this, $name)) {
  41. $this->{$name}();
  42. } else {
  43. $this->cancel();
  44. }
  45. }
  46. /**
  47. * 取消操作
  48. */
  49. public function cancel()
  50. {
  51. $this->output->writeln("已取消");
  52. return true;
  53. }
  54. }