Make.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Argument;
  15. use think\console\Output;
  16. use think\facade\App;
  17. use think\facade\Config;
  18. use think\facade\Env;
  19. abstract class Make extends Command
  20. {
  21. protected $type;
  22. abstract protected function getStub();
  23. protected function configure()
  24. {
  25. $this->addArgument('name', Argument::REQUIRED, "The name of the class");
  26. }
  27. protected function execute(Input $input, Output $output)
  28. {
  29. $name = trim($input->getArgument('name'));
  30. $classname = $this->getClassName($name);
  31. $pathname = $this->getPathName($classname);
  32. if (is_file($pathname)) {
  33. $output->writeln('<error>' . $this->type . ' already exists!</error>');
  34. return false;
  35. }
  36. if (!is_dir(dirname($pathname))) {
  37. mkdir(dirname($pathname), 0755, true);
  38. }
  39. file_put_contents($pathname, $this->buildClass($classname));
  40. $output->writeln('<info>' . $this->type . ' created successfully.</info>');
  41. }
  42. protected function buildClass($name)
  43. {
  44. $stub = file_get_contents($this->getStub());
  45. $namespace = trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
  46. $class = str_replace($namespace . '\\', '', $name);
  47. return str_replace(['{%className%}', '{%actionSuffix%}', '{%namespace%}', '{%app_namespace%}'], [
  48. $class,
  49. Config::get('action_suffix'),
  50. $namespace,
  51. App::getNamespace(),
  52. ], $stub);
  53. }
  54. protected function getPathName($name)
  55. {
  56. $name = str_replace(App::getNamespace() . '\\', '', $name);
  57. return Env::get('app_path') . ltrim(str_replace('\\', '/', $name), '/') . '.php';
  58. }
  59. protected function getClassName($name)
  60. {
  61. $appNamespace = App::getNamespace();
  62. if (strpos($name, $appNamespace . '\\') !== false) {
  63. return $name;
  64. }
  65. if (Config::get('app_multi_module')) {
  66. if (strpos($name, '/')) {
  67. list($module, $name) = explode('/', $name, 2);
  68. } else {
  69. $module = 'common';
  70. }
  71. } else {
  72. $module = null;
  73. }
  74. if (strpos($name, '/') !== false) {
  75. $name = str_replace('/', '\\', $name);
  76. }
  77. return $this->getNamespace($appNamespace, $module) . '\\' . $name;
  78. }
  79. protected function getNamespace($appNamespace, $module)
  80. {
  81. return $module ? ($appNamespace . '\\' . $module) : $appNamespace;
  82. }
  83. }