|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Console; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
7
|
|
|
|
|
8
|
|
|
class FormCommand extends Command |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The console command name. |
|
12
|
|
|
* |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $name = 'admin:form'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The console command description. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $description = 'laravel-admin form filed generator'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Execute the console command. |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public function handle() |
|
30
|
|
|
{ |
|
31
|
|
|
$modelName = $this->option('model'); |
|
32
|
|
|
if (empty($modelName) || !class_exists($modelName)) { |
|
33
|
|
|
$this->error('Model does not exists !'); |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// use doctrine/dbal |
|
38
|
|
|
$model = $this->laravel->make($modelName); |
|
|
|
|
|
|
39
|
|
|
$table = $model->getConnection()->getTablePrefix() . $model->getTable(); |
|
40
|
|
|
$schema = $model->getConnection()->getDoctrineSchemaManager($table); |
|
41
|
|
|
|
|
42
|
|
|
if (!method_exists($schema, 'getDatabasePlatform')) { |
|
43
|
|
|
$this->error('You need to require doctrine/dbal: ~2.3 in your own composer.json to get database columns. '); |
|
44
|
|
|
$this->info('Using install command: composer require doctrine/dbal'); |
|
45
|
|
|
return false; |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// custom mapping the types that doctrine/dbal does not support |
|
50
|
|
|
$databasePlatform = $schema->getDatabasePlatform(); |
|
51
|
|
|
$databasePlatform->registerDoctrineTypeMapping('enum', 'string'); |
|
52
|
|
|
$databasePlatform->registerDoctrineTypeMapping('geometry', 'string'); |
|
53
|
|
|
$databasePlatform->registerDoctrineTypeMapping('geometrycollection', 'string'); |
|
54
|
|
|
$databasePlatform->registerDoctrineTypeMapping('linestring', 'string'); |
|
55
|
|
|
$databasePlatform->registerDoctrineTypeMapping('multilinestring', 'string'); |
|
56
|
|
|
$databasePlatform->registerDoctrineTypeMapping('multipoint', 'string'); |
|
57
|
|
|
$databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string'); |
|
58
|
|
|
$databasePlatform->registerDoctrineTypeMapping('point', 'string'); |
|
59
|
|
|
$databasePlatform->registerDoctrineTypeMapping('polygon', 'string'); |
|
60
|
|
|
$databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string'); |
|
61
|
|
|
$databasePlatform->registerDoctrineTypeMapping('multipolygon', 'string'); |
|
62
|
|
|
|
|
63
|
|
|
$database = null; |
|
64
|
|
|
if (strpos($table, '.')) { |
|
65
|
|
|
list($database, $table) = explode('.', $table); |
|
66
|
|
|
} |
|
67
|
|
|
$columns = $schema->listTableColumns($table, $database); |
|
68
|
|
|
|
|
69
|
|
|
$adminForm = ''; |
|
70
|
|
|
if ($columns) { |
|
71
|
|
|
foreach ($columns as $column) { |
|
72
|
|
|
$name = $column->getName(); |
|
73
|
|
|
if (in_array($name, ['id', 'created_at', 'deleted_at'])) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
$type = $column->getType()->getName(); |
|
77
|
|
|
$comment = $column->getComment(); |
|
78
|
|
|
$default = $column->getDefault(); |
|
79
|
|
|
|
|
80
|
|
|
// set column fieldType and defaultValue |
|
81
|
|
|
switch ($type) { |
|
82
|
|
|
case 'boolean': |
|
83
|
|
|
case 'bool': |
|
84
|
|
|
$fieldType = 'switch'; |
|
85
|
|
|
$defaultValue = $default; |
|
86
|
|
|
break; |
|
87
|
|
|
case 'json': |
|
88
|
|
|
case 'array': |
|
89
|
|
|
case 'object': |
|
90
|
|
|
$fieldType = 'text'; |
|
91
|
|
|
$defaultValue = $default; |
|
92
|
|
|
break; |
|
93
|
|
|
case 'string': |
|
94
|
|
|
switch ($name) { |
|
95
|
|
|
case $this->checkColumn($name, ['email']): |
|
96
|
|
|
$fieldType = 'email'; |
|
97
|
|
|
break; |
|
98
|
|
|
case $this->checkColumn($name, ['password', 'pwd']): |
|
99
|
|
|
$fieldType = 'password'; |
|
100
|
|
|
break; |
|
101
|
|
|
case $this->checkColumn($name, ['url', 'link', 'src', 'href']): |
|
102
|
|
|
$fieldType = 'url'; |
|
103
|
|
|
break; |
|
104
|
|
|
case $this->checkColumn($name, ['ip']): |
|
105
|
|
|
$fieldType = 'ip'; |
|
106
|
|
|
break; |
|
107
|
|
|
case $this->checkColumn($name, ['mobile', 'phone']): |
|
108
|
|
|
$fieldType = 'mobile'; |
|
109
|
|
|
break; |
|
110
|
|
|
case $this->checkColumn($name, ['color', 'rgb']): |
|
111
|
|
|
$fieldType = 'color'; |
|
112
|
|
|
break; |
|
113
|
|
|
case $this->checkColumn($name, ['image', 'img', 'avatar']) : |
|
|
|
|
|
|
114
|
|
|
$fieldType = 'image'; |
|
115
|
|
|
break; |
|
116
|
|
|
case $this->checkColumn($name, ['file', 'attachment']) : |
|
|
|
|
|
|
117
|
|
|
$fieldType = 'file'; |
|
118
|
|
|
break; |
|
119
|
|
|
default: |
|
120
|
|
|
$fieldType = 'text'; |
|
121
|
|
|
} |
|
122
|
|
|
$defaultValue = "'{$default}'"; |
|
123
|
|
|
break; |
|
124
|
|
|
case 'integer': |
|
125
|
|
|
case 'bigint': |
|
126
|
|
|
case 'smallint': |
|
127
|
|
|
case 'timestamp': |
|
128
|
|
|
$fieldType = 'number'; |
|
129
|
|
|
$defaultValue = $default; |
|
130
|
|
|
break; |
|
131
|
|
|
case 'decimal': |
|
132
|
|
|
case 'float': |
|
133
|
|
|
case 'real': |
|
134
|
|
|
$fieldType = 'decimal'; |
|
135
|
|
|
$defaultValue = $default; |
|
136
|
|
|
break; |
|
137
|
|
|
case 'datetime': |
|
138
|
|
|
$fieldType = 'datetime'; |
|
139
|
|
|
$defaultValue = "date('Y-m-d H:i:s')"; |
|
140
|
|
|
break; |
|
141
|
|
|
case 'date': |
|
142
|
|
|
$fieldType = 'date'; |
|
143
|
|
|
$defaultValue = "date('Y-m-d')"; |
|
144
|
|
|
break; |
|
145
|
|
|
case 'text': |
|
146
|
|
|
case 'blob': |
|
147
|
|
|
$fieldType = 'textarea'; |
|
148
|
|
|
$defaultValue = 'NULL'; |
|
149
|
|
|
break; |
|
150
|
|
|
default: |
|
151
|
|
|
$fieldType = 'text'; |
|
152
|
|
|
$defaultValue = "''"; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// set column comment |
|
156
|
|
|
$comment = $comment ? $comment : $name; |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
$adminForm .= "\$form->{$fieldType}('{$name}', '{$comment}')->default({$defaultValue});\n"; |
|
160
|
|
|
} |
|
161
|
|
|
$this->alert("laravel-admin form filed generator for {$modelName}:"); |
|
162
|
|
|
$this->info($adminForm); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Check if the table column contains the specified keywords of the array. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $haystack |
|
171
|
|
|
* @param array $needle |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
private function checkColumn(string $haystack, array $needle) |
|
175
|
|
|
{ |
|
176
|
|
|
foreach ($needle as $value) { |
|
177
|
|
|
if (strstr($haystack, $value) !== false) { |
|
178
|
|
|
return true; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
return false; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get the console command options. |
|
187
|
|
|
* |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function getOptions() |
|
191
|
|
|
{ |
|
192
|
|
|
return [ |
|
193
|
|
|
['model', null, InputOption::VALUE_REQUIRED, |
|
194
|
|
|
'The eloquent model that should be use as controller data source.',], |
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.