1 | <?php |
||
16 | class GenerateSchemaCommand extends Command |
||
17 | { |
||
18 | use CodeGenerate; |
||
19 | |||
20 | /** |
||
21 | * Configuration of command |
||
22 | */ |
||
23 | protected function configure() |
||
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) |
||
145 | |||
146 | /** |
||
147 | * @param array $columns |
||
148 | * @return string |
||
149 | */ |
||
150 | private function generateParams($columns) |
||
163 | |||
164 | /** |
||
165 | * @param array $columns |
||
166 | * @return string |
||
167 | */ |
||
168 | private function generateAttributesToClass($columns) |
||
184 | } |
||
185 |