diff --git a/src/Generators/FactoryGenerator.php b/src/Generators/FactoryGenerator.php index 3060ddf5..0c0c7d3e 100644 --- a/src/Generators/FactoryGenerator.php +++ b/src/Generators/FactoryGenerator.php @@ -27,10 +27,7 @@ public function output(array $tree): array /** @var \Blueprint\Models\Model $model */ foreach ($tree['models'] as $model) { $path = $this->getPath($model); - $this->files->put( - $path, - $this->populateStub($stub, $model) - ); + $this->files->put($path, $this->populateStub($stub, $model)); $output['created'][] = $path; } @@ -67,12 +64,12 @@ protected function buildDefinition(Model $model) continue; } - if ($column->dataType() === 'id') { + if (in_array($column->dataType(), ['id', 'uuid'])) { $name = Str::beforeLast($column->name(), '_id'); $class = Str::studly($column->attributes()[0] ?? $name); $definition .= self::INDENT . "'{$column->name()}' => "; - $definition .= sprintf("factory(%s::class)", '\\' . $model->fullyQualifiedNamespace() . '\\' . $class); + $definition .= sprintf('factory(%s::class)', '\\' . $model->fullyQualifiedNamespace() . '\\' . $class); $definition .= ',' . PHP_EOL; } elseif (in_array($column->dataType(), ['enum', 'set']) and !empty($column->attributes())) { $definition .= self::INDENT . "'{$column->name()}' => "; @@ -98,6 +95,8 @@ protected function buildDefinition(Model $model) implode(', ', [$scale, 0, (str_repeat(9, $precision - $scale) . '.' . str_repeat(9, $scale))]), $definition ); + } elseif ($column->dataType() == 'json') { + $definition .= self::INDENT . "'{$column->name()}' => '[]'," . PHP_EOL; } else { $definition .= self::INDENT . "'{$column->name()}' => "; $faker = self::fakerData($column->name()) ?? self::fakerDataType($column->dataType()); @@ -158,11 +157,13 @@ public static function fakerDataType(string $type) 'text' => 'text', 'date' => 'date()', 'time' => 'time()', - 'guid' => 'word', + 'guid' => 'uuid', + 'uuid' => 'uuid', 'datetimetz' => 'dateTime()', 'datetime' => 'dateTime()', 'timestamp' => 'dateTime()', 'integer' => 'randomNumber()', + 'unsignedsmallinteger' => 'randomNumber()', 'bigint' => 'randomNumber()', 'smallint' => 'randomNumber()', 'decimal' => 'randomFloat(/** decimal_attributes **/)', diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index 0cea80b1..208a6932 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -30,10 +30,7 @@ public function output(array $tree): array /** @var \Blueprint\Models\Model $model */ foreach ($tree['models'] as $model) { $path = $this->getPath($model, $sequential_timestamp->addSecond()); - $this->files->put( - $path, - $this->populateStub($stub, $model) - ); + $this->files->put($path, $this->populateStub($stub, $model)); $output['created'][] = $path; } @@ -57,15 +54,19 @@ protected function buildDefinition(Model $model) /** @var \Blueprint\Models\Column $column */ foreach ($model->columns() as $column) { $dataType = $column->dataType(); - if ($column->name() === 'id') { + if ($column->name() === 'id' && $dataType != 'uuid') { $dataType = 'bigIncrements'; } elseif ($column->dataType() === 'id') { $dataType = 'unsignedBigInteger'; } + if ($column->dataType() === 'uuid') { + $dataType = 'uuid'; + } + $definition .= self::INDENT . '$table->' . $dataType . "('{$column->name()}'"; - if (!empty($column->attributes()) && $column->dataType() !== 'id') { + if (!empty($column->attributes()) && !in_array($column->dataType(), ['id', 'uuid'])) { $definition .= ', '; if (in_array($column->dataType(), ['set', 'enum'])) { $definition .= json_encode($column->attributes()); @@ -75,15 +76,27 @@ protected function buildDefinition(Model $model) } $definition .= ')'; + $foreign = ''; + foreach ($column->modifiers() as $modifier) { if (is_array($modifier)) { - $definition .= "->" . key($modifier) . "(" . current($modifier) . ")"; + if (key($modifier) == 'foreign') { + $foreign = + self::INDENT . + '$table->foreign(' . + "'{$column->name()}')->references('id')->on('" . + Str::lower(Str::plural(current($modifier))) . + "')->onDelete('cascade');" . + PHP_EOL; + } else { + $definition .= '->' . key($modifier) . '(' . current($modifier) . ')'; + } } else { $definition .= '->' . $modifier . '()'; } } - $definition .= ';' . PHP_EOL; + $definition .= ';' . PHP_EOL . $foreign; } if ($model->usesSoftDeletes()) { diff --git a/src/Generators/ModelGenerator.php b/src/Generators/ModelGenerator.php index d0313eec..dd5da989 100644 --- a/src/Generators/ModelGenerator.php +++ b/src/Generators/ModelGenerator.php @@ -27,10 +27,7 @@ public function output(array $tree): array /** @var \Blueprint\Models\Model $model */ foreach ($tree['models'] as $model) { $path = $this->getPath($model); - $this->files->put( - $path, - $this->populateStub($stub, $model) - ); + $this->files->put($path, $this->populateStub($stub, $model)); $output['created'][] = $path; } @@ -194,6 +191,10 @@ private function castForColumn(Column $column) return 'decimal'; } + if ($column->dataType() === 'json') { + return 'array'; + } + return null; } @@ -201,7 +202,7 @@ private function pretty_print_array(array $data, $assoc = true) { $output = var_export($data, true); $output = preg_replace('/^\s+/m', ' ', $output); - $output = preg_replace(['/^array\s\(/', "/\)$/"], ['[', ' ]'], $output); + $output = preg_replace(['/^array\s\(/', '/\)$/'], ['[', ' ]'], $output); if (!$assoc) { $output = preg_replace('/^(\s+)[^=]+=>\s+/m', '$1', $output); @@ -226,6 +227,7 @@ private function phpDataType(string $dataType) { static $php_data_types = [ 'id' => 'int', + 'uuid' => 'string', 'bigincrements' => 'int', 'biginteger' => 'int', 'boolean' => 'bool', diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php index 13bb575b..bb6062b1 100644 --- a/src/Lexers/ModelLexer.php +++ b/src/Lexers/ModelLexer.php @@ -86,13 +86,16 @@ class ModelLexer implements Lexer 'usecurrent' => 'useCurrent', 'always' => 'always', 'unique' => 'unique', + 'index' => 'index', + 'primary' => 'primary', + 'foreign' => 'foreign', ]; public function analyze(array $tokens): array { $registry = [ 'models' => [], - 'cache' => [] + 'cache' => [], ]; if (!empty($tokens['models'])) { @@ -107,7 +110,6 @@ public function analyze(array $tokens): array } } - return $registry; } @@ -155,7 +157,7 @@ private function buildModel(string $name, array $columns) $column = $this->buildColumn($name, $definition); $model->addColumn($column); - if ($column->name() !== 'id' && $column->dataType() === 'id') { + if ($column->name() !== 'id' && in_array($column->dataType(), ['id', 'uuid'])) { if ($column->attributes()) { $model->addRelationship('belongsTo', $column->attributes()[0] . ':' . $column->name()); } else {