Controller.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. class Controller extends Creator
  8. {
  9. /**
  10. * @var array
  11. */
  12. protected array $replace = [
  13. '{namespace}', '{uses}', '{controller}', '{model}', '{request}'
  14. ];
  15. /**
  16. * @param string $controller
  17. * @param string $model
  18. * @param string|null $request
  19. */
  20. public function __construct(
  21. public readonly string $controller,
  22. public readonly string $model,
  23. public readonly ?string $request = null
  24. ) {
  25. }
  26. /**
  27. * get file
  28. *
  29. * @return string
  30. */
  31. public function getFile(): string
  32. {
  33. // TODO: Implement getFile() method.
  34. return CatchAdmin::getModuleControllerPath($this->module).$this->getControllerName().$this->ext;
  35. }
  36. public function getContent(): string|bool
  37. {
  38. // TODO: Implement getContent() method.
  39. return Str::of(File::get($this->getControllerStub()))->replace($this->replace, [
  40. $this->getControllerNamespace(),
  41. $this->getUses(),
  42. $this->getControllerName(),
  43. $this->model,
  44. $this->request ?: 'Request'
  45. ])->toString();
  46. }
  47. /**
  48. * get controller name
  49. *
  50. * @return string
  51. */
  52. protected function getControllerName(): string
  53. {
  54. return Str::of($this->controller)->whenContains('Controller', function ($value) {
  55. return Str::of($value)->ucfirst();
  56. }, function ($value) {
  57. return Str::of($value)->append('Controller')->ucfirst();
  58. })->toString();
  59. }
  60. /**
  61. * get uses
  62. *
  63. * @return string
  64. */
  65. protected function getUses(): string
  66. {
  67. return Str::of('use ')
  68. ->append(CatchAdmin::getModuleModelNamespace($this->module).$this->model)
  69. ->append(';')
  70. ->newLine()
  71. ->append('use ')
  72. ->when($this->request, function ($str) {
  73. return $str->append(CatchAdmin::getModuleRequestNamespace($this->module).$this->request);
  74. }, function ($str) {
  75. return $str->append("Illuminate\Http\Request");
  76. })->append(';')->newLine()->toString();
  77. }
  78. /**
  79. * get controller stub
  80. *
  81. * @return string
  82. */
  83. protected function getControllerStub(): string
  84. {
  85. return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'controller.stub';
  86. }
  87. /**
  88. * get controller namespace
  89. *
  90. * @return string
  91. */
  92. protected function getControllerNamespace(): string
  93. {
  94. return Str::of(CatchAdmin::getModuleControllerNamespace($this->module))->rtrim('\\')->append(';')->toString();
  95. }
  96. }