1
|
|
|
<?php namespace Hafiz\Commands; |
2
|
|
|
|
3
|
|
|
use CodeIgniter\CLI\BaseCommand; |
4
|
|
|
use CodeIgniter\CLI\CLI; |
5
|
|
|
use Hafiz\Libraries\DBHandler; |
6
|
|
|
use Hafiz\Libraries\FileHandler; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Make Command Class for creating a migration Instance skeleton class |
10
|
|
|
* Model can be created as skeleton or |
11
|
|
|
* user can point a Table to get property for model |
12
|
|
|
* |
13
|
|
|
* @package CodeIgniter\Commands |
14
|
|
|
* @extend BaseCommand |
15
|
|
|
*/ |
16
|
|
|
class MakeMigration extends BaseCommand |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
protected $db = null; |
20
|
|
|
|
21
|
|
|
protected $file = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The group command is heading under all |
25
|
|
|
* commands will be listed |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $group = 'CI4-Recharge'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The Command's name |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $name = 'make:migrate'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The Command's short description |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $description = 'Creates a Migration file.'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The Command's usage |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $usage = 'make:migrate [migrate_name] [Options]'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The Command's Arguments |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $arguments = [ |
53
|
|
|
'migrate_name' => 'The Migration file name', |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* The Command's Options |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $options = [ |
61
|
|
|
'-n' => 'Set migrate namespace', |
62
|
|
|
'-t' => 'Set migrate Database table', |
63
|
|
|
'-all' => 'Set migration for Complete database' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a new migration file with the current timestamp. |
68
|
|
|
* @param array $params |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function run(array $params = []) |
72
|
|
|
{ |
73
|
|
|
helper(['inflector', 'filesystem']); |
74
|
|
|
$this->file = new FileHandler(); |
75
|
|
|
$this->db = new DBHandler(); |
76
|
|
|
|
77
|
|
|
$name = array_shift($params); |
78
|
|
|
$ns = $params['-n'] ?? CLI::getOption('n'); |
79
|
|
|
$table = $params['-t'] ?? CLI::getOption('t'); |
80
|
|
|
$alltables = $params['-all'] ?? CLI::getOption('all'); |
81
|
|
|
|
82
|
|
|
if (empty($name) && is_null($alltables)) |
83
|
|
|
$name = CLI::prompt(lang('Recharge.migrateName'), null, 'string'); |
84
|
|
|
|
85
|
|
|
if (empty($name) && is_null($alltables)) { |
86
|
|
|
CLI::error(lang('Recharge.badName')); |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
//namespace locator |
91
|
|
|
$nsinfo = $this->file->getNamespaceInfo($ns, 'App'); |
92
|
|
|
//class & file name |
93
|
|
|
$ns = $nsinfo['ns']; |
94
|
|
|
$targetDir = $nsinfo['path'] . '/Database/Migrations/'; |
95
|
|
|
$config = config('Migrations'); |
96
|
|
|
$timestamp = gmdate($config->timestampFormat); |
97
|
|
|
|
98
|
|
|
//if all database migration creation selected |
99
|
|
|
|
100
|
|
|
if (is_bool($alltables) && $alltables === true) { |
101
|
|
|
$tableNames = $this->db->getTableNames(); |
102
|
|
|
|
103
|
|
|
foreach ($tableNames as $tableName) { |
104
|
|
|
//disable framework generation tables |
105
|
|
|
if ($tableName == 'migrations' || $tableName == 'ci_sessions') continue; |
106
|
|
|
|
107
|
|
|
$this->generateMigration($timestamp, $ns, $targetDir, NULL, $tableName); |
108
|
|
|
} |
109
|
|
|
} else |
110
|
|
|
$this->generateMigration($timestamp, $ns, $targetDir, $name, $table); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $timestamp |
115
|
|
|
* @param string $ns |
116
|
|
|
* @param string $targetDir |
117
|
|
|
* @param string|null $name |
118
|
|
|
* @param null $table |
|
|
|
|
119
|
|
|
*/ |
120
|
|
|
public function generateMigration(string $timestamp, string $ns, string $targetDir, string $name = NULL, $table = NULL): void |
121
|
|
|
{ |
122
|
|
|
if (empty($name)) { |
123
|
|
|
$fileName = $timestamp . 'create_' . underscore($table) . '_table.php'; |
|
|
|
|
124
|
|
|
$migrateName = pascalize('create_' . underscore($table) . '_table'); |
125
|
|
|
} else { |
126
|
|
|
$fileName = $timestamp . underscore($name) . '.php'; |
127
|
|
|
$migrateName = pascalize($name); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$filepath = $targetDir . $fileName; |
131
|
|
|
|
132
|
|
|
if ($this->file->verifyDirectory($filepath)) { |
133
|
|
|
//do we have to add table info |
134
|
|
|
if (!empty($table)) { |
135
|
|
|
if ($this->db->checkTableExist($table)) { |
136
|
|
|
$properties = $this->db->getTableInfos($table); |
137
|
|
|
extract($properties); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$data = [ |
142
|
|
|
'{namespace}' => $ns, |
143
|
|
|
'{name}' => $migrateName, |
144
|
|
|
'{created_at}' => date("d F, Y h:i:s A"), |
145
|
|
|
'{attributes}' => $attributes ?? NULL, |
146
|
|
|
'{table}' => $table ?? NULL, |
147
|
|
|
'{keys}' => $keys ?? NULL, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
//check a directory exist |
151
|
|
|
if ($this->file->checkFileExist($filepath) == true) { |
152
|
|
|
$template = $this->file->renderTemplate('migrate', $data); |
153
|
|
|
if (!write_file($filepath, $template)) { |
154
|
|
|
CLI::error(lang('Recharge.writeError', [$filepath])); |
155
|
|
|
return; |
156
|
|
|
} |
157
|
|
|
CLI::write('Created file: ' . CLI::color($filepath, 'green')); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|