Route.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Develop\Support\Generate\Create;
  4. use Catch\CatchAdmin;
  5. use Illuminate\Support\Facades\File;
  6. use Illuminate\Support\Str;
  7. /**
  8. * Route
  9. */
  10. class Route extends Creator
  11. {
  12. /**
  13. * @param string $controller
  14. */
  15. public function __construct(public readonly string $controller)
  16. {
  17. }
  18. /**
  19. * get file
  20. *
  21. * @return string
  22. */
  23. public function getFile(): string
  24. {
  25. return CatchAdmin::getModuleRoutePath($this->module);
  26. }
  27. /**
  28. * get content
  29. *
  30. * @return string
  31. */
  32. public function getContent(): string
  33. {
  34. // route 主要添加两个点
  35. // use Controller
  36. // 添加路由
  37. $route = Str::of('');
  38. $originContent = File::get(CatchAdmin::getModuleRoutePath($this->module));
  39. // 如果已经有 controller,就不再追加路由
  40. if (Str::of($originContent)->contains($this->getUserController())) {
  41. return $originContent;
  42. }
  43. File::lines(CatchAdmin::getModuleRoutePath($this->module))
  44. ->each(function ($line) use (&$route) {
  45. if (Str::of($line)->contains('Route::prefix')) {
  46. $route = $route->trim(PHP_EOL)
  47. ->newLine()
  48. ->append($this->getUserController())
  49. ->append(';')
  50. ->newLine(2)
  51. ->append($line)
  52. ->newLine();
  53. } else {
  54. $route = $route->append($line)->newLine();
  55. }
  56. });
  57. $apiResource = "Route::apiResource('{api}', {controller}::class);";
  58. return Str::of($route->toString())->replace(
  59. ['{module}', '//next'],
  60. [
  61. lcfirst($this->module),
  62. Str::of($apiResource)->replace(['{api}', '{controller}'], [$this->getApiString(), $this->getControllerName()])
  63. ->prepend("\t")
  64. ->prepend(PHP_EOL)
  65. ->newLine()->append("\t//next")]
  66. )->toString();
  67. }
  68. /**
  69. * get api
  70. *
  71. * @return string
  72. */
  73. public function getApiString(): string
  74. {
  75. return Str::of($this->getControllerName())->remove('Controller')->snake('_')->replace('_', '/')->toString();
  76. }
  77. /**
  78. * get api route
  79. *
  80. * @return string
  81. */
  82. public function getApiRute(): string
  83. {
  84. return lcfirst($this->module).'/'.$this->getApiString();
  85. }
  86. /**
  87. * use controller
  88. *
  89. * @return string
  90. */
  91. protected function getUserController(): string
  92. {
  93. return 'use '.CatchAdmin::getModuleControllerNamespace($this->module).$this->getControllerName();
  94. }
  95. /**
  96. * get controller name
  97. *
  98. * @return string
  99. */
  100. protected function getControllerName(): string
  101. {
  102. return Str::of($this->controller)->whenContains('Controller', function ($value) {
  103. return Str::of($value)->ucfirst();
  104. }, function ($value) {
  105. return Str::of($value)->append('Controller')->ucfirst();
  106. })->toString();
  107. }
  108. }