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 | if ($input->getOption('from-database') && $keywords[0] != 'create') { |
|
64 | 3 | $message = '--from-database is only available to create_*table*_table keyword'; |
|
65 | |||
66 | 3 | throw new \InvalidArgumentException($message); |
|
67 | } |
||
68 | |||
69 | 24 | $data = $this->prepareData($input, $keywords); |
|
70 | |||
71 | 24 | $rendered = $this->renderer->render('Migration.twig', $data); |
|
72 | 24 | $rendered = str_replace("));\n\n\t}", "));\n\t}", $rendered); |
|
73 | |||
74 | 24 | $this->filesystem->write('application/migrations/' . $fileName . '.php', $rendered); |
|
75 | |||
76 | 24 | return $output->writeln('<info>"' . $fileName . '" has been created.</info>'); |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * Defines the columns to be included in the migration. |
||
81 | * |
||
82 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
83 | * @param array $keywords |
||
84 | * @param array $data |
||
85 | * @return array |
||
86 | */ |
||
87 | 6 | protected function defineColumns(InputInterface $input, array $keywords, array $data) |
|
88 | { |
||
89 | 6 | $data['table_name'] = $keywords[1]; |
|
90 | |||
91 | 6 | array_push($data['columns'], $this->setColumn($input, $keywords[2])); |
|
92 | |||
93 | 6 | if ($data['command_name'] == 'modify') { |
|
94 | 3 | foreach ($this->describe->getTable($data['table_name']) as $column) { |
|
95 | 3 | $column->getField() != $keywords[2] || array_push($data['defaults'], $column); |
|
96 | 3 | } |
|
97 | 3 | } |
|
98 | |||
99 | 6 | return $data; |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * Gets the file name of the specified migration. |
||
104 | * |
||
105 | * @param string $name |
||
106 | * @param boolean $isSequential |
||
107 | * @return string |
||
108 | */ |
||
109 | 27 | protected function getFileName($name, $isSequential = false) |
|
110 | { |
||
111 | 27 | $migrationType = $this->getMigrationType(); |
|
112 | |||
113 | 27 | $fileName = date('YmdHis') . '_' . underscore($name); |
|
114 | |||
115 | 27 | if ($migrationType == 'sequential' || $isSequential) { |
|
116 | 3 | $number = 1; |
|
117 | |||
118 | 3 | $files = new \FilesystemIterator(APPPATH . 'migrations', \FilesystemIterator::SKIP_DOTS); |
|
119 | |||
120 | 3 | $number += iterator_count($files); |
|
121 | |||
122 | 3 | $sequence = sprintf('%03d', $number); |
|
123 | 3 | $fileName = $sequence . '_' . substr($fileName, 15); |
|
124 | 3 | } |
|
125 | |||
126 | 27 | return $fileName; |
|
127 | } |
||
128 | |||
129 | /** |
||
130 | * Gets the defined keywords from the command. |
||
131 | * |
||
132 | * @param string $name |
||
133 | * @return array |
||
134 | */ |
||
135 | 27 | protected function getKeywords($name) |
|
136 | { |
||
137 | 27 | $empty = [ '', '', '' ]; |
|
138 | 27 | $path = APPPATH . 'migrations'; |
|
139 | |||
140 | 27 | file_exists($path) || mkdir($path); |
|
141 | |||
142 | 27 | preg_match('/(.*?)_(.*?)_table/', underscore($name), $keywords); |
|
143 | |||
144 | 27 | if (strpos($keywords[2], '_in') !== false) { |
|
145 | 9 | preg_match('/(.*?)_(.*?)_in_(.*?)_table/', underscore($name), $keywords); |
|
146 | |||
147 | 9 | list($keywords[3], $keywords[2]) = [ $keywords[2], $keywords[3] ]; |
|
148 | 9 | } |
|
149 | |||
150 | 27 | array_shift($keywords); |
|
151 | |||
152 | 27 | return array_replace($empty, $keywords); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Prepares the data to be inserted in the template. |
||
157 | * |
||
158 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
159 | * @param array $keywords |
||
160 | * @return array |
||
161 | */ |
||
162 | 24 | protected function prepareData(InputInterface $input, array $keywords) |
|
163 | { |
||
164 | 24 | $data = [ 'columns' => [], 'defaults' => [] ]; |
|
165 | |||
166 | 24 | $data['command_name'] = $keywords[0]; |
|
167 | 24 | $data['data_types'] = [ 'string' => 'VARCHAR', 'integer' => 'INT' ]; |
|
168 | 24 | $data['class_name'] = underscore($input->getArgument('name')); |
|
169 | 24 | $data['table_name'] = $keywords[1]; |
|
170 | |||
171 | 24 | if ($input->getOption('from-database') && $data['command_name'] == 'create') { |
|
172 | 3 | $data['columns'] = $this->describe->getTable($data['table_name']); |
|
173 | 3 | } |
|
174 | |||
175 | 24 | return ($data['command_name'] != 'create') ? $this->defineColumns($input, $keywords, $data) : $data; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Sets properties for a specified column |
||
180 | * |
||
181 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
182 | * @param string $fieldName |
||
183 | * @return \Rougin\Describe\Column |
||
184 | */ |
||
185 | 6 | protected function setColumn(InputInterface $input, $fieldName) |
|
200 | } |
||
201 |