Build.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\app\command;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\input\Argument;
  15. use think\console\Output;
  16. class Build extends Command
  17. {
  18. /**
  19. * 应用基础目录
  20. * @var string
  21. */
  22. protected $basePath;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function configure()
  27. {
  28. $this->setName('build')
  29. ->addArgument('app', Argument::OPTIONAL, 'app name .')
  30. ->setDescription('Build App Dirs');
  31. }
  32. protected function execute(Input $input, Output $output)
  33. {
  34. $this->basePath = $this->app->getBasePath();
  35. $app = $input->getArgument('app') ?: '';
  36. if (is_file($this->basePath . 'build.php')) {
  37. $list = include $this->basePath . 'build.php';
  38. } else {
  39. $list = [
  40. '__dir__' => ['controller', 'model', 'view'],
  41. ];
  42. }
  43. $this->buildApp($app, $list);
  44. $output->writeln("<info>Successed</info>");
  45. }
  46. /**
  47. * 创建应用
  48. * @access protected
  49. * @param string $app 应用名
  50. * @param array $list 目录结构
  51. * @return void
  52. */
  53. protected function buildApp(string $app, array $list = []): void
  54. {
  55. if (!is_dir($this->basePath . $app)) {
  56. // 创建应用目录
  57. mkdir($this->basePath . $app);
  58. }
  59. $appPath = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '');
  60. $namespace = 'app' . ($app ? '\\' . $app : '');
  61. // 创建配置文件和公共文件
  62. $this->buildCommon($app);
  63. // 创建模块的默认页面
  64. $this->buildHello($app, $namespace);
  65. foreach ($list as $path => $file) {
  66. if ('__dir__' == $path) {
  67. // 生成子目录
  68. foreach ($file as $dir) {
  69. $this->checkDirBuild($appPath . $dir);
  70. }
  71. } elseif ('__file__' == $path) {
  72. // 生成(空白)文件
  73. foreach ($file as $name) {
  74. if (!is_file($appPath . $name)) {
  75. file_put_contents($appPath . $name, 'php' == pathinfo($name, PATHINFO_EXTENSION) ? '<?php' . PHP_EOL : '');
  76. }
  77. }
  78. } else {
  79. // 生成相关MVC文件
  80. foreach ($file as $val) {
  81. $val = trim($val);
  82. $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.php';
  83. $space = $namespace . '\\' . $path;
  84. $class = $val;
  85. switch ($path) {
  86. case 'controller': // 控制器
  87. if ($this->app->config->get('route.controller_suffix')) {
  88. $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . 'Controller.php';
  89. $class = $val . 'Controller';
  90. }
  91. $content = "<?php" . PHP_EOL . "namespace {$space};" . PHP_EOL . PHP_EOL . "class {$class}" . PHP_EOL . "{" . PHP_EOL . PHP_EOL . "}";
  92. break;
  93. case 'model': // 模型
  94. $content = "<?php" . PHP_EOL . "namespace {$space};" . PHP_EOL . PHP_EOL . "use think\Model;" . PHP_EOL . PHP_EOL . "class {$class} extends Model" . PHP_EOL . "{" . PHP_EOL . PHP_EOL . "}";
  95. break;
  96. case 'view': // 视图
  97. $filename = $appPath . $path . DIRECTORY_SEPARATOR . $val . '.html';
  98. $this->checkDirBuild(dirname($filename));
  99. $content = '';
  100. break;
  101. default:
  102. // 其他文件
  103. $content = "<?php" . PHP_EOL . "namespace {$space};" . PHP_EOL . PHP_EOL . "class {$class}" . PHP_EOL . "{" . PHP_EOL . PHP_EOL . "}";
  104. }
  105. if (!is_file($filename)) {
  106. file_put_contents($filename, $content);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * 创建应用的欢迎页面
  114. * @access protected
  115. * @param string $app 目录
  116. * @param string $namespace 类库命名空间
  117. * @return void
  118. */
  119. protected function buildHello(string $app, string $namespace): void
  120. {
  121. $suffix = $this->app->config->get('route.controller_suffix') ? 'Controller' : '';
  122. $filename = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '') . 'controller' . DIRECTORY_SEPARATOR . 'Index' . $suffix . '.php';
  123. if (!is_file($filename)) {
  124. $content = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'controller.stub');
  125. $content = str_replace(['{%name%}', '{%app%}', '{%layer%}', '{%suffix%}'], [$app, $namespace, 'controller', $suffix], $content);
  126. $this->checkDirBuild(dirname($filename));
  127. file_put_contents($filename, $content);
  128. }
  129. }
  130. /**
  131. * 创建应用的公共文件
  132. * @access protected
  133. * @param string $app 目录
  134. * @return void
  135. */
  136. protected function buildCommon(string $app): void
  137. {
  138. $appPath = $this->basePath . ($app ? $app . DIRECTORY_SEPARATOR : '');
  139. if (!is_file($appPath . 'common.php')) {
  140. file_put_contents($appPath . 'common.php', "<?php" . PHP_EOL . "// 这是系统自动生成的公共文件" . PHP_EOL);
  141. }
  142. foreach (['event', 'middleware', 'common'] as $name) {
  143. if (!is_file($appPath . $name . '.php')) {
  144. file_put_contents($appPath . $name . '.php', "<?php" . PHP_EOL . "// 这是系统自动生成的{$name}定义文件" . PHP_EOL . "return [" . PHP_EOL . PHP_EOL . "];" . PHP_EOL);
  145. }
  146. }
  147. }
  148. /**
  149. * 创建目录
  150. * @access protected
  151. * @param string $dirname 目录名称
  152. * @return void
  153. */
  154. protected function checkDirBuild(string $dirname): void
  155. {
  156. if (!is_dir($dirname)) {
  157. mkdir($dirname, 0755, true);
  158. }
  159. }
  160. }