Model.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. declare(strict_types=1);
  12. namespace Modules\Develop\Support\Generate\Create;
  13. use Catch\CatchAdmin;
  14. use Illuminate\Database\Eloquent\Model as EloquentModel;
  15. use Illuminate\Database\Eloquent\SoftDeletes;
  16. use Illuminate\Support\Facades\File;
  17. use Illuminate\Support\Facades\Schema as SchemaFacade;
  18. use Illuminate\Support\Str;
  19. class Model extends Creator
  20. {
  21. protected array $replace = [
  22. '{uses}',
  23. '{property}',
  24. '{namespace}',
  25. '{model}',
  26. '{traits}',
  27. '{table}',
  28. '{fillable}',
  29. '{searchable}',
  30. '{fieldsInList}',
  31. '{isPaginate}', '{form}'
  32. ];
  33. protected array $structures;
  34. protected bool $softDelete;
  35. /**
  36. * @param string $modelName
  37. * @param string $tableName
  38. * @param bool $isPaginate
  39. */
  40. public function __construct(
  41. protected string $modelName,
  42. protected readonly string $tableName,
  43. protected readonly bool $isPaginate
  44. ) {
  45. $model = new class () extends EloquentModel {
  46. use SoftDeletes;
  47. };
  48. $this->softDelete = in_array($model->getDeletedAtColumn(), SchemaFacade::getColumnListing($this->tableName));
  49. }
  50. /**
  51. * get file
  52. *
  53. * @return string
  54. */
  55. public function getFile(): string
  56. {
  57. // TODO: Implement getFile() method.
  58. return CatchAdmin::getModuleModelPath($this->module).$this->getModelName().$this->ext;
  59. }
  60. /**
  61. * get content
  62. *
  63. * @return string
  64. */
  65. public function getContent(): string
  66. {
  67. $modelStub = File::get($this->getModelStub());
  68. return Str::of($modelStub)->replace($this->replace, [$this->getUses(),
  69. $this->getProperties(),
  70. $this->getModelNamespace(),
  71. $this->getModelName(),
  72. $this->getTraits(),
  73. $this->tableName,
  74. $this->getFillable(),
  75. $this->getSearchable(),
  76. $this->getFieldsInList(),
  77. $this->isPaginate(),
  78. $this->getInForm()
  79. ])->toString();
  80. }
  81. /**
  82. * get model namespace
  83. *
  84. * @return string
  85. */
  86. public function getModelNamespace(): string
  87. {
  88. return Str::of(CatchAdmin::getModuleModelNamespace($this->module))->trim('\\')->append(';')->toString();
  89. }
  90. /**
  91. * get model name
  92. *
  93. * @return string
  94. */
  95. public function getModelName(): string
  96. {
  97. $modelName = Str::of($this->modelName);
  98. if (! $modelName->length()) {
  99. $modelName = Str::of($this->tableName)->camel();
  100. }
  101. return $modelName->ucfirst()->toString();
  102. }
  103. /**
  104. * get uses
  105. *
  106. * @return string
  107. */
  108. protected function getUses(): string
  109. {
  110. if (! $this->softDelete) {
  111. return <<<Text
  112. use Catch\Traits\DB\BaseOperate;
  113. use Catch\Traits\DB\ScopeTrait;
  114. use Catch\Traits\DB\Trans;
  115. use Illuminate\Database\Eloquent\Model;
  116. Text;
  117. } else {
  118. return <<<Text
  119. use Catch\Base\CatchModel as Model;
  120. Text;
  121. }
  122. }
  123. /**
  124. * get traits
  125. *
  126. * @return string
  127. */
  128. protected function getTraits(): string
  129. {
  130. return $this->softDelete ? '' : 'use BaseOperate, Trans, ScopeTrait;';
  131. }
  132. /**
  133. *
  134. * @return string
  135. */
  136. protected function getProperties(): string
  137. {
  138. $comment = Str::of('/**')->newLine();
  139. foreach ($this->getTableColumns() as $column) {
  140. $comment = $comment->append(sprintf(' * @property $%s', $column))->newLine();
  141. }
  142. return $comment->append('*/')->toString();
  143. }
  144. /**
  145. * get fillable
  146. *
  147. * @return string
  148. */
  149. protected function getFillable(): string
  150. {
  151. $fillable = Str::of('');
  152. foreach ($this->getTableColumns() as $column) {
  153. $fillable = $fillable->append(" '{$column}'")->append(',');
  154. }
  155. return $fillable->rtrim(',')->toString();
  156. }
  157. /**
  158. *
  159. * @return array
  160. */
  161. protected function getTableColumns(): array
  162. {
  163. return getTableColumns($this->tableName);
  164. }
  165. /**
  166. * get field in list
  167. *
  168. * @return string
  169. */
  170. protected function getFieldsInList(): string
  171. {
  172. $str = Str::of('');
  173. foreach ($this->structures as $structure) {
  174. if ($structure['list']) {
  175. $str = $str->append("'{$structure['field']}'")->append(',');
  176. }
  177. }
  178. return $str->rtrim(',')->toString();
  179. }
  180. /**
  181. * get field in list
  182. *
  183. * @return string
  184. */
  185. protected function getInForm(): string
  186. {
  187. $str = Str::of('');
  188. foreach ($this->structures as $structure) {
  189. if ($structure['form']) {
  190. $str = $str->append("'{$structure['field']}'")->append(',');
  191. }
  192. }
  193. return $str->rtrim(',')->toString();
  194. }
  195. /**
  196. * searchable
  197. *
  198. * @return string
  199. */
  200. protected function getSearchable(): string
  201. {
  202. $searchable = Str::of('');
  203. foreach ($this->structures as $structure) {
  204. if ($structure['search'] && $structure['field'] && $structure['search_op']) {
  205. $searchable = $searchable->append(sprintf("'%s' => '%s'", $structure['field'], $structure['search_op']))->append(',')->newLine();
  206. }
  207. }
  208. return $searchable->toString();
  209. }
  210. /**
  211. * @return string
  212. */
  213. protected function isPaginate(): string
  214. {
  215. return $this->isPaginate ? '' : Str::of('protected bool $isPaginate = false;')->toString();
  216. }
  217. /**
  218. * @param array $structures
  219. * @return $this
  220. */
  221. public function setStructures(array $structures): static
  222. {
  223. $this->structures = $structures;
  224. return $this;
  225. }
  226. /**
  227. * get stub
  228. *
  229. * @return string
  230. */
  231. protected function getModelStub(): string
  232. {
  233. return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'model.stub';
  234. }
  235. }