123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?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 Exception;
- use Illuminate\Database\Schema\Blueprint;
- use Illuminate\Support\Facades\File;
- use Illuminate\Support\Facades\Schema as MigrationSchema;
- use Illuminate\Support\Str;
- /**
- * schema
- */
- class Schema extends Creator
- {
- /**
- * @var bool
- */
- protected bool $createdAt = true;
- /**
- * @var bool
- */
- protected bool $updatedAt = true;
- /**
- * @var bool
- */
- protected bool $deletedAt = true;
- /**
- * @var bool
- */
- protected bool $creatorId = true;
- /**
- * @var array
- */
- protected array $structures = [];
- /**
- * @param string $table
- * @param string $engine
- * @param string $charset
- * @param string $collection
- * @param string $comment
- */
- public function __construct(
- public readonly string $table,
- public readonly string $engine,
- public readonly string $charset,
- public readonly string $collection,
- public readonly string $comment
- ) {
- }
- /**
- * create
- *
- * @return string|bool
- * @throws Exception
- */
- public function create(): string|bool
- {
- if (! count($this->structures)) {
- return false;
- }
- if (MigrationSchema::hasTable($this->table)) {
- throw new Exception(sprintf('[%s] 表已经存在', $this->table));
- }
- try {
- $this->createTable();
- if (MigrationSchema::hasTable($this->table)) {
- return parent::create();
- }
- return false;
- } catch (Exception $e) {
- MigrationSchema::dropIfExists($this->table);
- throw new Exception("由于{$e->getMessage()}, 表[{$this->table}]创建失败");
- }
- }
- /**
- * get file
- *
- * @return string
- */
- public function getFile(): string
- {
- // TODO: Implement getFile() method.
- return CatchAdmin::getModuleMigrationPath($this->module).date('Y_m_d_his_').'create_'.$this->table.'.php';
- }
- /**
- * create table
- *
- * @throws Exception
- */
- protected function createTable(): void
- {
- MigrationSchema::create($this->table, function (Blueprint $table) {
- foreach ($this->structures as $structure) {
- // if field && type hava value
- if ($structure['type'] && $structure['field']) {
- if ($structure['type'] == 'string') {
- $column = $table->string($structure['field'], $structure['length'] ?: 255);
- } elseif ($structure['type'] == 'char') {
- $column = $table->char($structure['field'], $structure['length']);
- } else {
- $column = $table->{$structure['type']}($structure['field']);
- }
- $column = $column->nullable($structure['nullable']);
- if (is_null($structure['default'])) {
- } else {
- if (is_numeric($structure['default']) || mb_strlen($structure['default'])) {
- $column = $column->default($structure['default']);
- }
- }
- if ($structure['comment']) {
- $column = $column->comment($structure['comment']);
- }
- if ($structure['unique']) {
- $column->unique($structure['unique']);
- }
- }
- }
- if ($this->creatorId) {
- $table->creatorId();
- }
- if ($this->createdAt) {
- $table->createdAt();
- }
- if ($this->updatedAt) {
- $table->updatedAt();
- }
- if ($this->deletedAt) {
- $table->deletedAt();
- }
- $table->charset = $this->charset;
- $table->engine = $this->engine;
- $table->collation = $this->collection;
- $table->comment($this->comment);
- });
- }
- /**
- * get migration content
- *
- * @return string
- */
- public function getContent(): string
- {
- $stub = File::get($this->getStub());
- return Str::of($stub)->replace(['{method}','{table}', '{content}'], ['create', $this->table, $this->getMigrationContent()])->toString();
- }
- /**
- * get content
- *
- * @return string
- */
- public function getMigrationContent(): string
- {
- $content = Str::of('');
- foreach ($this->structures as $structure) {
- $begin = Str::of('$table->');
- $type = Str::of($structure['type']);
- if ($type->exactly('string')) {
- $begin = $begin->append(sprintf("string('%s'%s)", $structure['field'], $structure['length'] ? ", {$structure['length']}" : ''));
- } elseif ($type->exactly('char')) {
- $begin = $begin->append(sprintf("char('%s', %s)", $structure['field'], $structure['length']));
- } elseif ($type->exactly('id')) {
- $begin = $begin->append(Str::of($structure['field'])->exactly('id') ? 'id()' : sprintf("id('%s')", $structure['field']));
- } else {
- $begin = $begin->append(sprintf("%s('%s')", $structure['type'], $structure['field']));
- }
- $content = $content->append($begin)
- ->when($structure['nullable'], function ($str) {
- return $str->append('->nullable()');
- })
- ->when(isset($structure['default']), function ($str) use ($structure){
- $default = $structure['default'];
- if (is_numeric($default)) {
- $default = intval($default);
- return $str->append("->default({$default})");
- }
- if ($default) {
- return $str->append("->default('{$default}')");
- }
- return $str;
- })
- ->when($structure['unique'], function ($str) {
- return $str->append("->unique()");
- })
- ->when($structure['comment'], function ($str, $comment) {
- return $str->append("->comment('{$comment}')");
- })
- ->append(';')
- ->newLine();
- }
- if ($this->creatorId) {
- $content = $content->append(Str::of('$table->')->append('creatorId();'))->newLine();
- }
- if ($this->createdAt) {
- $content = $content->append(Str::of('$table->')->append('createdAt();'))->newLine();
- }
- if ($this->updatedAt) {
- $content = $content->append(Str::of('$table->')->append('updatedAt();'))->newLine();
- }
- if ($this->deletedAt) {
- $content = $content->append(Str::of('$table->')->append('deletedAt();'))->newLine();
- }
- return $content->newLine()
- ->append("\$table->engine='{$this->engine}'")
- ->append(';')
- ->newLine()
- ->append("\$table->comment('{$this->comment}')")
- ->append(';')
- ->toString();
- }
- /**
- * @param bool $createdAt
- * @return $this
- */
- public function setCreatedAt(bool $createdAt): static
- {
- $this->createdAt = $createdAt;
- return $this;
- }
- /**
- * @param bool $updatedAt
- * @return $this
- */
- public function setUpdatedAt(bool $updatedAt): static
- {
- $this->updatedAt = $updatedAt;
- return $this;
- }
- /**
- * @param bool $deletedAt
- * @return $this
- */
- public function setDeletedAt(bool $deletedAt): static
- {
- $this->deletedAt = $deletedAt;
- return $this;
- }
- /**
- * @param bool $creatorId
- * @return $this
- */
- public function setCreatorId(bool $creatorId): static
- {
- $this->creatorId = $creatorId;
- return $this;
- }
- /**
- * @param array $structures
- * @return $this
- */
- public function setStructures(array $structures): static
- {
- $this->structures = $structures;
- return $this;
- }
- /**
- * get stub
- *
- * @return string
- */
- protected function getStub(): string
- {
- return dirname(__DIR__).DIRECTORY_SEPARATOR.'stubs'.DIRECTORY_SEPARATOR.'migration.stub';
- }
- }
|