createTool.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\command;
  13. use think\Exception;
  14. use think\console\Command;
  15. use think\console\Input;
  16. use think\console\Output;
  17. use think\console\input\Option;
  18. class createTool extends Command
  19. {
  20. public $config;
  21. public $log = '创建记录:'.PHP_EOL;
  22. protected function configure()
  23. {
  24. // 指令配置
  25. $this->setName('createTool')
  26. ->addArgument('table', Option::VALUE_REQUIRED, '表名称')
  27. ->addOption('path', 'p',Option::VALUE_REQUIRED, '创建路径,不填则创建在最外层')
  28. ->addOption('controller', 'c',Option::VALUE_OPTIONAL,"可选参数,需要创建的控制器:admin,mer,api,pc,ser")
  29. ->addOption('key', 'k',Option::VALUE_REQUIRED, '主键名称')
  30. ->setDescription('创建一个新的数据表全部模块:model,dao,repository,controller');
  31. }
  32. protected function execute(Input $input, Output $output)
  33. {
  34. $options['table'] = $input->getArgument('table');
  35. if (!preg_match('/^[a-z_]+$/', $options['table'])){
  36. $output->error('表名称格式不正确:仅包含小写字母和下划线');
  37. $output->error('例: php think createTool table_name -p system/merchant -c admin -k id');
  38. return;
  39. }
  40. $options['path'] = $input->getOption('path');
  41. $options['controller'] = null;
  42. if ($controller = $input->getOption('controller')) {
  43. $options['controller'] = explode(',',$controller);
  44. }
  45. $options['key'] = $input->getOption('key');
  46. $output->writeln('开始执行');
  47. $output->writeln('');
  48. $this->create($options);
  49. $output->writeln($this->log);
  50. $output->writeln('执行完成');
  51. }
  52. public function create($config)
  53. {
  54. $table = $config['table'];
  55. $prefix = env('database.prefix', '');
  56. $len = strlen($prefix);
  57. $_prefix = substr($table,0,$len);
  58. if ($prefix == $_prefix) {
  59. $table = substr($table,$len);
  60. }
  61. $config['table'] = $table;
  62. $class_name = str_replace(' ', '', ucwords(str_replace('_', ' ', $table)));
  63. $config['class_name'] = $class_name;
  64. $path = explode('/', $config['path']);
  65. $config['namespace_path'] = implode('\\',$path);
  66. $this->config = $config;
  67. ////生成model
  68. $this->createModel();
  69. //生成dao
  70. $this->createDao();
  71. //生成repository
  72. $this->createRepository();
  73. if ($this->config['controller']) {
  74. foreach ($this->config['controller'] as $c) {
  75. $this->createController($c);
  76. }
  77. }
  78. }
  79. public function createModel()
  80. {
  81. $file_path = app_path().'common/model/'.$this->config['path'];
  82. $file_name = $this->config['class_name'].'.php';
  83. $id = $this->config['key'] ?: $this->config['table'].'_id';
  84. $content = $this->getStart();
  85. $content .= "namespace app\common\model\\{$this->config['namespace_path']};
  86. use app\common\model\BaseModel;
  87. class {$this->config['class_name']} extends BaseModel
  88. {
  89. public static function tablePk(): ?string
  90. {
  91. return '{$id}';
  92. }
  93. public static function tableName(): string
  94. {
  95. return '{$this->config['table']}';
  96. }
  97. }
  98. ";
  99. $this->createFile($file_path, $file_name,$content);
  100. }
  101. public function createDao()
  102. {
  103. $file_path = app_path().'common/dao/'.$this->config['path'];
  104. $file_name = $this->config['class_name'].'Dao.php';
  105. $content = $this->getStart();
  106. $content .= "namespace app\common\dao\\{$this->config['namespace_path']};
  107. use app\common\dao\BaseDao;
  108. use app\common\model\\{$this->config['namespace_path']}\\{$this->config['class_name']};
  109. class {$this->config['class_name']}Dao extends BaseDao
  110. {
  111. protected function getModel(): string
  112. {
  113. return {$this->config['class_name']}::class;
  114. }
  115. }";
  116. $this->createFile($file_path, $file_name, $content);
  117. }
  118. public function createRepository()
  119. {
  120. $file_path = app_path().'common/repositories/'.$this->config['path'];
  121. $file_name = $this->config['class_name'].'Repository.php';
  122. $content = $this->getStart();
  123. $content .= "namespace app\common\\repositories\\{$this->config['namespace_path']};
  124. use app\common\\repositories\BaseRepository;
  125. use app\common\\dao\\{$this->config['namespace_path']}\\{$this->config['class_name']}Dao;
  126. class {$this->config['class_name']}Repository extends BaseRepository
  127. {
  128. public function __construct({$this->config['class_name']}Dao \$dao)
  129. {
  130. \$this->dao = \$dao;
  131. }
  132. }";
  133. $this->createFile($file_path, $file_name, $content);
  134. }
  135. public function createController($controller)
  136. {
  137. switch ($controller) {
  138. case "admin":
  139. $contr = 'admin';
  140. break;
  141. case "mer":
  142. $contr = 'merchant';
  143. break;
  144. case "api":
  145. $contr = 'api';
  146. break;
  147. case "pc":
  148. $contr = 'pc';
  149. break;
  150. case "ser":
  151. $contr = 'service';
  152. break;
  153. default:
  154. throw new Exception('控制器类型错误');
  155. }
  156. $file_path = app_path().'controller/'.$contr.'/'.$this->config['path'];
  157. $file_name = $this->config['class_name'].'.php';
  158. $content = $this->getStart();
  159. $content .= "namespace app\controller\\{$contr}\\{$this->config['namespace_path']};
  160. use think\App;
  161. use crmeb\basic\BaseController;
  162. use app\common\\repositories\\{$this->config['namespace_path']}\\{$this->config['class_name']}Repository;
  163. class {$this->config['class_name']} extends BaseController
  164. {
  165. protected \$repository;
  166. public function __construct(App \$app, {$this->config['class_name']}Repository \$repository)
  167. {
  168. parent::__construct(\$app);
  169. \$this->repository = \$repository;
  170. }
  171. }";
  172. $this->createFile($file_path, $file_name, $content);
  173. }
  174. public function createFile($path,$name, $content)
  175. {
  176. if (!is_dir($path)) {
  177. mkdir($path, 0777, true);
  178. }
  179. $file_path = $path.'/'.$name;
  180. if (file_exists($file_path)) {
  181. throw new \Exception('文件已存在:'.$file_path);
  182. }
  183. try{
  184. file_put_contents($file_path, $content);
  185. $this->log .= $file_path. PHP_EOL;
  186. return [true, '创建成功'];
  187. }catch (\Exception $exception) {
  188. throw new Exception($exception->getMessage());
  189. }
  190. }
  191. public function getStart()
  192. {
  193. $time = date('Y',time());
  194. $content = '<?php'.PHP_EOL.PHP_EOL;
  195. $content .= "// +----------------------------------------------------------------------
  196. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  197. // +----------------------------------------------------------------------
  198. // | Copyright (c) 2016~$time https://www.crmeb.com All rights reserved.
  199. // +----------------------------------------------------------------------
  200. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  201. // +----------------------------------------------------------------------
  202. // | Author: CRMEB Team <admin@crmeb.com>
  203. // +----------------------------------------------------------------------
  204. ".PHP_EOL;
  205. return $content;
  206. }
  207. }