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