Route.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 crmeb\exceptions\CrudException;
  15. use crmeb\services\crud\enum\ActionEnum;
  16. use think\helper\Str;
  17. class Route extends Make
  18. {
  19. /**
  20. * @var string
  21. */
  22. protected $name = 'route';
  23. /**
  24. * @return string
  25. * @author 等风来
  26. * @email 136327134@qq.com
  27. * @date 2023/4/4
  28. */
  29. protected function setBaseDir(): string
  30. {
  31. return 'app' . DS . 'adminapi' . DS . 'route' . DS . 'crud';
  32. }
  33. /**
  34. * @param string $name
  35. * @param array $options
  36. * @return Route
  37. */
  38. public function handle(string $name, array $options = [])
  39. {
  40. $path = $options['path'] ?? '';
  41. $route = $options['route'] ?? '';
  42. $controller = $options['controller'] ?? $name;
  43. $routePath = $options['routePath'] ?? '';
  44. $menus = $options['menus'] ?? '';
  45. if (!$route) {
  46. throw new CrudException(500045);
  47. }
  48. return $this->setRouteContent($route, $routePath, $controller, $menus)
  49. ->setRoute($name, $path);
  50. }
  51. /**
  52. * 设置路由模板内容
  53. * @param string $name
  54. * @param string $path
  55. * @return $this
  56. * @author 等风来
  57. * @email 136327134@qq.com
  58. * @date 2023/8/12
  59. */
  60. protected function setRoute(string $name, string $path)
  61. {
  62. $content = file_get_contents($this->getStub());
  63. $contentStr = str_replace($this->var, $this->value, $content);
  64. $filePath = $this->getFilePathName($path, strtolower($name));
  65. $this->setPathname($filePath);
  66. $this->setContent($contentStr);
  67. return $this;
  68. }
  69. /**
  70. * 设置路由页面内容
  71. * @param string $route
  72. * @param string $routePath
  73. * @param string $controller
  74. * @param string $menus
  75. * @return $this
  76. * @author 等风来
  77. * @email 136327134@qq.com
  78. * @date 2023/8/12
  79. */
  80. protected function setRouteContent(string $route, string $routePath, string $controller, string $menus)
  81. {
  82. $var = [
  83. '{%ROUTE%}',
  84. '{%CONTROLLER%}',
  85. '{%ROUTE_PATH%}',
  86. '{%MENUS%}',
  87. ];
  88. $value = [
  89. $route,
  90. $routePath,
  91. $controller ? ($routePath ? '.' : '') . Str::studly($controller) : '',
  92. $menus
  93. ];
  94. $routeContent = "";
  95. foreach (ActionEnum::ACTION_ALL as $item) {
  96. $routeContent .= file_get_contents($this->getStub($item)) . "\r\n";
  97. }
  98. $this->value['CONTENT_PHP'] = str_replace($var, $value, $routeContent);
  99. return $this;
  100. }
  101. /**
  102. * @param string $path
  103. * @param string $name
  104. * @return string
  105. * @author 等风来
  106. * @email 136327134@qq.com
  107. * @date 2023/4/11
  108. */
  109. protected function getFilePathName(string $path, string $name): string
  110. {
  111. $path = ltrim(str_replace('\\', '/', $path), '/');
  112. return $this->getBasePath($path) . $name . '.' . $this->fileMime;
  113. }
  114. /**
  115. * 设置模板
  116. * @param string $type
  117. * @return string|string[]
  118. * @author 等风来
  119. * @email 136327134@qq.com
  120. * @date 2023/3/14
  121. */
  122. protected function getStub(string $type = 'route')
  123. {
  124. $routePath = __DIR__ . DS . 'stubs' . DS . 'route' . DS;
  125. $stubs = [
  126. 'index' => $routePath . 'index.stub',
  127. 'create' => $routePath . 'create.stub',
  128. 'save' => $routePath . 'save.stub',
  129. 'edit' => $routePath . 'edit.stub',
  130. 'update' => $routePath . 'update.stub',
  131. 'status' => $routePath . 'status.stub',
  132. 'delete' => $routePath . 'delete.stub',
  133. 'route' => $routePath . 'route.stub',
  134. 'read' => $routePath . 'read.stub',
  135. ];
  136. return $type ? $stubs[$type] : $stubs;
  137. }
  138. }