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
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
12 |
|
public function getLatestVersion() |
76
|
|
|
{ |
77
|
12 |
|
$config = $this->filesystem->read('application/config/migration.php'); |
78
|
12 |
|
$pattern = '/\$config\[\'migration_version\'\] = (\d+);/'; |
79
|
|
|
|
80
|
12 |
|
preg_match_all($pattern, $config, $match); |
81
|
|
|
|
82
|
12 |
|
return $match[1][0]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Gets list of migrations from the specified directory. |
87
|
|
|
* |
88
|
|
|
* @param string $path |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
12 |
|
public function getMigrations($path) |
92
|
|
|
{ |
93
|
12 |
|
$filenames = []; |
94
|
12 |
|
$migrations = []; |
95
|
|
|
|
96
|
|
|
// Searches a listing of migration files and sorts them after |
97
|
12 |
|
$directory = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS); |
98
|
12 |
|
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST); |
99
|
|
|
|
100
|
12 |
|
foreach ($iterator as $path) { |
101
|
9 |
|
array_push($filenames, str_replace('.php', '', $path->getFilename())); |
102
|
|
|
|
103
|
9 |
|
$migration = substr($path->getFilename(), 0, 14); |
104
|
|
|
|
105
|
9 |
|
is_numeric($migration) || $migration = substr($path->getFilename(), 0, 3); |
106
|
|
|
|
107
|
9 |
|
array_push($migrations, $migration); |
108
|
12 |
|
} |
109
|
|
|
|
110
|
12 |
|
sort($filenames); |
111
|
12 |
|
sort($migrations); |
112
|
|
|
|
113
|
12 |
|
return [ $filenames, $migrations ]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Checks whether the command is enabled or not in the current environment. |
118
|
|
|
* |
119
|
|
|
* @return boolean |
120
|
|
|
*/ |
121
|
3 |
|
public function isEnabled() |
122
|
|
|
{ |
123
|
3 |
|
$migrations = glob(APPPATH . 'migrations/*.php'); |
124
|
|
|
|
125
|
3 |
|
return count($migrations) > 0; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Enables/disables the Migration Class. |
130
|
|
|
* |
131
|
|
|
* @param boolean $enabled |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
9 |
|
public function toggleMigration($enabled = false) |
135
|
|
|
{ |
136
|
9 |
|
$old = [ '$config[\'migration_enabled\'] = TRUE;', '$config[\'migration_enabled\'] = true;' ]; |
137
|
9 |
|
$new = [ '$config[\'migration_enabled\'] = FALSE;', '$config[\'migration_enabled\'] = false;' ]; |
138
|
|
|
|
139
|
9 |
|
if ($enabled) { |
140
|
9 |
|
$old = [ '$config[\'migration_enabled\'] = FALSE;', '$config[\'migration_enabled\'] = false;' ]; |
141
|
9 |
|
$new = [ '$config[\'migration_enabled\'] = TRUE;', '$config[\'migration_enabled\'] = true;' ]; |
142
|
9 |
|
} |
143
|
|
|
|
144
|
9 |
|
$config = $this->filesystem->read('application/config/migration.php'); |
145
|
9 |
|
$config = str_replace($old, $new, $config); |
146
|
|
|
|
147
|
9 |
|
$this->filesystem->update('application/config/migration.php', $config); |
148
|
9 |
|
} |
149
|
|
|
} |
150
|
|
|
|