Creator.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. namespace Modules\Develop\Support\Generate\Create;
  4. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  5. use Illuminate\Support\Facades\File;
  6. /**
  7. * creator
  8. */
  9. abstract class Creator
  10. {
  11. /**
  12. * @var string
  13. */
  14. protected string $ext = '.php';
  15. /**
  16. * @var string
  17. */
  18. protected string $module;
  19. /**
  20. * @var string
  21. */
  22. protected string $file;
  23. /**
  24. * create
  25. *
  26. * @return bool|string
  27. * @throws FileNotFoundException
  28. */
  29. public function create(): bool|string
  30. {
  31. return $this->put();
  32. }
  33. /**
  34. * the file which content put in
  35. *
  36. * @return string
  37. */
  38. abstract public function getFile(): string;
  39. /**
  40. * get content
  41. * @return string|bool
  42. */
  43. abstract public function getContent(): string|bool;
  44. /**
  45. * @return string|bool
  46. * @throws FileNotFoundException
  47. */
  48. protected function put(): string|bool
  49. {
  50. if (! $this->getContent()) {
  51. return false;
  52. }
  53. $this->file = $this->getFile();
  54. File::put($this->file, $this->getContent());
  55. if (File::exists($this->file)) {
  56. return $this->file;
  57. }
  58. throw new FileNotFoundException("create [$this->file] failed");
  59. }
  60. /**
  61. * set ext
  62. *
  63. * @param string $ext
  64. * @return $this
  65. */
  66. protected function setExt(string $ext): static
  67. {
  68. $this->ext = $ext;
  69. return $this;
  70. }
  71. /**
  72. * set module
  73. *
  74. * @param string $module
  75. * @return $this
  76. */
  77. public function setModule(string $module): static
  78. {
  79. $this->module = $module;
  80. return $this;
  81. }
  82. /**
  83. * get file
  84. *
  85. * @return string
  86. */
  87. public function getGenerateFile(): string
  88. {
  89. return $this->file;
  90. }
  91. }