Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Blueprint\Contracts\Generator;
use Blueprint\Contracts\Lexer;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;

Expand All @@ -29,6 +30,11 @@ public static function appPath()
return str_replace('\\', '/', config('blueprint.app_path'));
}

public static function isLaravel8OrHigher()
{
return version_compare(App::version(), '8.0.0', '>=');
}

public function parse($content, $strip_dashes = true)
{
$content = str_replace(["\r\n", "\r"], "\n", $content);
Expand Down
36 changes: 15 additions & 21 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Models\Column;
use Blueprint\Models\Model;
use Blueprint\Tree;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Shift\Faker\Registry as FakerRegistry;

Expand All @@ -28,16 +28,15 @@ public function output(Tree $tree): array
{
$output = [];


if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$stub = $this->files->stub('factory.stub');
} else {
$stub = $this->files->stub('factory.closure.stub');
}

/** @var \Blueprint\Models\Model $model */
foreach ($tree->models() as $model) {
if (! $this->isLaravel8Up()) {
if (! Blueprint::isLaravel8OrHigher()) {
$this->addImport($model, 'Faker\Generator as Faker');
}
$this->addImport($model, $model->fullyQualifiedClassName());
Expand Down Expand Up @@ -75,7 +74,7 @@ protected function populateStub(string $stub, Model $model)
{
$stub = str_replace('{{ model }}', $model->name(), $stub);
$stub = str_replace('//', $this->buildDefinition($model), $stub);
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$stub = str_replace('use {{ namespacedModel }};', $this->buildImports($model), $stub);
} else {
$stub = str_replace('use Faker\Generator as Faker;'.PHP_EOL.'use {{ namespacedModel }};', $this->buildImports($model), $stub);
Expand Down Expand Up @@ -121,11 +120,11 @@ protected function buildDefinition(Model $model)

$class = Str::studly(Str::singular($table));

if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$this->addImport($model, $model->fullyQualifiedNamespace().'\\'.$class);
}
if ($key === 'id') {
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
$definition .= sprintf('%s::factory()', $class);
$definition .= ','.PHP_EOL;
Expand All @@ -135,7 +134,7 @@ protected function buildDefinition(Model $model)
$definition .= ','.PHP_EOL;
}
} else {
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
$definition .= sprintf('%s::factory()->create()->%s', $class, $key);
$definition .= ','.PHP_EOL;
Expand All @@ -151,7 +150,7 @@ protected function buildDefinition(Model $model)
$name = Str::beforeLast($column->name(), '_id');
$class = Str::studly($column->attributes()[0] ?? $name);

if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$this->addImport($model, $model->fullyQualifiedNamespace().'\\'.$class);
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
$definition .= sprintf('%s::factory()', $class);
Expand All @@ -162,7 +161,7 @@ protected function buildDefinition(Model $model)
$definition .= ','.PHP_EOL;
} elseif (in_array($column->dataType(), ['enum', 'set']) && ! empty($column->attributes())) {
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
$definition .= '$this->faker->'.$faker;
} else {
Expand All @@ -177,7 +176,7 @@ protected function buildDefinition(Model $model)
);
} elseif (in_array($column->dataType(), ['decimal', 'double', 'float'])) {
$faker = FakerRegistry::fakerData($column->name()) ?? FakerRegistry::fakerDataType($column->dataType());
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
$definition .= '$this->faker->'.$faker;
} else {
Expand All @@ -196,7 +195,7 @@ protected function buildDefinition(Model $model)
);
} elseif (in_array($column->dataType(), ['json', 'jsonb'])) {
$default = $column->defaultValue() ?? "'{}'";
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => {$default},".PHP_EOL;
} else {
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => {$default},".PHP_EOL;
Expand All @@ -205,15 +204,15 @@ protected function buildDefinition(Model $model)
if ($column->isNullable()) {
continue;
}
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= sprintf('%s%s => $this->faker->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_id'", FakerRegistry::fakerDataType('id'), PHP_EOL);
$definition .= sprintf('%s%s => $this->faker->%s,%s', str_repeat(self::INDENT, 3), "'{$column->name()}_type'", FakerRegistry::fakerDataType('string'), PHP_EOL);
} else {
$definition .= sprintf('%s%s => $faker->%s,%s', str_repeat(self::INDENT, 2), "'{$column->name()}_id'", FakerRegistry::fakerDataType('id'), PHP_EOL);
$definition .= sprintf('%s%s => $faker->%s,%s', str_repeat(self::INDENT, 2), "'{$column->name()}_type'", FakerRegistry::fakerDataType('string'), PHP_EOL);
}
} elseif ($column->dataType() === 'rememberToken') {
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
} else {
$this->addImport($model, 'Illuminate\Support\Str');
Expand All @@ -222,7 +221,7 @@ protected function buildDefinition(Model $model)
$definition .= 'Str::random(10)';
$definition .= ','.PHP_EOL;
} else {
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3)."'{$column->name()}' => ";
} else {
$definition .= str_repeat(self::INDENT, 2)."'{$column->name()}' => ";
Expand All @@ -238,7 +237,7 @@ protected function buildDefinition(Model $model)
$faker = 'word';
}

if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$definition .= '$this->faker->'.$faker;
} else {
$definition .= '$faker->'.$faker;
Expand Down Expand Up @@ -275,9 +274,4 @@ private function fillableColumns(array $columns): array
return ! in_array('nullable', $column->modifiers());
});
}

protected function isLaravel8Up()
{
return version_compare(App::version(), '8.0.0', '>=');
}
}
12 changes: 3 additions & 9 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Blueprint\Models\Column;
use Blueprint\Models\Model;
use Blueprint\Tree;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;

class ModelGenerator implements Generator
Expand All @@ -24,7 +23,7 @@ public function output(Tree $tree): array
{
$output = [];

if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$stub = $this->files->stub('model.class.stub');
} else {
$stub = $this->files->stub('model.class.no-factory.stub');
Expand Down Expand Up @@ -53,7 +52,7 @@ public function types(): array

protected function populateStub(string $stub, Model $model)
{
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$stub = str_replace('{{ namespace }}', $model->fullyQualifiedNamespace(), $stub);
$stub = str_replace(PHP_EOL.'class {{ class }}', $this->buildClassPhpDoc($model).PHP_EOL.'class {{ class }}', $stub);
$stub = str_replace('{{ class }}', $model->name(), $stub);
Expand Down Expand Up @@ -232,7 +231,7 @@ protected function addTraits(Model $model, $stub)
}

$stub = str_replace('use Illuminate\\Database\\Eloquent\\Model;', 'use Illuminate\\Database\\Eloquent\\Model;'.PHP_EOL.'use Illuminate\\Database\\Eloquent\\SoftDeletes;', $stub);
if ($this->isLaravel8Up()) {
if (Blueprint::isLaravel8OrHigher()) {
$stub = Str::replaceFirst('use HasFactory', 'use HasFactory, SoftDeletes', $stub);
} else {
$stub = Str::replaceFirst('{', '{'.PHP_EOL.' use SoftDeletes;'.PHP_EOL, $stub);
Expand Down Expand Up @@ -307,11 +306,6 @@ private function castForColumn(Column $column)
}
}

protected function isLaravel8Up()
{
return version_compare(App::version(), '8.0.0', '>=');
}

private function pretty_print_array(array $data, $assoc = true)
{
$output = var_export($data, true);
Expand Down
15 changes: 5 additions & 10 deletions src/Generators/SeederGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Blueprint\Generators;

use Blueprint\Blueprint;
use Blueprint\Contracts\Generator;
use Blueprint\Tree;
use Illuminate\Support\Facades\App;

class SeederGenerator implements Generator
{
Expand All @@ -31,7 +31,7 @@ public function output(Tree $tree): array

$output = [];

if ($this->isLaravel8OrHigher()) {
if (Blueprint::isLaravel8OrHigher()) {
$stub = $this->files->stub('seeder.stub');
} else {
$stub = $this->files->stub('seeder.no-factory.stub');
Expand All @@ -55,7 +55,7 @@ public function types(): array
protected function populateStub(string $stub, string $model)
{
$stub = str_replace('{{ class }}', $this->getClassName($model), $stub);
if ($this->isLaravel8OrHigher()) {
if (Blueprint::isLaravel8OrHigher()) {
$this->addImport($model, 'Illuminate\Database\Seeder');

$stub = str_replace('//', $this->build($model), $stub);
Expand All @@ -73,7 +73,7 @@ protected function getClassName(string $model)

protected function build(string $model)
{
if ($this->isLaravel8OrHigher()) {
if (Blueprint::isLaravel8OrHigher()) {
$this->addImport($model, $this->tree->fqcnForContext($model));
return sprintf('%s::factory()->times(5)->create();', class_basename($this->tree->fqcnForContext($model)));
}
Expand All @@ -97,15 +97,10 @@ private function addImport(string $model, $class)

private function getPath($model)
{
if ($this->isLaravel8OrHigher()) {
if (Blueprint::isLaravel8OrHigher()) {
return 'database/seeders/'.$model.'Seeder.php';
}

return 'database/seeds/'.$model.'Seeder.php';
}

protected function isLaravel8OrHigher()
{
return version_compare(App::version(), '8.0.0', '>=');
}
}
Loading