Base.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\command;
  3. use think\console\Input;
  4. use think\console\Output;
  5. use think\facade\Log;
  6. /**
  7. * 命令行基类,提供基本功能,继承的子类重写main方法即可
  8. * Class Base
  9. * @package app\command
  10. */
  11. class Base extends \think\console\Command
  12. {
  13. protected $input; //输入变量
  14. protected $output;//输出引用
  15. /**
  16. * 输出方法
  17. * @param $obj
  18. * @param string $type
  19. */
  20. protected function writeln($obj,$type="info"){
  21. $msg=print_r($obj,true);
  22. Log::record($msg,$type);
  23. $this->output->writeln("[".$type."]".$msg);
  24. }
  25. /**
  26. * 命令行核心方法,不要重写
  27. * @param Input $input
  28. * @param Output $output
  29. * @return int|void|null
  30. */
  31. protected function execute(Input $input, Output $output)
  32. {
  33. $this->input=$input;
  34. $this->output=$output;
  35. $this->main();
  36. }
  37. /**
  38. * 命令行主程序,继承后重写此方法,将业务写在此方法中
  39. */
  40. protected function main(){
  41. }
  42. }