MigrateController.php
4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace thread\app\console\controllers;
use Yii;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;
/**
* Class BackendController
* Uses for base configuration of backend. All backend controllers methods should extends this one.
*
* @package thread\app\console\controllers
*/
class MigrateController extends \yii\console\controllers\MigrateController
{
/**
*
* [
* '@thread/modules/user/migrations',
* ]
*
* @var array
*/
public $migrationPaths = [];
/**
*
*[
* '@thread/modules',
* ]
*
* @var array
*/
public $migrationPathsOfModules = [];
/**
* MigrateController constructor.
* @param string $id
* @param \yii\base\Module $module
* @param array $config
*/
public function __construct($id, $module, $config = [])
{
parent::__construct($id, $module, $config);
$this->initPathsOfModules();
}
/**
*
*/
public function initPathsOfModules()
{
if (!empty($this->migrationPathsOfModules)) {
foreach ($this->migrationPathsOfModules as $module) {
$list = $this->getDirsIntoModule(Yii::getAlias($module));
if (!empty($list)) {
foreach ($list as $item) {
if (is_dir($item . DIRECTORY_SEPARATOR . 'migrations')) {
$this->migrationPaths[] = $item . DIRECTORY_SEPARATOR . 'migrations';
}
}
}
}
}
}
/**
* @param $baseDir
* @return array
*/
public function getDirsIntoModule($baseDir)
{
$list = [];
$handle = opendir($baseDir);
if ($handle === false) {
throw new InvalidParamException("Unable to open directory: $baseDir");
}
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $baseDir . DIRECTORY_SEPARATOR . $file;
if (is_dir($path)) {
$list[] = $path;
}
}
closedir($handle);
return $list;
}
/**
* Returns the migrations that are not applied.
* @return array list of new migrations
*/
protected function getNewMigrations()
{
$applied = [];
foreach ($this->getMigrationHistory(null) as $version => $time) {
$applied[substr($version, 1, 13)] = true;
}
$migrations = [];
foreach ($this->migrationPaths as $path) {
$migrations = ArrayHelper::merge($migrations, $this->getMigrationsByPath(\Yii::getAlias($path), $applied));
}
$migrations = array_unique($migrations);
sort($migrations);
return $migrations;
}
/**
* @param string $class
* @return mixed
*/
protected function createMigration($class)
{
foreach ($this->migrationPaths as $path) {
$file = Yii::getAlias($path) . DIRECTORY_SEPARATOR . $class . '.php';
if (is_file($file)) {
break;
}
}
require_once($file);
return new $class(['db' => $this->db]);
}
/**
* @param null $path
* @return array
*/
protected function getMigrationsByPath($path = null, $applied)
{
$migrations = [];
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
if ($file === '.' || $file === '..') {
continue;
}
$path = $path . DIRECTORY_SEPARATOR . $file;
if (preg_match('/^(m(\d{6}_\d{6})_.*?)\.php$/', $file, $matches)) {
if (isset($matches[1])) {
array_push($migrations, $matches[1]);
}
}
}
foreach ($migrations as $key => $item) {
if (isset($applied[substr($item, 1, 13)])) {
unset($migrations[$key]);
}
}
closedir($handle);
return $migrations;
}
}