MakeCommandCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Webman\Console\Commands;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Question\ConfirmationQuestion;
  8. use Webman\Console\Util;
  9. class MakeCommandCommand extends Command
  10. {
  11. protected static $defaultName = 'make:command';
  12. protected static $defaultDescription = 'Make command';
  13. /**
  14. * @return void
  15. */
  16. protected function configure()
  17. {
  18. $this->addArgument('name', InputArgument::REQUIRED, 'Command name');
  19. }
  20. /**
  21. * @param InputInterface $input
  22. * @param OutputInterface $output
  23. * @return int
  24. */
  25. protected function execute(InputInterface $input, OutputInterface $output): int
  26. {
  27. $command = $name = trim($input->getArgument('name'));
  28. $output->writeln("Make command $name");
  29. // make:command 不支持子目录
  30. $name = str_replace(['\\', '/'], '', $name);
  31. if (!$command_str = Util::guessPath(app_path(), 'command')) {
  32. $command_str = Util::guessPath(app_path(), 'controller') === 'Controller' ? 'Command' : 'command';
  33. }
  34. $items= explode(':', $name);
  35. $name='';
  36. foreach ($items as $item) {
  37. $name.=ucfirst($item);
  38. }
  39. $file = app_path() . DIRECTORY_SEPARATOR . $command_str . DIRECTORY_SEPARATOR . "$name.php";
  40. $upper = $command_str === 'Command';
  41. $namespace = $upper ? 'App\Command' : 'app\command';
  42. if (is_file($file)) {
  43. $helper = $this->getHelper('question');
  44. $question = new ConfirmationQuestion("$file already exists. Do you want to override it? (yes/no)", false);
  45. if (!$helper->ask($input, $output, $question)) {
  46. return Command::SUCCESS;
  47. }
  48. }
  49. $this->createCommand($name, $namespace, $file, $command);
  50. return self::SUCCESS;
  51. }
  52. protected function getClassName($name)
  53. {
  54. return preg_replace_callback('/:([a-zA-Z])/', function ($matches) {
  55. return strtoupper($matches[1]);
  56. }, ucfirst($name)) . 'Command';
  57. }
  58. /**
  59. * @param $name
  60. * @param $namespace
  61. * @param $path
  62. * @return void
  63. */
  64. protected function createCommand($name, $namespace, $file, $command)
  65. {
  66. $path = pathinfo($file, PATHINFO_DIRNAME);
  67. if (!is_dir($path)) {
  68. mkdir($path, 0777, true);
  69. }
  70. $desc = str_replace(':', ' ', $command);
  71. $command_content = <<<EOF
  72. <?php
  73. namespace $namespace;
  74. use Symfony\Component\Console\Command\Command;
  75. use Symfony\Component\Console\Input\InputInterface;
  76. use Symfony\Component\Console\Input\InputOption;
  77. use Symfony\Component\Console\Input\InputArgument;
  78. use Symfony\Component\Console\Output\OutputInterface;
  79. class $name extends Command
  80. {
  81. protected static \$defaultName = '$command';
  82. protected static \$defaultDescription = '$desc';
  83. /**
  84. * @return void
  85. */
  86. protected function configure()
  87. {
  88. \$this->addArgument('name', InputArgument::OPTIONAL, 'Name description');
  89. }
  90. /**
  91. * @param InputInterface \$input
  92. * @param OutputInterface \$output
  93. * @return int
  94. */
  95. protected function execute(InputInterface \$input, OutputInterface \$output): int
  96. {
  97. \$name = \$input->getArgument('name');
  98. \$output->writeln('Hello $command');
  99. return self::SUCCESS;
  100. }
  101. }
  102. EOF;
  103. file_put_contents($file, $command_content);
  104. }
  105. }