1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Refinery\Commands; |
4
|
|
|
|
5
|
|
|
use Rougin\Describe\Describe; |
6
|
|
|
use League\Flysystem\Filesystem; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstract Command |
10
|
|
|
* |
11
|
|
|
* @package Refinery |
12
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractCommand extends \Symfony\Component\Console\Command\Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \CI_Controller |
18
|
|
|
*/ |
19
|
|
|
protected $codeigniter; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Rougin\Describe\Describe |
23
|
|
|
*/ |
24
|
|
|
protected $describe; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \League\Flysystem\Filesystem |
28
|
|
|
*/ |
29
|
|
|
protected $filesystem; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Twig_Environment |
33
|
|
|
*/ |
34
|
|
|
protected $renderer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \CI_Controller $codeigniter |
38
|
|
|
* @param \Rougin\Describe\Describe $describe |
39
|
|
|
* @param \League\Flysystem\Filesystem $filesystem |
40
|
|
|
* @param \Twig_Environment $renderer |
41
|
|
|
*/ |
42
|
33 |
|
public function __construct(\CI_Controller $codeigniter, Describe $describe, Filesystem $filesystem, \Twig_Environment $renderer) |
43
|
|
|
{ |
44
|
33 |
|
parent::__construct(); |
45
|
|
|
|
46
|
33 |
|
$this->codeigniter = $codeigniter; |
47
|
33 |
|
$this->describe = $describe; |
48
|
33 |
|
$this->filesystem = $filesystem; |
49
|
33 |
|
$this->renderer = $renderer; |
50
|
33 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Changes the migration version. |
54
|
|
|
* |
55
|
|
|
* @param integer $current |
56
|
|
|
* @param integer $timestamp |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
9 |
|
public function changeVersion($current, $timestamp) |
60
|
|
|
{ |
61
|
9 |
|
$old = '$config[\'migration_version\'] = ' . $current . ';'; |
62
|
9 |
|
$new = '$config[\'migration_version\'] = ' . $timestamp . ';'; |
63
|
|
|
|
64
|
9 |
|
$config = $this->filesystem->read('application/config/migration.php'); |
65
|
9 |
|
$config = str_replace($old, $new, $config); |
66
|
|
|
|
67
|
9 |
|
$this->filesystem->update('application/config/migration.php', $config); |
68
|
9 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets the latest migration version |
72
|
|
|
* |
73
|
|
|
* @param boolean $rollback |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
12 |
|
public function getLatestVersion($rollback = false) |
77
|
|
|
{ |
78
|
12 |
|
$config = $this->filesystem->read('application/config/migration.php'); |
79
|
12 |
|
$pattern = '/\$config\[\'migration_version\'\] = (\d+);/'; |
80
|
|
|
|
81
|
12 |
|
preg_match_all($pattern, $config, $match); |
82
|
|
|
|
83
|
12 |
|
if ($rollback && intval($match[1][0]) <= 0) { |
84
|
3 |
|
throw new \UnexpectedValueException("There's nothing to be rollbacked at."); |
85
|
|
|
} |
86
|
|
|
|
87
|
9 |
|
return $match[1][0]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets list of migrations from the specified directory. |
92
|
|
|
* |
93
|
|
|
* @param string $path |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
12 |
|
public function getMigrations($path) |
97
|
|
|
{ |
98
|
12 |
|
$filenames = []; |
99
|
12 |
|
$migrations = []; |
100
|
|
|
|
101
|
12 |
|
$limits = [ 'sequential' => 3, 'timestamp' => 14 ]; |
102
|
12 |
|
$type = $this->getMigrationType(); |
103
|
|
|
|
104
|
|
|
// Searches a listing of migration files and sorts them after |
105
|
12 |
|
$directory = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS); |
106
|
12 |
|
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST); |
107
|
|
|
|
108
|
12 |
|
foreach ($iterator as $path) { |
109
|
9 |
|
array_push($filenames, str_replace('.php', '', $path->getFilename())); |
110
|
9 |
|
array_push($migrations, substr($path->getFilename(), 0, $limits[$type])); |
111
|
12 |
|
} |
112
|
|
|
|
113
|
12 |
|
sort($filenames); |
114
|
12 |
|
sort($migrations); |
115
|
|
|
|
116
|
12 |
|
return [ $filenames, $migrations ]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the current migration type. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
30 |
|
public function getMigrationType() |
125
|
|
|
{ |
126
|
30 |
|
$config = $this->filesystem->read('application/config/migration.php'); |
127
|
|
|
|
128
|
30 |
|
preg_match('/\$config\[\'migration_type\'\] = \'(.*?)\';/i', $config, $match); |
129
|
|
|
|
130
|
30 |
|
return $match[1] ?: 'timestamp'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Checks whether the command is enabled or not in the current environment. |
135
|
|
|
* |
136
|
|
|
* @return boolean |
137
|
|
|
*/ |
138
|
3 |
|
public function isEnabled() |
139
|
|
|
{ |
140
|
3 |
|
$migrations = glob(APPPATH . 'migrations/*.php'); |
141
|
|
|
|
142
|
3 |
|
return count($migrations) > 0; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Migrates the specified versions to database. |
147
|
|
|
* |
148
|
|
|
* @param string $old |
149
|
|
|
* @param string $new |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
9 |
|
public function migrate($old, $new) |
153
|
|
|
{ |
154
|
|
|
// Enable migration and change the current version to a latest one |
155
|
9 |
|
$this->toggleMigration(true); |
156
|
9 |
|
$this->changeVersion($old, $new); |
157
|
|
|
|
158
|
9 |
|
$this->codeigniter->load->library('migration'); |
|
|
|
|
159
|
9 |
|
$this->codeigniter->migration->current(); |
|
|
|
|
160
|
|
|
|
161
|
9 |
|
$this->toggleMigration(); |
162
|
9 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Enables/disables the Migration Class. |
166
|
|
|
* |
167
|
|
|
* @param boolean $enabled |
168
|
|
|
* @return void |
169
|
|
|
*/ |
170
|
11 |
|
public function toggleMigration($enabled = false) |
171
|
|
|
{ |
172
|
9 |
|
$old = [ '$config[\'migration_enabled\'] = TRUE;', '$config[\'migration_enabled\'] = true;' ]; |
173
|
9 |
|
$new = [ '$config[\'migration_enabled\'] = FALSE;', '$config[\'migration_enabled\'] = false;' ]; |
174
|
|
|
|
175
|
9 |
|
if ($enabled) { |
176
|
9 |
|
$old = [ '$config[\'migration_enabled\'] = FALSE;', '$config[\'migration_enabled\'] = false;' ]; |
177
|
11 |
|
$new = [ '$config[\'migration_enabled\'] = TRUE;', '$config[\'migration_enabled\'] = true;' ]; |
178
|
9 |
|
} |
179
|
|
|
|
180
|
9 |
|
$config = $this->filesystem->read('application/config/migration.php'); |
181
|
9 |
|
$config = str_replace($old, $new, $config); |
182
|
|
|
|
183
|
9 |
|
$this->filesystem->update('application/config/migration.php', $config); |
184
|
9 |
|
} |
185
|
|
|
} |
186
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.