-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathSchemaFactoryMakeCommand.php
More file actions
150 lines (121 loc) · 5.25 KB
/
Copy pathSchemaFactoryMakeCommand.php
File metadata and controls
150 lines (121 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Vyuldashev\LaravelOpenApi\Console;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\BooleanType;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\DateType;
use Doctrine\DBAL\Types\DecimalType;
use Doctrine\DBAL\Types\IntegerType;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema as SchemaFacade;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;
class SchemaFactoryMakeCommand extends GeneratorCommand
{
protected $name = 'openapi:make-schema';
protected $description = 'Create a new Schema factory class';
protected $type = 'Schema';
protected function buildClass($name)
{
$output = parent::buildClass($name);
$output = str_replace('DummySchema', Str::replaceLast('Schema', '', class_basename($name)), $output);
if ($model = $this->option('model')) {
return $this->buildModel($output, $model);
}
return $output;
}
protected function buildModel($output, $model)
{
$appVersion = explode('.', app()::VERSION);
$namespace = $appVersion[0] >= 8 ? $this->laravel->getNamespace().'Models\\' : $this->laravel->getNamespace();
$model = Str::start($model, $namespace);
if (! is_a($model, Model::class, true)) {
throw new InvalidArgumentException('Invalid model');
}
/** @var Model $model */
$model = app($model);
$columns = SchemaFacade::connection($model->getConnectionName())->getColumnListing(config('database.connections.'.config('database.default').'.prefix',
'').$model->getTable());
$connection = $model->getConnection();
$definition = 'return Schema::object(\''.class_basename($model).'\')'.PHP_EOL;
$definition .= ' ->properties('.PHP_EOL;
$properties = collect($columns)
->map(static function ($column) use ($model, $connection) {
/** @var Column $column */
$column = $connection->getDoctrineColumn(config('database.connections.'.config('database.default').'.prefix',
'').$model->getTable(), $column);
$name = $column->getName();
$default = $column->getDefault();
$notNull = $column->getNotnull();
switch (get_class($column->getType())) {
case IntegerType::class:
$format = 'Schema::integer(%s)->default(%s)';
$args = [$name, $notNull ? (int) $default : null];
break;
case BooleanType::class:
$format = 'Schema::boolean(%s)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
case DateType::class:
$format = 'Schema::string(%s)->format(Schema::FORMAT_DATE)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
case DateTimeType::class:
$format = 'Schema::string(%s)->format(Schema::FORMAT_DATE_TIME)->default(%s)';
$args = [$name, $notNull ? $default : null];
break;
case DecimalType::class:
$format = 'Schema::number(%s)->format(Schema::FORMAT_FLOAT)->default(%s)';
$args = [$name, $notNull ? (float) $default : null];
break;
default:
$format = 'Schema::string(%s)->default(%s)';
$args = [$name, $default];
break;
}
$args = array_map(static function ($value) {
if ($value === null) {
return 'null';
}
if (is_numeric($value)) {
return $value;
}
return '\''.$value.'\'';
}, $args);
$indentation = str_repeat(' ', 4);
return sprintf($indentation.$format, ...$args);
})
->implode(','.PHP_EOL);
$definition .= $properties.PHP_EOL;
$definition .= ' );';
return str_replace('DummyDefinition', $definition, $output);
}
protected function getStub(): string
{
if ($this->option('model')) {
return __DIR__.'/stubs/schema.model.stub';
}
return __DIR__.'/stubs/schema.stub';
}
protected function getDefaultNamespace($rootNamespace): string
{
return $rootNamespace.'\OpenApi\Schemas';
}
protected function qualifyClass($name): string
{
$name = parent::qualifyClass($name);
if (Str::endsWith($name, 'Schema')) {
return $name;
}
return $name.'Schema';
}
protected function getOptions(): array
{
return [
['model', 'm', InputOption::VALUE_OPTIONAL, 'The model class schema being generated for'],
['force', null, InputOption::VALUE_NONE, 'Create the class even if the factory already exists'],
];
}
}