Crud.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. namespace app\crud\command;
  14. use think\console\Command;
  15. use think\console\Input;
  16. use think\console\input\Option;
  17. use think\console\Output;
  18. class Crud extends Command
  19. {
  20. protected function configure()
  21. {
  22. $this->setName('auto crud')
  23. ->addOption('name', 'a', Option::VALUE_OPTIONAL, 'the name', null)
  24. ->addOption('module', 'm', Option::VALUE_OPTIONAL, 'the module name', null)
  25. ->addOption('table', 't', Option::VALUE_OPTIONAL, 'the table name', null)
  26. ->addOption('controller', 'c', Option::VALUE_OPTIONAL, 'the controller name', null)
  27. ->addOption('types', 'y', Option::VALUE_OPTIONAL, 'the types name', null)
  28. ->setDescription('auto make crud file');
  29. }
  30. protected function execute(Input $input, Output $output)
  31. {
  32. $name = $input->getOption('name');
  33. if (!$name) {
  34. $output->error("请输入 -na 名称");
  35. exit;
  36. }
  37. $module = $input->getOption('module');
  38. if (!$module) {
  39. $output->error("请输入 -m 模块名");
  40. exit;
  41. }
  42. $table = $input->getOption('table');
  43. if (!$table) {
  44. $output->error("请输入 -t 表名");
  45. exit;
  46. }
  47. $controller = $input->getOption('controller');
  48. if (!$controller) {
  49. $output->error("请输入 -c 控制器名");
  50. exit;
  51. }
  52. $types = $input->getOption('types');
  53. if (!$types) {
  54. $types = 1;
  55. }
  56. $this->make($name,$module,$table,$controller,$types);
  57. $output->info($name . "crud make success");
  58. }
  59. public function make($name,$module,$model,$controller,$types)
  60. {
  61. !defined('DS') && define('DS', DIRECTORY_SEPARATOR);
  62. // 使用DS常量和rtrim确保路径格式正确
  63. $crud_dir = rtrim(CMS_ROOT, DS) . DS . 'crud' . DS;
  64. // 统一使用DS常量拼接路径
  65. $dirs = ['view', 'controller', 'model', 'validate'];
  66. foreach($dirs as $dir) {
  67. $targetDir = $crud_dir . $dir;
  68. if (!is_dir($targetDir)) {
  69. mkdir($targetDir, 0755, true);
  70. }
  71. }
  72. $Bmodel = ucfirst(camelize($model));
  73. $Bcontroller = ucfirst(camelize($controller));
  74. // 修正路径定义,统一使用DS分隔符
  75. $crud = [
  76. ['name'=>'add','path'=>'view' . DS . $controller,'filename'=>'add.html'],
  77. ['name'=>'list','path'=>'view' . DS . $controller,'filename'=>'datalist.html'],
  78. ['name'=>'view','path'=>'view' . DS . $controller,'filename'=>'view.html'],
  79. ['name'=>'controller','path'=>'controller','filename'=>$Bcontroller.'.php'],
  80. ['name'=>'model','path'=>'model','filename'=>$Bmodel.'.php'],
  81. ['name'=>'validate','path'=>'validate','filename'=>$Bcontroller.'Validate.php'],
  82. ];
  83. foreach($crud as $k => $v){
  84. $tpl = dirname(__DIR__) . DS . 'tpl' . DS . $types . DS . $v['name'] . '.tpl';
  85. $tplContent = file_get_contents($tpl);
  86. $tplContent = str_replace('<module>', $module, $tplContent);
  87. $tplContent = str_replace('<controller>', $controller, $tplContent);
  88. $tplContent = str_replace('<Bcontroller>', $Bcontroller, $tplContent);
  89. $tplContent = str_replace('<model>', $model, $tplContent);
  90. $tplContent = str_replace('<Bmodel>', $Bmodel, $tplContent);
  91. $tplContent = str_replace('<name>', $name, $tplContent);
  92. $targetPath = $crud_dir . $v['path'];
  93. if (!is_dir($targetPath)) {
  94. mkdir($targetPath, 0755, true);
  95. }
  96. // 修正文件保存路径
  97. $filePath = $targetPath . DS . $v['filename'];
  98. file_put_contents($filePath, $tplContent);
  99. }
  100. }
  101. }