|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Refinery\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use FilesystemIterator; |
|
6
|
|
|
use RecursiveIteratorIterator; |
|
7
|
|
|
use RecursiveDirectoryIterator; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
use Rougin\Describe\Column; |
|
15
|
|
|
use Rougin\Describe\Describe; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create Migration Command |
|
19
|
|
|
* |
|
20
|
|
|
* Creates a new migration file based on its file name. |
|
21
|
|
|
* |
|
22
|
|
|
* @package Refinery |
|
23
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class CreateMigrationCommand extends AbstractCommand |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Checks whether the command is enabled or not in the current environment. |
|
29
|
|
|
* |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
3 |
|
public function isEnabled() |
|
33
|
|
|
{ |
|
34
|
3 |
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Sets the configurations of the specified command. |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
33 |
|
protected function configure() |
|
43
|
|
|
{ |
|
44
|
33 |
|
$this->setName('create') |
|
45
|
33 |
|
->setDescription('Creates a new migration file') |
|
46
|
33 |
|
->addArgument( |
|
47
|
33 |
|
'name', |
|
48
|
33 |
|
InputArgument::REQUIRED, |
|
49
|
|
|
'Name of the migration file' |
|
50
|
33 |
|
)->addOption( |
|
51
|
33 |
|
'from-database', |
|
52
|
33 |
|
null, |
|
53
|
33 |
|
InputOption::VALUE_NONE, |
|
54
|
|
|
'Generates a migration based from the database' |
|
55
|
33 |
|
)->addOption( |
|
56
|
33 |
|
'sequential', |
|
57
|
33 |
|
null, |
|
58
|
33 |
|
InputOption::VALUE_NONE, |
|
59
|
|
|
'Generates a migration file with a sequential identifier' |
|
60
|
33 |
|
)->addOption( |
|
61
|
33 |
|
'type', |
|
62
|
33 |
|
null, |
|
63
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
64
|
33 |
|
'Data type of the column', |
|
65
|
|
|
'varchar' |
|
66
|
33 |
|
)->addOption( |
|
67
|
33 |
|
'length', |
|
68
|
33 |
|
null, |
|
69
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
70
|
33 |
|
'Length of the column', |
|
71
|
|
|
50 |
|
72
|
33 |
|
)->addOption( |
|
73
|
33 |
|
'auto_increment', |
|
74
|
33 |
|
null, |
|
75
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
76
|
33 |
|
'Generates an "AUTO_INCREMENT" flag on the column', |
|
77
|
|
|
false |
|
78
|
33 |
|
)->addOption( |
|
79
|
33 |
|
'default', |
|
80
|
33 |
|
null, |
|
81
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
82
|
33 |
|
'Generates a default value in the column definition', |
|
83
|
|
|
'' |
|
84
|
33 |
|
)->addOption( |
|
85
|
33 |
|
'null', |
|
86
|
33 |
|
null, |
|
87
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
88
|
33 |
|
'Generates a "NULL" value in the column definition', |
|
89
|
|
|
false |
|
90
|
33 |
|
)->addOption( |
|
91
|
33 |
|
'primary', |
|
92
|
33 |
|
null, |
|
93
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
94
|
33 |
|
'Generates a "PRIMARY" value in the column definition', |
|
95
|
|
|
false |
|
96
|
33 |
|
)->addOption( |
|
97
|
33 |
|
'unsigned', |
|
98
|
33 |
|
null, |
|
99
|
33 |
|
InputOption::VALUE_OPTIONAL, |
|
100
|
33 |
|
'Generates an "UNSIGNED" value in the column definition', |
|
101
|
|
|
false |
|
102
|
33 |
|
); |
|
103
|
33 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Executes the command. |
|
107
|
|
|
* |
|
108
|
|
|
* @param InputInterface $input |
|
109
|
|
|
* @param OutputInterface $output |
|
110
|
|
|
* @return object|OutputInterface |
|
111
|
|
|
*/ |
|
112
|
27 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
113
|
|
|
{ |
|
114
|
27 |
|
$name = underscore($input->getArgument('name')); |
|
115
|
27 |
|
$path = APPPATH . 'migrations'; |
|
116
|
|
|
|
|
117
|
|
|
// Creates a "application/migrations" directory if it doesn't exist yet |
|
118
|
27 |
|
if ( ! file_exists($path)) { |
|
119
|
27 |
|
mkdir($path); |
|
120
|
27 |
|
} |
|
121
|
|
|
|
|
122
|
27 |
|
$fileName = $path . '/' . date('YmdHis') . '_' . $name . '.php'; |
|
123
|
|
|
|
|
124
|
27 |
|
$isSequential = strpos( |
|
125
|
27 |
|
file_get_contents(APPPATH . '/config/migration.php'), |
|
126
|
|
|
'$config[\'migration_type\'] = \'timestamp\'' |
|
127
|
27 |
|
); |
|
128
|
|
|
|
|
129
|
27 |
|
if ($input->getOption('sequential') || $isSequential === false) { |
|
130
|
27 |
|
$number = 1; |
|
131
|
|
|
|
|
132
|
27 |
|
$skipDots = FilesystemIterator::SKIP_DOTS; |
|
133
|
27 |
|
$files = new FilesystemIterator($path . '/', $skipDots); |
|
134
|
|
|
|
|
135
|
27 |
|
if ($files != '') { |
|
136
|
6 |
|
$number += iterator_count($files); |
|
137
|
6 |
|
} |
|
138
|
|
|
|
|
139
|
27 |
|
$sequence = sprintf('%03d', $number); |
|
140
|
27 |
|
$fileName = $path . '/' . $sequence . '_' . $name . '.php'; |
|
141
|
27 |
|
} |
|
142
|
|
|
|
|
143
|
27 |
|
$keywords = explode('_', $name); |
|
144
|
|
|
|
|
145
|
|
|
if ( |
|
146
|
27 |
|
$input->getOption('from-database') |
|
147
|
27 |
|
&& $keywords[0] != 'create' |
|
148
|
27 |
|
&& count($keywords) != 3 |
|
149
|
27 |
|
) { |
|
150
|
3 |
|
$command = '--from-database'; |
|
151
|
3 |
|
$keyword = 'create_*table*_table'; |
|
152
|
|
|
|
|
153
|
3 |
|
$message = "$command is only available to $keyword keyword"; |
|
154
|
|
|
|
|
155
|
3 |
|
return $output->writeln('<error>' . $message . '</error>'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
24 |
|
$data = []; |
|
159
|
24 |
|
$data['columns'] = []; |
|
160
|
24 |
|
$data['command'] = $keywords[0]; |
|
161
|
24 |
|
$data['defaultColumns'] = []; |
|
162
|
24 |
|
$data['description'] = str_replace($path . '/', '', $fileName); |
|
163
|
24 |
|
$data['name'] = $name; |
|
164
|
24 |
|
$data['table'] = (isset($keywords[1])) ? $keywords[1] : ''; |
|
165
|
|
|
|
|
166
|
24 |
|
$data['dataTypes'] = [ |
|
167
|
24 |
|
'string' => 'VARCHAR', |
|
168
|
|
|
'integer' => 'INT' |
|
169
|
24 |
|
]; |
|
170
|
|
|
|
|
171
|
24 |
|
switch ($data['command']) { |
|
172
|
24 |
|
case 'create': |
|
173
|
18 |
|
if ( ! $input->getOption('from-database')) { |
|
174
|
15 |
|
break; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
3 |
|
$data['columns'] = $this->describe->getTable($data['table']); |
|
178
|
|
|
|
|
179
|
3 |
|
break; |
|
180
|
6 |
|
case 'add': |
|
181
|
6 |
|
case 'delete': |
|
182
|
3 |
|
$field = $keywords[1]; |
|
183
|
3 |
|
$data['table'] = (isset($keywords[3])) ? $keywords[3] : ''; |
|
184
|
|
|
|
|
185
|
3 |
|
$column = new Column; |
|
186
|
3 |
|
$column->setField($field); |
|
187
|
|
|
|
|
188
|
3 |
|
array_push($data['columns'], $this->setColumn($column, $input)); |
|
189
|
|
|
|
|
190
|
3 |
|
break; |
|
191
|
3 |
|
case 'modify': |
|
192
|
3 |
|
$field = $keywords[1]; |
|
193
|
3 |
|
$data['table'] = (isset($keywords[3])) ? $keywords[3] : ''; |
|
194
|
|
|
|
|
195
|
3 |
|
$column = new Column; |
|
196
|
3 |
|
$column->setField($field); |
|
197
|
|
|
|
|
198
|
3 |
|
array_push($data['columns'], $this->setColumn($column, $input)); |
|
199
|
|
|
|
|
200
|
3 |
|
$table = $this->describe->getTable($data['table']); |
|
201
|
|
|
|
|
202
|
3 |
|
foreach ($table as $column) { |
|
203
|
|
|
if ($column->getField() == $field) { |
|
204
|
|
|
array_push($data['defaultColumns'], $column); |
|
205
|
|
|
|
|
206
|
|
|
break; |
|
207
|
|
|
} |
|
208
|
3 |
|
} |
|
209
|
|
|
|
|
210
|
3 |
|
break; |
|
211
|
24 |
|
} |
|
212
|
|
|
|
|
213
|
24 |
|
$template = $this->renderer->render('Migration.tpl', $data); |
|
214
|
|
|
|
|
215
|
24 |
|
$file = fopen($fileName, 'wb'); |
|
216
|
24 |
|
file_put_contents($fileName, $template); |
|
217
|
24 |
|
fclose($file); |
|
218
|
|
|
|
|
219
|
24 |
|
$fileName = str_replace($path . '/', '', $fileName); |
|
220
|
24 |
|
$message = '"' . $fileName . '" has been created.'; |
|
221
|
|
|
|
|
222
|
24 |
|
return $output->writeln('<info>' . $message . '</info>'); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Sets properties for a specified column |
|
227
|
|
|
* |
|
228
|
|
|
* @param Column $column |
|
229
|
|
|
* @param InputInterface $input |
|
230
|
|
|
* @return Column |
|
231
|
|
|
*/ |
|
232
|
6 |
|
private function setColumn(Column $column, InputInterface $input) |
|
233
|
|
|
{ |
|
234
|
6 |
|
$column->setNull($input->getOption('null')); |
|
235
|
6 |
|
$column->setDataType($input->getOption('type')); |
|
236
|
6 |
|
$column->setLength($input->getOption('length')); |
|
237
|
6 |
|
$column->setPrimary($input->getOption('primary')); |
|
238
|
6 |
|
$column->setUnsigned($input->getOption('unsigned')); |
|
239
|
6 |
|
$column->setDefaultValue($input->getOption('default')); |
|
240
|
6 |
|
$column->setAutoIncrement($input->getOption('auto_increment')); |
|
241
|
|
|
|
|
242
|
6 |
|
return $column; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|