ControllerMake.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace app\crud\make\make;
  3. use app\crud\make\ToAutoMake;
  4. use think\facade\App;
  5. use think\facade\Db;
  6. use think\console\Output;
  7. class ControllerMake implements ToAutoMake
  8. {
  9. public function check($controller, $path)
  10. {
  11. !defined('DS') && define('DS', DIRECTORY_SEPARATOR);
  12. $controller = ucfirst(camelize($controller));
  13. $controllerFilePath = base_path() . $path . DS . 'controller' . DS . $controller . '.php';
  14. if (!is_dir(base_path() . $path . DS . 'controller')) {
  15. mkdir(base_path() . $path . DS . 'controller', 0755, true);
  16. }
  17. if (file_exists($controllerFilePath)) {
  18. $output = new Output();
  19. $output->error("$controller.php已经存在");
  20. exit;
  21. }
  22. }
  23. public function make($controller, $path, $table)
  24. {
  25. $controllerTpl = dirname(dirname(__DIR__)) . '/tpl/controller.tpl';
  26. $tplContent = file_get_contents($controllerTpl);
  27. $controller = ucfirst(camelize($controller));
  28. $model = ucfirst(camelize($table));
  29. $filePath = empty($path) ? '' : DS . $path;
  30. $namespace = empty($path) ? '\\' : '\\' . $path . '\\';
  31. $prefix = config('database.connections.mysql.prefix');
  32. $column = Db::query('SHOW FULL COLUMNS FROM `' . $prefix . $table . '`');
  33. $pk = '';
  34. foreach ($column as $vo) {
  35. if ($vo['Key'] == 'PRI') {
  36. $pk = $vo['Field'];
  37. break;
  38. }
  39. }
  40. $wheremap = '';
  41. foreach ($column as $vo) {
  42. if ($vo['Field'] == 'delete_time') {
  43. $wheremap='$where[] = ["delete_time","=",0];';
  44. break;
  45. }
  46. }
  47. $add_column = get_cache('crud_a_'.$table);
  48. $edit_column = get_cache('crud_e_'.$table);
  49. $timeadd = '';
  50. $timeedit = '';
  51. foreach ($add_column as $key => $vo) {
  52. if($vo['type'] == 'datetime'){
  53. $timeadd.='if(isset($param["'.$vo['field'].'"])){
  54. ';
  55. $timeadd.='$param["'.$vo['field'].'"]= $param["'.$vo['field'].'"]?strtotime($param["'.$vo['field'].'"]):0;
  56. ';
  57. $timeadd.='}
  58. ';
  59. }
  60. }
  61. foreach ($edit_column as $key => $vo) {
  62. if($vo['type'] == 'datetime'){
  63. $timeedit.='if(isset($param["'.$vo['field'].'"])){
  64. ';
  65. $timeedit.='$param["'.$vo['field'].'"]= $param["'.$vo['field'].'"]?strtotime($param["'.$vo['field'].'"]):0;
  66. ';
  67. $timeedit.='}
  68. ';
  69. }
  70. }
  71. $tplContent = str_replace('<namespace>', $namespace, $tplContent);
  72. $tplContent = str_replace('<controller>', $controller, $tplContent);
  73. $tplContent = str_replace('<model>', $model, $tplContent);
  74. $tplContent = str_replace('<pk>', $pk, $tplContent);
  75. $tplContent = str_replace('<wheremap>', $wheremap, $tplContent);
  76. $tplContent = str_replace('<timeadd>', $timeadd, $tplContent);
  77. $tplContent = str_replace('<timeedit>', $timeedit, $tplContent);
  78. file_put_contents(base_path() . $filePath . DS . 'controller' . DS . $controller . '.php', $tplContent);
  79. /*
  80. // 检测BaseController是否存在
  81. if (!file_exists(App::getAppPath() . $filePath . DS . 'controller' . DS . 'BaseController.php')) {
  82. $controllerTpl = dirname(dirname(__DIR__)) . '/tpl/baseController.tpl';
  83. $tplContent = file_get_contents($controllerTpl);
  84. $tplContent = str_replace('<namespace>', $namespace, $tplContent);
  85. file_put_contents(base_path() . $filePath . DS . 'controller' . DS . 'BaseController.php', $tplContent);
  86. }
  87. */
  88. }
  89. }