Make.php 3.0 KB

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