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