Command.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 刘志淳 <chun@engineer.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\command\make;
  12. use think\console\command\Make;
  13. use think\console\input\Argument;
  14. use think\facade\App;
  15. class Command extends Make
  16. {
  17. protected $type = "Command";
  18. protected function configure()
  19. {
  20. parent::configure();
  21. $this->setName('make:command')
  22. ->addArgument('commandName', Argument::OPTIONAL, "The name of the command")
  23. ->setDescription('Create a new command class');
  24. }
  25. protected function buildClass($name)
  26. {
  27. $commandName = $this->input->getArgument('commandName') ?: strtolower(basename($name));
  28. $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  29. $class = str_replace($namespace . '\\', '', $name);
  30. $stub = file_get_contents($this->getStub());
  31. return str_replace(['{%commandName%}', '{%className%}', '{%namespace%}', '{%app_namespace%}'], [
  32. $commandName,
  33. $class,
  34. $namespace,
  35. App::getNamespace(),
  36. ], $stub);
  37. }
  38. protected function getStub()
  39. {
  40. return __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'command.stub';
  41. }
  42. protected function getNamespace($appNamespace, $module)
  43. {
  44. return $appNamespace . '\\command';
  45. }
  46. }