AdminResourceGenerator.php
11.4 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 管理端类创建
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Console\Commands
* @package App\Console\Commands
* @author Richer <yangzi1028@163.com>
* @date 2020年7月7日 16:06:50
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Console\Commands;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
/**
* Class AdminResourceGenerator
*
* @category App\Console\Commands
* @package App\Console\Commands
* @author Richer <yangzi1028@163.com>
* @date 2020年7月7日 16:06:50
* @copyright 2021-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
class AdminResourceGenerator
{
/**
* 模型
*
* @var Model
*/
protected $model;
/**
* 定义字段
*
* @var array
*/
protected $formats = [
'form_field' => "\$form->%s('%s', __('%s'))",
'show_field' => "\$show->field('%s', __('%s'))",
'grid_column' => "\$grid->column('%s', __('%s'))",
];
/**
* 字段类型
*
* @var array
*/
private $_doctrineTypeMapping = [
'string' => [
'enum', 'geometry', 'geometrycollection', 'linestring',
'polygon', 'multilinestring', 'multipoint', 'multipolygon',
'point',
],
];
/**
* 字段类型映射
*
* @var array
*/
protected $fieldTypeMapping = [
'ip' => 'ip',
'email' => 'email|mail',
'password' => 'password|pwd',
'url' => 'url|link|src|href',
'mobile' => 'mobile|phone',
'color' => 'color|rgb',
'image' => 'image|img|avatar|pic|picture|cover',
'file' => 'file|attachment',
];
/**
* AdminResourceGenerator constructor.
*
* @param $model
*/
public function __construct($model)
{
$this->model = $this->getModel('App\Models\\'.ucfirst($model));
}
/**
* @param mixed $model
*
* @return mixed
*/
protected function getModel($model)
{
if ($model instanceof Model) {
return $model;
}
if (!class_exists($model) || !is_string($model) || !is_subclass_of($model, Model::class)) {
throw new \InvalidArgumentException("Invalid model [$model] !");
}
return new $model();
}
/**
* 渲染生成form表单
*
* @return string
* @throws \Exception
*/
public function generateForm()
{
$reservedColumns = $this->getReservedColumns();
$output = '';
foreach ($this->getTableColumns() as $column) {
$name = $column->getName();
if (in_array($name, $reservedColumns)) {
continue;
}
$type = $column->getType()->getName();
// 字段长度
$length = $column->getLength();
// 字段注释
// $comment = $column->getComment();
$comment = "__('$name')";
// 是否必填
$notnull = $column->getNotnull() === true ? 1 : 0;
$default = $column->getDefault();
$defaultValue = "'{$default}'";
// set column fieldType and defaultValue
switch ($type) {
case 'boolean':
case 'bool':
$output .= sprintf(" \$this->generateFormSwitchField(\$this->form, '%s', '%s', null, %s, %s)", $name, $comment, $defaultValue, $notnull);
break;
//case 'string':
// $fieldType = 'text';
// foreach ($this->fieldTypeMapping as $type => $regex) {
// if (preg_match("/^($regex)$/i", $name) !== 0) {
// $fieldType = $type;
// break;
// }
// }
// $defaultValue = "'{$default}'";
case 'text':
case 'blob':
case 'json':
case 'array':
case 'object':
$output .= sprintf(" \$this->generateFormTextAreaField(\$this->form, '%s', '%s', %s, 3, %s)", $name, $comment, $length, $notnull);
break;
case 'integer':
case 'bigint':
case 'smallint':
case 'timestamp':
$output .= sprintf(" \$this->generateFormNumberField(\$this->form, '%s', '%s', %s, 1, %s)", $name, $comment, $notnull, $defaultValue);
break;
case 'decimal':
case 'float':
case 'real':
$output .= sprintf(" \$this->generateFormCurrencyField(\$this->form, '%s', '%s', %s)", $name, $comment, $notnull);
break;
case 'datetime':
case 'date':
case 'time':
$output .= sprintf(" \$this->generateFormDateField(\$this->form, '%s', '%s', %s)", $name, $comment, $notnull);
break;
default:
$output .= sprintf(" \$this->generateFormTextField(\$this->form, '%s', '%s', %s, %s)", $name, $comment, $length, $notnull);
}
$output .= ";\r\n";
}
return $output;
}
/**
* 渲染生成详情
*
* @return string
* @throws \Exception
*/
public function generateShow()
{
$output = '';
foreach ($this->getTableColumns() as $column) {
$name = $column->getName();
// set column label
$label = $this->formatLabel($name);
$output .= sprintf($this->formats['show_field'], $name, $label);
$output .= ";\r\n";
}
return $output;
}
/**
* 渲染生成 grid
*
* @return string
* @throws \Exception
*/
public function generateGrid()
{
$output = '';
foreach ($this->getTableColumns() as $column) {
// 字段
$name = $column->getName();
// 字段类型
//$type = $column->getType();
$type = $column->getType()->getName();
// 字段长度
$length = $column->getLength();
// 字段注释
$comment = $column->getComment();
$comment = "__('$name')";
// 是否必填
$notnull = $column->getNotnull();
// set column fieldType and defaultValue
switch ($type) {
case 'boolean':
case 'bool':
$output .= sprintf(" \$this->generateGridSwitchField(\$this->grid, '%s', '%s')", $name, $comment);
break;
case 'integer':
case 'bigint':
case 'smallint':
$output .= sprintf(" \$this->generateGridNumberField(\$this->grid, '%s', '%s')", $name, $comment);
break;
case 'decimal':
case 'float':
case 'real':
$output .= sprintf(" \$this->generateGridCurrencyField(\$this->grid, '%s', '%s')", $name, $comment);
break;
case 'timestamp':
case 'datetime':
case 'date':
case 'time':
$output .= sprintf(" \$this->generateGridDateField(\$this->grid, '%s', '%s')", $name, $comment);
break;
default:
$output .= sprintf(" \$this->generateGridTextField(\$this->grid, '%s', '%s')", $name, $comment);
}
$output .= ";\r\n";
}
/*foreach ($this->getTableColumns() as $column) {
// 字段
$name = $column->getName();
// 字段类型
$type = $column->getType();
// 字段长度
$length = $column->getLength();
// 字段注释
$comment = $column->getComment();
$label = $this->formatLabel($name);
// 根据字段的类型来生成 字段
switch (class_basename($type)) {
// case 'BigIntType':
// case 'IntegerType':
// case 'StringType':
// case 'TextType':
// $output .= sprintf("\$this->generateGridTextField(\$this->grid, '%s', '%s')", $name, $comment);
// break;
case 'DateTimeType':
$output .= sprintf("\$this->generateGridDateField(\$this->grid, '%s', '%s')", $name, $comment);
break;
case 'DecimalType':
$output .= sprintf("\$this->generateGridCurrencyField(\$this->grid, '%s', '%s')", $name, $comment);
break;
default:
$output .= sprintf("\$this->generateGridTextField(\$this->grid, '%s', '%s')", $name, $comment);
break;
}
$output .= ";\r\n";
}*/
return $output;
}
protected function getReservedColumns()
{
return [
$this->model->getKeyName(),
$this->model->getCreatedAtColumn(),
$this->model->getUpdatedAtColumn(),
'deleted_at',
];
}
/**
* Get columns of a giving model.
*
* @throws \Exception
*
* @return \Doctrine\DBAL\Schema\Column[]
*/
protected function getTableColumns()
{
// update by Richer 于 2021年12月21日15:38:40 重新
$table = $this->model->getTable();
$columns = Schema::getColumnListing($table);
foreach ($columns as $column) {
$type = Schema::getColumnType($table, $column);
dump($type);
}
dd($columns);
if (!$this->model->getConnection()->isDoctrineAvailable()) {
throw new \Exception(
'You need to require doctrine/dbal: ~2.3 in your own composer.json to get database columns. '
);
}
$table = $this->model->getConnection()->getTablePrefix().$this->model->getTable();
$schema = $this->model->getConnection()->getDoctrineSchemaManager($table);
// custom mapping the types that doctrine/dbal does not support
$databasePlatform = $schema->getDatabasePlatform();
foreach ($this->_doctrineTypeMapping as $doctrineType => $dbTypes) {
foreach ($dbTypes as $dbType) {
$databasePlatform->registerDoctrineTypeMapping($dbType, $doctrineType);
}
}
$database = null;
if (strpos($table, '.')) {
list($database, $table) = explode('.', $table);
}
return $schema->listTableColumns($table, $database);
}
/**
* Format label.
*
* @param string $value
*
* @return string
*/
protected function formatLabel($value)
{
return ucfirst(str_replace(['-', '_'], ' ', $value));
}
}