1 | <?php |
||
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) |
||
133 | |||
134 | /** |
||
135 | * @param array $columns |
||
136 | * @return string |
||
137 | */ |
||
138 | private function generateParams($columns) |
||
151 | |||
152 | /** |
||
153 | * @param array $columns |
||
154 | * @return string |
||
155 | */ |
||
156 | private function generateAttributesToClass($columns) |
||
172 | } |
||
173 |