Controller.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace crmeb\services\crud;
  14. use think\helper\Str;
  15. /**
  16. * 创建Controller
  17. * Class Controller
  18. * @author 等风来
  19. * @email 136327134@qq.com
  20. * @date 2023/3/13
  21. * @package crmeb\servives\crud
  22. */
  23. class Controller extends Make
  24. {
  25. /**
  26. * @var string
  27. */
  28. protected $name = 'controller';
  29. /**
  30. * @return string
  31. * @author 等风来
  32. * @email 136327134@qq.com
  33. * @date 2023/4/4
  34. */
  35. protected function setBaseDir(): string
  36. {
  37. return 'app' . DS . 'adminapi' . DS . 'controller' . DS . 'crud';
  38. }
  39. /**
  40. * @return Controller
  41. * @author 等风来
  42. * @email 136327134@qq.com
  43. * @date 2023/3/13
  44. */
  45. public function handle(string $name, array $options = [])
  46. {
  47. $path = $options['path'] ?? '';
  48. $contentPhp = '';
  49. $var = ["{%date%}", '{%validateName%}'];
  50. $fieldPhp = [$this->value['date'], $options['validateName'] ?? ''];
  51. $action = $options['action'] ?? [];
  52. $field = $options['field'] ?? [];
  53. if (!$action) {
  54. $action = ['index', 'create', 'save', 'edit', 'update', 'delete'];
  55. }
  56. if ($field) {
  57. $fieldStr = '';
  58. foreach ($field as $k) {
  59. $fieldStr .= $this->tab(3) . "['$k', ''],\n";
  60. }
  61. $fieldPhp[] = $fieldStr;
  62. }
  63. $res = false;
  64. foreach ($action as $item) {
  65. if (in_array($item, ['save', 'update'])) {
  66. $res = true;
  67. }
  68. [$class, $stub] = $this->getStubContent($name, $item);
  69. $contentPhp .= $stub . "\r\n";
  70. }
  71. if ($res) {
  72. $var[] = '{%field-php%}';
  73. }
  74. if ($var && $fieldPhp) {
  75. $contentPhp = str_replace($var, $fieldPhp, $contentPhp);
  76. }
  77. [$className, $contentController] = $this->getStubContent($name, 'controller');
  78. $this->value['nameCamel'] = Str::studly($name);
  79. $this->value['name'] = $className;
  80. $this->value['path'] = $this->getfolderPath($path);
  81. $this->value['content-php'] = $contentPhp;
  82. $this->value['use-php'] = "use " . str_replace('/', '\\', $options['usePath']) . "Services;\n";
  83. $contentStr = str_replace($this->var, $this->value, $contentController);
  84. $filePath = $this->getFilePathName($path, $this->value['nameCamel']);
  85. $this->usePath = $this->value['path'];
  86. $this->setPathname($filePath);
  87. $this->setContent($contentStr);
  88. return $this;
  89. }
  90. /**
  91. * 返回模板路径
  92. * @param string $type
  93. * @return mixed|string|string[]
  94. * @author 等风来
  95. * @email 136327134@qq.com
  96. * @date 2023/3/13
  97. */
  98. protected function getStub(string $type = 'controller')
  99. {
  100. $controllerPath = __DIR__ . DS . 'stubs' . DS . 'controller' . DS;
  101. $stubs = [
  102. 'index' => $controllerPath . 'index.stub',
  103. 'create' => $controllerPath . 'create.stub',
  104. 'save' => $controllerPath . 'save.stub',
  105. 'edit' => $controllerPath . 'edit.stub',
  106. 'update' => $controllerPath . 'update.stub',
  107. 'delete' => $controllerPath . 'delete.stub',
  108. 'controller' => $controllerPath . 'crudController.stub',
  109. ];
  110. return $type ? $stubs[$type] : $stubs;
  111. }
  112. /**
  113. * @param string $path
  114. * @param string $name
  115. * @return string
  116. * @author 等风来
  117. * @email 136327134@qq.com
  118. * @date 2023/3/13
  119. */
  120. protected function getFilePathName(string $path, string $name): string
  121. {
  122. $path = str_replace(['app\\', 'app/'], '', $path);
  123. $path = ltrim(str_replace('\\', '/', $path), '/');
  124. return $this->getBasePath($path) . $name . '.' . $this->fileMime;
  125. }
  126. }