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() |
|
36 | { |
||
37 | 33 | $this->setName('create') |
|
38 | 33 | ->setDescription('Creates a new migration file') |
|
39 | 33 | ->addArgument('name', InputArgument::REQUIRED, 'Name of the migration file') |
|
40 | 33 | ->addOption('from-database', null, InputOption::VALUE_NONE, 'Generates a migration based from the database') |
|
41 | 33 | ->addOption('sequential', null, InputOption::VALUE_NONE, 'Generates a migration file with a sequential identifier') |
|
42 | 33 | ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'Data type of the column', 'varchar') |
|
43 | 33 | ->addOption('length', null, InputOption::VALUE_OPTIONAL, 'Length of the column', 50) |
|
44 | 33 | ->addOption('auto_increment', null, InputOption::VALUE_OPTIONAL, 'Generates an "AUTO_INCREMENT" flag on the column', false) |
|
45 | 33 | ->addOption('default', null, InputOption::VALUE_OPTIONAL, 'Generates a default value in the column definition', '') |
|
46 | 33 | ->addOption('null', null, InputOption::VALUE_OPTIONAL, 'Generates a "NULL" value in the column definition', false) |
|
47 | 33 | ->addOption('primary', null, InputOption::VALUE_OPTIONAL, 'Generates a "PRIMARY" value in the column definition', false) |
|
48 | 33 | ->addOption('unsigned', null, InputOption::VALUE_OPTIONAL, 'Generates an "UNSIGNED" value in the column definition', false); |
|
49 | 33 | } |
|
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 | $config = $this->filesystem->read('application/config/migration.php'); |
|
61 | |||
62 | 27 | $name = underscore($input->getArgument('name')); |
|
63 | 27 | $path = APPPATH . 'migrations'; |
|
64 | |||
65 | 27 | file_exists($path) || mkdir($path); |
|
66 | |||
67 | 27 | $fileName = date('YmdHis') . '_' . $name; |
|
68 | |||
69 | // Returns the migration type to be used |
||
70 | 27 | preg_match('/\$config\[\'migration_type\'\] = \'(.*?)\';/i', $config, $match); |
|
71 | |||
72 | 27 | if ($match[1] == 'sequential' || $input->getOption('sequential')) { |
|
73 | 27 | $number = 1; |
|
74 | |||
75 | 27 | $files = new \FilesystemIterator($path, \FilesystemIterator::SKIP_DOTS); |
|
76 | |||
77 | 27 | iterator_count($files) <= 0 || $number += iterator_count($files); |
|
78 | |||
79 | 27 | $sequence = sprintf('%03d', $number); |
|
80 | 27 | $fileName = $sequence . '_' . $name; |
|
81 | 27 | } |
|
82 | |||
83 | 27 | $keywords = explode('_', $name); |
|
84 | |||
85 | 27 | $data = $this->prepareData($input, $keywords); |
|
86 | 24 | $data = $this->defineColumns($input, $keywords, $data); |
|
87 | |||
88 | 24 | $rendered = $this->renderer->render('Migration.twig', $data); |
|
89 | |||
90 | 24 | $this->filesystem->write('application/migrations/' . $fileName . '.php', $rendered); |
|
91 | |||
92 | 24 | return $output->writeln('<info>"' . $fileName . '" has been created.</info>'); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * Defines the columns to be included in the migration. |
||
97 | * |
||
98 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
99 | * @param array $keywords |
||
100 | * @param array $data |
||
101 | * @return array |
||
102 | */ |
||
103 | 24 | protected function defineColumns(InputInterface $input, array $keywords, array $data) |
|
128 | |||
129 | /** |
||
130 | * Prepares the data to be inserted in the template. |
||
131 | * |
||
132 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
133 | * @param array $keywords |
||
134 | * @return array |
||
135 | */ |
||
136 | 27 | protected function prepareData(InputInterface $input, array $keywords) |
|
153 | |||
154 | /** |
||
155 | * Sets properties for a specified column |
||
156 | * |
||
157 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
158 | * @param string $fieldName |
||
159 | * @return \Rougin\Describe\Column |
||
160 | */ |
||
161 | 6 | protected function setColumn(InputInterface $input, $fieldName) |
|
176 | } |
||
177 |