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