1 | <?php |
||
18 | class CreateMigrationCommand extends AbstractCommand |
||
19 | { |
||
20 | /** |
||
21 | * Checks whether the command is enabled or not in the current environment. |
||
22 | * |
||
23 | * @return boolean |
||
24 | */ |
||
25 | 3 | public function isEnabled() |
|
29 | |||
30 | /** |
||
31 | * Sets the configurations of the specified command. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | 33 | protected function configure() |
|
50 | |||
51 | /** |
||
52 | * Executes the command. |
||
53 | * |
||
54 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
55 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
56 | * @return object|\Symfony\Component\Console\Output\OutputInterface |
||
57 | */ |
||
58 | 27 | protected function execute(InputInterface $input, OutputInterface $output) |
|
59 | { |
||
60 | 27 | $fileName = $this->getFileName($input->getArgument('name'), $input->getOption('sequential')); |
|
61 | 27 | $keywords = $this->getKeywords($input->getArgument('name')); |
|
62 | |||
63 | 27 | $data = $this->prepareData($input, $keywords); |
|
64 | |||
65 | 27 | if ($input->getOption('from-database') && $data['command_name'] != 'create') { |
|
66 | 3 | $message = '--from-database is only available to create_*table*_table keyword'; |
|
67 | |||
68 | 3 | throw new \InvalidArgumentException($message); |
|
69 | } |
||
70 | |||
71 | 24 | if ($data['command_name'] != 'create') { |
|
72 | 6 | $data = $this->defineColumns($input, $keywords, $data); |
|
73 | 6 | } |
|
74 | |||
75 | 24 | $rendered = $this->renderer->render('Migration.twig', $data); |
|
76 | |||
77 | 24 | $this->filesystem->write('application/migrations/' . $fileName . '.php', $rendered); |
|
78 | |||
79 | 24 | return $output->writeln('<info>"' . $fileName . '" has been created.</info>'); |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Defines the columns to be included in the migration. |
||
84 | * |
||
85 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
86 | * @param array $keywords |
||
87 | * @param array $data |
||
88 | * @return array |
||
89 | */ |
||
90 | 6 | protected function defineColumns(InputInterface $input, array $keywords, array $data) |
|
91 | { |
||
92 | 6 | $data['table_name'] = $keywords[3]; |
|
93 | |||
94 | 6 | array_push($data['columns'], $this->setColumn($input, $keywords[1])); |
|
95 | |||
96 | 6 | if ($data['command_name'] == 'modify') { |
|
97 | 3 | foreach ($this->describe->getTable($data['table_name']) as $column) { |
|
98 | 3 | $column->getField() != $keywords[1] || array_push($data['defaults'], $column); |
|
99 | 3 | } |
|
100 | 3 | } |
|
101 | |||
102 | 6 | return $data; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Gets the file name of the specified migration. |
||
107 | * |
||
108 | * @param string $name |
||
109 | * @param boolean $isSequential |
||
110 | * @return string |
||
111 | */ |
||
112 | 27 | protected function getFileName($name, $isSequential = false) |
|
113 | { |
||
114 | 27 | $migrationType = $this->getMigrationType(); |
|
115 | |||
116 | 27 | $fileName = date('YmdHis') . '_' . underscore($name); |
|
117 | |||
118 | 27 | if ($migrationType == 'sequential' || $isSequential) { |
|
119 | 27 | $number = 1; |
|
120 | |||
121 | 27 | $files = new \FilesystemIterator(APPPATH . 'migrations', \FilesystemIterator::SKIP_DOTS); |
|
122 | |||
123 | 27 | $number += iterator_count($files); |
|
124 | |||
125 | 27 | $sequence = sprintf('%03d', $number); |
|
126 | 27 | $fileName = $sequence . '_' . substr($fileName, 15); |
|
127 | 27 | } |
|
128 | |||
129 | 27 | return $fileName; |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Gets the defined keywords from the command. |
||
134 | * |
||
135 | * @param string $name |
||
136 | * @return array |
||
137 | */ |
||
138 | 27 | protected function getKeywords($name) |
|
139 | { |
||
140 | 27 | $path = APPPATH . 'migrations'; |
|
141 | |||
142 | 27 | file_exists($path) || mkdir($path); |
|
143 | |||
144 | 27 | $keywords = [ '', '', '', '' ]; |
|
145 | |||
146 | 27 | return array_replace($keywords, explode('_', underscore($name))); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Prepares the data to be inserted in the template. |
||
151 | * |
||
152 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
153 | * @param array $keywords |
||
154 | * @return array |
||
155 | */ |
||
156 | 27 | protected function prepareData(InputInterface $input, array $keywords) |
|
157 | { |
||
158 | 27 | $data = []; |
|
159 | |||
160 | 27 | $data['command_name'] = $keywords[0]; |
|
161 | 27 | $data['data_types'] = [ 'string' => 'VARCHAR', 'integer' => 'INT' ]; |
|
162 | 27 | $data['class_name'] = underscore($input->getArgument('name')); |
|
163 | 27 | $data['table_name'] = $keywords[1]; |
|
164 | |||
165 | 27 | $data['columns'] = []; |
|
166 | 27 | $data['defaults'] = []; |
|
167 | |||
168 | 27 | if ($input->getOption('from-database') && $data['command_name'] == 'create') { |
|
169 | 3 | $data['columns'] = $this->describe->getTable($data['table_name']); |
|
170 | 3 | } |
|
171 | |||
172 | 27 | return $data; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Sets properties for a specified column |
||
177 | * |
||
178 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
179 | * @param string $fieldName |
||
180 | * @return \Rougin\Describe\Column |
||
181 | */ |
||
182 | 6 | protected function setColumn(InputInterface $input, $fieldName) |
|
197 | } |
||
198 |