|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Refinery\Common; |
|
4
|
|
|
|
|
5
|
|
|
use FilesystemIterator; |
|
6
|
|
|
use RecursiveIteratorIterator; |
|
7
|
|
|
use RecursiveDirectoryIterator; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Migration Helper |
|
11
|
|
|
* |
|
12
|
|
|
* Provides common methods used in other commands |
|
13
|
|
|
* |
|
14
|
|
|
* @package Refinery |
|
15
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class MigrationHelper |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Changes the migration version. |
|
21
|
|
|
* |
|
22
|
|
|
* @param int $current |
|
23
|
|
|
* @param int $timestamp |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
6 |
|
public static function changeVersion($current, $timestamp) |
|
27
|
|
|
{ |
|
28
|
6 |
|
$path = APPPATH . '/config/migration.php'; |
|
29
|
6 |
|
$migrationFile = file_get_contents($path); |
|
30
|
|
|
|
|
31
|
6 |
|
$currentVersion = '$config[\'migration_version\'] = ' . $current . ';'; |
|
32
|
6 |
|
$newVersion = '$config[\'migration_version\'] = ' . $timestamp . ';'; |
|
33
|
|
|
|
|
34
|
6 |
|
$migrationFile = str_replace( |
|
35
|
6 |
|
$currentVersion, |
|
36
|
6 |
|
$newVersion, |
|
37
|
|
|
$migrationFile |
|
38
|
6 |
|
); |
|
39
|
|
|
|
|
40
|
6 |
|
$file = fopen($path, 'wb'); |
|
41
|
6 |
|
file_put_contents($path, $migrationFile); |
|
42
|
6 |
|
fclose($file); |
|
43
|
6 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Gets the latest migration version |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
8 |
|
public static function getLatestVersion($file) |
|
51
|
|
|
{ |
|
52
|
8 |
|
$pattern = '/\$config\[\'migration_version\'\] = (\d+);/'; |
|
53
|
|
|
|
|
54
|
8 |
|
preg_match_all($pattern, $file, $match); |
|
55
|
|
|
|
|
56
|
8 |
|
return $match[1][0]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Gets list of migrations from the specified directory. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $path |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
8 |
|
public static function getMigrations($path) |
|
66
|
|
|
{ |
|
67
|
8 |
|
$filenames = []; |
|
68
|
8 |
|
$migrations = []; |
|
69
|
|
|
|
|
70
|
8 |
|
if (! is_dir($path)) { |
|
71
|
2 |
|
return [$filenames, $migrations]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
6 |
|
$skipDots = FilesystemIterator::SKIP_DOTS; |
|
75
|
6 |
|
$selfFirst = RecursiveIteratorIterator::SELF_FIRST; |
|
76
|
|
|
|
|
77
|
|
|
// Searches a listing of migration files and sorts them after |
|
78
|
6 |
|
$directory = new RecursiveDirectoryIterator($path, $skipDots); |
|
79
|
6 |
|
$iterator = new RecursiveIteratorIterator($directory, $selfFirst); |
|
80
|
|
|
|
|
81
|
6 |
|
foreach ($iterator as $path) { |
|
82
|
6 |
|
$filenames[] = str_replace('.php', '', $path->getFilename()); |
|
83
|
6 |
|
$migration = substr($path->getFilename(), 0, 14); |
|
84
|
|
|
|
|
85
|
6 |
|
if (! is_numeric($migration)) { |
|
86
|
6 |
|
$migration = substr($path->getFilename(), 0, 3); |
|
87
|
6 |
|
} |
|
88
|
|
|
|
|
89
|
6 |
|
$migrations[] = $migration; |
|
90
|
6 |
|
} |
|
91
|
|
|
|
|
92
|
6 |
|
sort($filenames); |
|
93
|
6 |
|
sort($migrations); |
|
94
|
|
|
|
|
95
|
6 |
|
return [$filenames, $migrations]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Enables/disables the Migration Class. |
|
100
|
|
|
* |
|
101
|
|
|
* @param boolean $enabled |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
6 |
|
public static function toggleMigration($enabled = false) |
|
105
|
|
|
{ |
|
106
|
6 |
|
$path = APPPATH . '/config/migration.php'; |
|
107
|
6 |
|
$migrationFile = file_get_contents($path); |
|
108
|
|
|
|
|
109
|
|
|
$search = [ |
|
110
|
6 |
|
'$config[\'migration_enabled\'] = TRUE;', |
|
111
|
6 |
|
'$config[\'migration_enabled\'] = true;', |
|
112
|
6 |
|
]; |
|
113
|
|
|
|
|
114
|
|
|
$replace = [ |
|
115
|
6 |
|
'$config[\'migration_enabled\'] = FALSE;', |
|
116
|
6 |
|
'$config[\'migration_enabled\'] = false;', |
|
117
|
6 |
|
]; |
|
118
|
|
|
|
|
119
|
6 |
|
if ($enabled) { |
|
120
|
|
|
$search = [ |
|
121
|
6 |
|
'$config[\'migration_enabled\'] = FALSE;', |
|
122
|
6 |
|
'$config[\'migration_enabled\'] = false;', |
|
123
|
6 |
|
]; |
|
124
|
|
|
|
|
125
|
|
|
$replace = [ |
|
126
|
6 |
|
'$config[\'migration_enabled\'] = TRUE;', |
|
127
|
6 |
|
'$config[\'migration_enabled\'] = true;', |
|
128
|
6 |
|
]; |
|
129
|
6 |
|
} |
|
130
|
|
|
|
|
131
|
6 |
|
$migrationFile = str_replace($search, $replace, $migrationFile); |
|
132
|
|
|
|
|
133
|
6 |
|
$file = fopen($path, 'wb'); |
|
134
|
6 |
|
file_put_contents($path, $migrationFile); |
|
135
|
6 |
|
fclose($file); |
|
136
|
6 |
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|