123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- // +----------------------------------------------------------------------
- // | CatchAdmin [Just Like ~ ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://catchadmin.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( https://github.com/JaguarJack/catchadmin-laravel/blob/master/LICENSE.md )
- // +----------------------------------------------------------------------
- // | Author: JaguarJack [ njphper@gmail.com ]
- // +----------------------------------------------------------------------
- declare(strict_types=1);
- namespace Modules\Develop\Support\Generate\Create;
- use Catch\CatchAdmin;
- use Illuminate\Database\Eloquent\Model as EloquentModel;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Schema as SchemaFacade;
- use Illuminate\Support\Str;
- class Model extends Creator
- {
- protected array $replace = [
- '{uses}',
- '{property}',
- '{namespace}',
- '{model}',
- '{traits}',
- '{table}',
- '{fillable}',
- '{searchable}',
- '{fieldsInList}',
- '{isPaginate}', '{form}'
- ];
- protected array $structures;
- protected bool $softDelete;
- /**
- * @param string $modelName
- * @param string $tableName
- * @param bool $isPaginate
- */
- public function __construct(
- protected string $modelName,
- protected readonly string $tableName,
- protected readonly bool $isPaginate
- ) {
- $model = new class () extends EloquentModel {
- use SoftDeletes;
- };
- $this->softDelete = in_array($model->getDeletedAtColumn(), SchemaFacade::getColumnListing($this->tableName));
- }
- /**
- * get file
- *
- * @return string
- */
- public function getFile(): string
- {
- // TODO: Implement getFile() method.
- return CatchAdmin::getModuleModelPath($this->module).$this->getModelName().$this->ext;
- }
- /**
- * get content
- *
- * @return string
- */
- public function getContent(): string
- {
- $modelStub = File::get($this->getModelStub());
- return Str::of($modelStub)->replace($this->replace, [$this->getUses(),
- $this->getProperties(),
- $this->getModelNamespace(),
- $this->getModelName(),
- $this->getTraits(),
- $this->tableName,
- $this->getFillable(),
- $this->getSearchable(),
- $this->getFieldsInList(),
- $this->isPaginate(),
- $this->getInForm()
- ])->toString();
- }
- /**
- * get model namespace
- *
- * @return string
- */
- public function getModelNamespace(): string
- {
- return Str::of(CatchAdmin::getModuleModelNamespace($this->module))->trim('\\')->append(';')->toString();
- }
- /**
- * get model name
- *
- * @return string
- */
- public function getModelName(): string
- {
- $modelName = Str::of($this->modelName);
- if (! $modelName->length()) {
- $modelName = Str::of($this->tableName)->camel();
- }
- return $modelName->ucfirst()->toString();
- }
- /**
- * get uses
- *
- * @return string
- */
- protected function getUses(): string
- {
- if (! $this->softDelete) {
- return <<<Text
- use Catch\Traits\DB\BaseOperate;
- use Catch\Traits\DB\ScopeTrait;
- use Catch\Traits\DB\Trans;
- use Illuminate\Database\Eloquent\Model;
- Text;
- } else {
- return <<<Text
- use Catch\Base\CatchModel as Model;
- Text;
- }
- }
- /**
- * get traits
- *
- * @return string
- */
- protected function getTraits(): string
- {
- return $this->softDelete ? '' : 'use BaseOperate, Trans, ScopeTrait;';
- }
- /**
- *
- * @return string
- */
- protected function getProperties(): string
- {
- $comment = Str::of('/**')->newLine();
- foreach ($this->getTableColumns() as $column) {
- $comment = $comment->append(sprintf(' * @property $%s', $column))->newLine();
- }
- return $comment->append('*/')->toString();
- }
- /**
- * get fillable
- *
- * @return string
- */
- protected function getFillable(): string
- {
- $fillable = Str::of('');
- foreach ($this->getTableColumns() as $column) {
- $fillable = $fillable->append(" '{$column}'")->append(',');
- }
- return $fillable->rtrim(',')->toString();
- }
- /**
- *
- * @return array
- */
- protected function getTableColumns(): array
- {
- return getTableColumns($this->tableName);
- }
- /**
- * get field in list
- *
- * @return string
- */
- protected function getFieldsInList(): string
- {
- $str = Str::of('');
- foreach ($this->structures as $structure) {
- if ($structure['list']) {
- $str = $str->append("'{$structure['field']}'")->append(',');
- }
- }
- return $str->rtrim(',')->toString();
- }
- /**
- * get field in list
- *
- * @return string
- */
- protected function getInForm(): string
- {
- $str = Str::of('');
- foreach ($this->structures as $structure) {
- if ($structure['form']) {
- $str = $str->append("'{$structure['field']}'")->append(',');
- }
- }
- return $str->rtrim(',')->toString();
- }
- /**
- * searchable
- *
- * @return string
- */
- protected function getSearchable(): string
- {
- $searchable = Str::of('');
- foreach ($this->structures as $structure) {
- if ($structure['search'] && $structure['field'] && $structure['search_op']) {
- $searchable = $searchable->append(sprintf("'%s' => '%s'", $structure['field'], $structure['search_op']))->append(',')->newLine();
- }
- }
- return $searchable->toString();
- }
- /**
- * @return string
- */
- protected function isPaginate(): string
- {
- return $this->isPaginate ? '' : Str::of('protected bool $isPaginate = false;')->toString();
- }
- /**
- * @param array $structures
- * @return $this
- */
- public function setStructures(array $structures): static
- {
- $this->structures = $structures;
- return $this;
- }
- /**
- * get stub
- *
- * @return string
- */
- protected function getModelStub(): string
- {
- return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'model.stub';
- }
- }
|