MakeModel.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\common\command;
  4. // 引入框架内置类
  5. use think\console\Input;
  6. use think\console\Output;
  7. use think\console\input\Argument;
  8. use think\console\input\Option;
  9. use think\console\Command;
  10. use app\common\facade\ModelUtils;
  11. class MakeModel extends Command
  12. {
  13. protected $type = "Model";
  14. protected function configure()
  15. {
  16. $this->setName('makemodel')
  17. ->addArgument('name', Argument::OPTIONAL, "console name")
  18. ->setDescription('Create a new model class');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $name = trim($input->getArgument('name'));
  23. $classname = $this->getClassName($name);
  24. try {
  25. ModelUtils::makeModel($classname);
  26. $output->writeln('<info>' . $this->type . ':' . $classname . ' created successfully.</info>');
  27. } catch (\Exception $e) {
  28. $output->writeln('<error>' . $e->getMessage() . '</error>');
  29. }
  30. }
  31. protected function getClassName($name)
  32. {
  33. if (strpos($name, '\\') !== false) {
  34. return $name;
  35. }
  36. if (strpos($name, '@')) {
  37. [$app, $name] = explode('@', $name);
  38. } else {
  39. $app = '';
  40. }
  41. if (strpos($name, '/') !== false) {
  42. $name = str_replace('/', '\\', $name);
  43. }
  44. return $this->getNamespace($app) . '\\' . $name;
  45. }
  46. protected function getNamespace(string $app): string
  47. {
  48. return 'app' . ($app ? '\\' . $app : '') . '\\model';
  49. }
  50. }