|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Refinery; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\SparkPlug\SparkPlug; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Manager |
|
9
|
|
|
* |
|
10
|
|
|
* @package Refinery |
|
11
|
|
|
* @author Rougin Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Manager |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Rougin\SparkPlug\SparkPlug |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $ci; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $path; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Initializes the migration instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $path |
|
29
|
|
|
*/ |
|
30
|
45 |
|
public function __construct($path) |
|
31
|
|
|
{ |
|
32
|
45 |
|
$globals = array('PRJK' => 'Refinery'); |
|
33
|
|
|
|
|
34
|
45 |
|
$ci = new SparkPlug($globals, array()); |
|
35
|
|
|
|
|
36
|
45 |
|
substr($path, -1) !== '/' && $path .= '/'; |
|
37
|
|
|
|
|
38
|
45 |
|
$this->path = (string) $path; |
|
39
|
|
|
|
|
40
|
45 |
|
$this->ci = $ci->set('APPPATH', $path); |
|
41
|
45 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Creates a new file to the "migrations" directory. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $filename |
|
47
|
|
|
* @param string $content |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
24 |
|
public function create($filename, $content) |
|
51
|
|
|
{ |
|
52
|
24 |
|
$path = $this->path . '/migrations/'; |
|
53
|
|
|
|
|
54
|
24 |
|
$filename = (string) $path . $filename; |
|
55
|
|
|
|
|
56
|
24 |
|
file_put_contents($filename, $content); |
|
57
|
24 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the current migration version. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
21 |
|
public function current() |
|
65
|
|
|
{ |
|
66
|
21 |
|
return $this->get('migration_version'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns a generated migration filename. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
27 |
|
public function filename($name) |
|
76
|
|
|
{ |
|
77
|
27 |
|
$type = $this->get('migration_type', "'timestamp'"); |
|
78
|
|
|
|
|
79
|
27 |
|
$prefix = (string) date('YmdHis', (integer) time()); |
|
80
|
|
|
|
|
81
|
27 |
|
if ($type === "'sequential'") { |
|
82
|
27 |
|
$path = (string) $this->path . '/migrations'; |
|
83
|
|
|
|
|
84
|
27 |
|
$path = new \FilesystemIterator($path); |
|
85
|
|
|
|
|
86
|
27 |
|
$regex = new \RegexIterator($path, '/^.+\.php$/i'); |
|
87
|
|
|
|
|
88
|
27 |
|
$items = count(iterator_to_array($regex)); |
|
89
|
|
|
|
|
90
|
27 |
|
$prefix = (string) sprintf('%03d', $items + 1); |
|
91
|
9 |
|
} |
|
92
|
|
|
|
|
93
|
27 |
|
return (string) $prefix . '_' . $name . '.php'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns an array of database migrations. |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
18 |
|
public function migrations() |
|
102
|
|
|
{ |
|
103
|
18 |
|
$migration = $this->load(); |
|
104
|
|
|
|
|
105
|
18 |
|
$items = $migration->find_migrations(); |
|
106
|
|
|
|
|
107
|
18 |
|
$this->set('migration_enabled', 'false'); |
|
108
|
|
|
|
|
109
|
18 |
|
return (array) $items; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Migrates to a specific schema version. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $version |
|
116
|
|
|
* @return boolean|string |
|
117
|
|
|
*/ |
|
118
|
18 |
|
public function migrate($version = null) |
|
119
|
|
|
{ |
|
120
|
18 |
|
$migration = $this->load(); |
|
121
|
|
|
|
|
122
|
18 |
|
if ($version !== null) { |
|
123
|
15 |
|
$result = $migration->version($version); |
|
124
|
5 |
|
} else { |
|
125
|
3 |
|
$result = $migration->latest(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
18 |
|
if ($result !== true) { |
|
129
|
18 |
|
$this->set('migration_version', $result); |
|
130
|
6 |
|
} |
|
131
|
|
|
|
|
132
|
18 |
|
$this->set('migration_enabled', 'false'); |
|
133
|
|
|
|
|
134
|
18 |
|
return $result; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Resets the migration schema. |
|
139
|
|
|
* |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
*/ |
|
142
|
9 |
|
public function reset() |
|
143
|
|
|
{ |
|
144
|
9 |
|
$migration = $this->load(); |
|
145
|
|
|
|
|
146
|
9 |
|
$result = (string) $migration->version(0); |
|
147
|
|
|
|
|
148
|
9 |
|
$result = $result === '000' ? 0 : $result; |
|
149
|
|
|
|
|
150
|
9 |
|
$this->set('migration_version', $result); |
|
151
|
|
|
|
|
152
|
9 |
|
$this->set('migration_enabled', 'false'); |
|
153
|
|
|
|
|
154
|
9 |
|
return (string) $result; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns a value from migration configuration. |
|
159
|
|
|
* |
|
160
|
|
|
* @param string $key |
|
161
|
|
|
* @param mixed $default |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
*/ |
|
164
|
36 |
|
protected function get($key, $default = null) |
|
165
|
|
|
{ |
|
166
|
36 |
|
$file = $this->path . '/config/migration.php'; |
|
167
|
|
|
|
|
168
|
36 |
|
$config = file_get_contents((string) $file); |
|
169
|
|
|
|
|
170
|
36 |
|
$pattern = '/\$config\[\\\'' . $key . '\\\'\] = (.*?);/i'; |
|
171
|
|
|
|
|
172
|
36 |
|
preg_match($pattern, $config, $matches); |
|
173
|
|
|
|
|
174
|
36 |
|
return $matches ? $matches[1] : $default; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Loads the migration instance. |
|
179
|
|
|
* |
|
180
|
|
|
* @return \CI_Migration |
|
181
|
|
|
*/ |
|
182
|
27 |
|
protected function load() |
|
183
|
|
|
{ |
|
184
|
27 |
|
$key = 'migration_enabled'; |
|
185
|
|
|
|
|
186
|
27 |
|
$ci = $this->ci->instance(); |
|
187
|
|
|
|
|
188
|
27 |
|
$this->set($key, 'true'); |
|
189
|
|
|
|
|
190
|
27 |
|
$ci->load->library('migration'); |
|
191
|
|
|
|
|
192
|
27 |
|
return $ci->migration; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Changes the value from migration configuration. |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $key |
|
199
|
|
|
* @param string $value |
|
200
|
|
|
* @return void |
|
201
|
|
|
*/ |
|
202
|
27 |
|
protected function set($key, $value) |
|
203
|
|
|
{ |
|
204
|
27 |
|
$file = (string) $this->path . '/config/migration.php'; |
|
205
|
|
|
|
|
206
|
27 |
|
$replace = '$config[\'' . $key . '\'] = ' . $value . ';'; |
|
207
|
|
|
|
|
208
|
27 |
|
$config = file_get_contents((string) $file); |
|
209
|
|
|
|
|
210
|
27 |
|
$pattern = '/\$config\[\\\'' . $key . '\\\'\] = (.*?);/i'; |
|
211
|
|
|
|
|
212
|
27 |
|
$result = preg_replace($pattern, $replace, $config); |
|
213
|
|
|
|
|
214
|
27 |
|
file_put_contents((string) $file, (string) $result); |
|
215
|
27 |
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|