|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Question\Question; |
|
10
|
|
|
use App\Common\Helper; |
|
11
|
|
|
use App\Console\Traits\CodeGenerate; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* GenerateSchemaCommand |
|
15
|
|
|
*/ |
|
16
|
|
|
class GenerateSchemaCommand extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
use CodeGenerate; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Configuration of command |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function configure() |
|
24
|
|
|
{ |
|
25
|
|
|
$this |
|
26
|
|
|
->setName('generate:schema') |
|
27
|
|
|
->setDescription('Command for generate schema') |
|
28
|
|
|
; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Execute method of command |
|
33
|
|
|
* |
|
34
|
|
|
* @param InputInterface $input |
|
35
|
|
|
* @param OutputInterface $output |
|
36
|
|
|
* |
|
37
|
|
|
* @return void |
|
38
|
|
|
* @throws \Exception |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
41
|
|
|
{ |
|
42
|
|
|
$output->writeln(['<comment>Welcome to the schema generator</comment>']); |
|
43
|
|
|
|
|
44
|
|
|
$helper = $this->getHelper('question'); |
|
45
|
|
|
$question = new Question('<info>Please enter table name: </info>'); |
|
46
|
|
|
$tableName = $helper->ask($input, $output, $question); |
|
47
|
|
|
$tableInfo = Capsule::schema()->getColumnListing($tableName); |
|
48
|
|
|
if (count($tableInfo) === 0) { |
|
49
|
|
|
$output->writeln([sprintf('<comment>Not found table %s</comment>', $tableName)]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$columns = $this->getColumnsInfo($tableInfo, $tableName); |
|
53
|
|
|
|
|
54
|
|
|
$modelName = substr($tableName, 0, -1); |
|
55
|
|
|
$className = Helper::underscoreToCamelCase($modelName, true).'Schema'; |
|
56
|
|
|
$baseName = $className.'.php'; |
|
57
|
|
|
$path = $this->getPath($baseName, SCHEMAS_PATH); |
|
58
|
|
|
$resourceType = str_replace('_', '-', strtolower($modelName)); |
|
59
|
|
|
|
|
60
|
|
|
$placeHolders = [ |
|
61
|
|
|
'<class>', |
|
62
|
|
|
'<resourceType>', |
|
63
|
|
|
'<resourceTypeInCamelCase>', |
|
64
|
|
|
'<attributes>', |
|
65
|
|
|
'<params>', |
|
66
|
|
|
'<attributesToClass>', |
|
67
|
|
|
]; |
|
68
|
|
|
$replacements = [ |
|
69
|
|
|
$className, |
|
70
|
|
|
str_replace('_', '-', strtolower($modelName)), |
|
71
|
|
|
Helper::dashesToCamelCase($resourceType, true), |
|
72
|
|
|
$this->generateAttributes($columns), |
|
73
|
|
|
$this->generateParams($columns), |
|
74
|
|
|
$this->generateAttributesToClass($columns), |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
$this->generateCode($placeHolders, $replacements, 'SchemaTemplate.tpl', $path); |
|
78
|
|
|
|
|
79
|
|
|
$output->writeln(sprintf('Generated new schema class to "<info>%s</info>"', realpath($path))); |
|
80
|
|
|
|
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array $tableInfo |
|
86
|
|
|
* @param string $tableName |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getColumnsInfo($tableInfo, $tableName) |
|
90
|
|
|
{ |
|
91
|
|
|
$columns = []; |
|
92
|
|
|
foreach ($tableInfo as $columnName) { |
|
93
|
|
|
$columnType = Capsule::schema()->getColumnType($tableName, $columnName); |
|
94
|
|
|
|
|
95
|
|
|
switch ($columnType) { |
|
96
|
|
|
case 'string': |
|
97
|
|
|
case 'text': |
|
98
|
|
|
$fake = '"String"'; |
|
99
|
|
|
break; |
|
100
|
|
|
case 'integer': |
|
101
|
|
|
$fake = '1'; |
|
102
|
|
|
break; |
|
103
|
|
|
case 'decimal': |
|
104
|
|
|
$fake = '1.0'; |
|
105
|
|
|
break; |
|
106
|
|
|
case 'datetime': |
|
107
|
|
|
$fake = '"2016-10-17T07:38:21+0000"'; |
|
108
|
|
|
break; |
|
109
|
|
|
default: |
|
110
|
|
|
$fake = ''; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$columns[] = [ |
|
114
|
|
|
'name' => $columnName, |
|
115
|
|
|
'type' => $columnType, |
|
116
|
|
|
'fake' => $fake, |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $columns; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param array $columns |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
private function generateAttributes($columns) |
|
128
|
|
|
{ |
|
129
|
|
|
$attributes = []; |
|
130
|
|
|
$counter = 1; |
|
131
|
|
|
foreach ($columns as $column) { |
|
132
|
|
|
if ($column['name'] === 'id') { |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
$counter++; |
|
136
|
|
|
if (count($columns) !== $counter) { |
|
137
|
|
|
$attributes[] = sprintf(' * "%s": %s,', $column['name'], $column['fake']); |
|
138
|
|
|
} else { |
|
139
|
|
|
$attributes[] = sprintf(' * "%s": %s', $column['name'], $column['fake']); |
|
140
|
|
|
} |
|
141
|
|
|
}; |
|
142
|
|
|
|
|
143
|
|
|
return implode("\n", $attributes); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param array $columns |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
|
|
private function generateParams($columns) |
|
151
|
|
|
{ |
|
152
|
|
|
$params = []; |
|
153
|
|
|
foreach ($columns as $column) { |
|
154
|
|
|
if ($column['name'] === 'id') { |
|
155
|
|
|
continue; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$params[] = sprintf(' * @apiParam {%s} %s', ucfirst($column['type']), $column['name']); |
|
159
|
|
|
}; |
|
160
|
|
|
|
|
161
|
|
|
return implode("\n", $params); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param array $columns |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
|
|
private function generateAttributesToClass($columns) |
|
169
|
|
|
{ |
|
170
|
|
|
$attributes = []; |
|
171
|
|
|
foreach ($columns as $column) { |
|
172
|
|
|
if ($column['name'] === 'id') { |
|
173
|
|
|
continue; |
|
174
|
|
|
} |
|
175
|
|
|
if ($column['type'] === 'datetime') { |
|
176
|
|
|
$attributes[] = sprintf("\t\t\t'%s'\t=> Carbon::parse(\$entity->%s)->setTimezone('UTC')->format(Carbon::ISO8601),", $column['name'], $column['name']); |
|
177
|
|
|
} else { |
|
178
|
|
|
$attributes[] = sprintf("\t\t\t'%s'\t=> (%s)\$entity->%s,", $column['name'], $column['type'], $column['name']); |
|
179
|
|
|
} |
|
180
|
|
|
}; |
|
181
|
|
|
|
|
182
|
|
|
return implode("\n", $attributes); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|