ViewRouter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 think\App;
  16. use think\helper\Str;
  17. /**
  18. * Class ViewRouter
  19. * @author 等风来
  20. * @email 136327134@qq.com
  21. * @date 2023/4/1
  22. * @package crmeb\services\crud
  23. */
  24. class ViewRouter extends Make
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $name = 'router';
  30. /**
  31. * @var string
  32. */
  33. protected $fileMime = 'js';
  34. /**
  35. * ViewRouter constructor.
  36. * @param App $app
  37. */
  38. public function __construct(App $app)
  39. {
  40. parent::__construct($app);
  41. $this->basePath = $this->adminTemplatePath;
  42. }
  43. /**
  44. * @return string
  45. * @author 等风来
  46. * @email 136327134@qq.com
  47. * @date 2023/4/4
  48. */
  49. protected function setBaseDir(): string
  50. {
  51. return 'router' . DS . 'modules' . DS . 'crud';
  52. }
  53. /**
  54. * @param string $name
  55. * @param string $path
  56. * @param array $options
  57. * @return ViewRouter
  58. * @author 等风来
  59. * @email 136327134@qq.com
  60. * @date 2023/4/4
  61. */
  62. public function handle(string $name, array $options = [])
  63. {
  64. $path = $options['path'] ?? '';
  65. [$nameData, $content] = $this->getStubContent($name);
  66. $menus = $options['menuName'] ?? $name;
  67. $route = $options['route'] ?? Str::snake($name);
  68. $pagePath = $options['pagePath'] ?? Str::camel($name);
  69. if (!$route) {
  70. throw new CrudException(500045);
  71. }
  72. $this->value['MENUS'] = $menus;
  73. $this->value['NAME'] = $nameData;
  74. $this->value['ROUTE'] = $route;
  75. $this->value['PAGE_PATH'] = $pagePath;
  76. if (isset($this->value['PATH'])) {
  77. $this->value['PATH'] = $this->getfolderPath($path);
  78. }
  79. $contentStr = str_replace($this->var, $this->value, $content);
  80. $filePath = $this->getFilePathName($path, Str::camel($name));
  81. $this->setContent($contentStr);
  82. $this->setPathname($filePath);
  83. return $this;
  84. }
  85. /**
  86. * @param string $path
  87. * @param string $name
  88. * @return string
  89. * @author 等风来
  90. * @email 136327134@qq.com
  91. * @date 2023/4/3
  92. */
  93. protected function getFilePathName(string $path, string $name): string
  94. {
  95. $path = ltrim(str_replace('\\', '/', $path), '/');
  96. return $this->getBasePath($path) . $name . '.' . $this->fileMime;
  97. }
  98. /**
  99. * @param string $type
  100. * @return string
  101. * @author 等风来
  102. * @email 136327134@qq.com
  103. * @date 2023/4/1
  104. */
  105. protected function getStub(string $type = '')
  106. {
  107. return __DIR__ . DS . 'stubs' . DS . 'view' . DS . 'router' . DS . 'modules' . DS . 'crud.stub';
  108. }
  109. }