|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\controllers\console\migrations\base; |
|
4
|
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2MigrationsController; |
|
6
|
|
|
use rhertogh\Yii2Oauth2Server\migrations\base\Oauth2BaseMigration; |
|
7
|
|
|
use Yii; |
|
8
|
|
|
use yii\base\Action; |
|
9
|
|
|
use yii\base\InvalidConfigException; |
|
10
|
|
|
use yii\console\controllers\BaseMigrateController; |
|
11
|
|
|
use yii\console\ExitCode; |
|
12
|
|
|
use yii\helpers\Console; |
|
13
|
|
|
use yii\helpers\FileHelper; |
|
14
|
|
|
use yii\helpers\StringHelper; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @property Oauth2MigrationsController $controller |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Oauth2BaseGenerateMigrationsAction extends Action |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Template to use for the generated migrations. |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
public $templateFile = __DIR__ . DIRECTORY_SEPARATOR . 'generate' . DIRECTORY_SEPARATOR . 'migration.php'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param class-string<Oauth2BaseMigration>[]|array<class-string<Oauth2BaseMigration>, array> $sourceMigrationClasses |
|
|
|
|
|
|
29
|
|
|
* @return int |
|
30
|
|
|
* @throws InvalidConfigException |
|
31
|
|
|
* @throws \yii\base\Exception |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function generateMigrations($sourceMigrationClasses) |
|
34
|
|
|
{ |
|
35
|
|
|
$module = $this->controller->module; |
|
36
|
|
|
$migrationsNamespace = $module->migrationsNamespace; |
|
37
|
|
|
|
|
38
|
|
|
if (empty($migrationsNamespace)) { |
|
39
|
|
|
throw new InvalidConfigException('Oauth2Module::$migrationsNamespace must be set.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$migrationsPath = $this->getNamespacePath($migrationsNamespace); |
|
43
|
|
|
$existingMigrations = $this->getExistingMigrations($migrationsPath); |
|
44
|
|
|
|
|
45
|
|
|
$generateMigrations = []; |
|
46
|
|
|
foreach ($sourceMigrationClasses as $sourceMigrationClass => $config) { |
|
47
|
|
|
if (is_int($sourceMigrationClass)) { |
|
48
|
|
|
$sourceMigrationClass = $config; |
|
49
|
|
|
$config = []; |
|
50
|
|
|
} |
|
51
|
|
|
$sourceMigrationName = StringHelper::basename($sourceMigrationClass); |
|
52
|
|
|
$wrapperName = $sourceMigrationName . 'Wrapper'; |
|
53
|
|
|
/** @var string|Oauth2BaseMigration $sourceMigrationWrapper */ |
|
54
|
|
|
$sourceMigrationWrapper = __NAMESPACE__ . '\\' . $wrapperName; |
|
55
|
|
|
if (!class_exists($sourceMigrationWrapper, false)) { |
|
56
|
|
|
eval( |
|
|
|
|
|
|
57
|
|
|
'namespace ' . __NAMESPACE__ . ';' |
|
58
|
|
|
. ' class ' . $wrapperName . ' extends \\' . $sourceMigrationClass . ' {}' |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ( |
|
63
|
|
|
$sourceMigrationWrapper::generationIsActive($module) |
|
64
|
|
|
&& ($this->controller->force || !array_key_exists($sourceMigrationName, $existingMigrations)) |
|
65
|
|
|
) { |
|
66
|
|
|
if ($this->controller->force && array_key_exists($sourceMigrationName, $existingMigrations)) { |
|
67
|
|
|
$generateClass = $migrationsNamespace . '\\' . $existingMigrations[$sourceMigrationName]; |
|
68
|
|
|
} else { |
|
69
|
|
|
$generateClass = $this->generateNewMigrationClassName($migrationsNamespace, $sourceMigrationName); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$file = $migrationsPath . DIRECTORY_SEPARATOR . StringHelper::basename($generateClass) . '.php'; |
|
73
|
|
|
$content = $this->generateMigrationSourceCode([ |
|
74
|
|
|
'class' => $generateClass, |
|
75
|
|
|
'sourceClass' => $sourceMigrationClass, |
|
76
|
|
|
'config' => $config, |
|
77
|
|
|
]); |
|
78
|
|
|
|
|
79
|
|
|
if ( |
|
80
|
|
|
!file_exists($file) |
|
81
|
|
|
|| file_get_contents($file) !== $content |
|
82
|
|
|
) { |
|
83
|
|
|
$generateMigrations[$file] = $content; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$applyInfo = $this->generateApplyInfo($migrationsNamespace); |
|
89
|
|
|
|
|
90
|
|
|
if (empty($generateMigrations)) { |
|
91
|
|
|
$this->controller->stdout( |
|
92
|
|
|
'No new to generate. However, they might still need to be applied.' . PHP_EOL . $applyInfo, |
|
93
|
|
|
Console::FG_GREEN |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
return ExitCode::OK; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$n = count($generateMigrations); |
|
100
|
|
|
$this->controller->stdout( |
|
101
|
|
|
"There " . ($n === 1 ? 'is' : 'are') . " $n " . ($n === 1 ? 'migration' : 'migrations') |
|
102
|
|
|
. ' to generate:' . PHP_EOL, |
|
103
|
|
|
Console::FG_YELLOW |
|
104
|
|
|
); |
|
105
|
|
|
foreach ($generateMigrations as $file => $content) { |
|
106
|
|
|
$this->controller->stdout("\t$file "); |
|
107
|
|
|
if (file_exists($file)) { |
|
108
|
|
|
$this->controller->stdout('[update]', Console::BG_YELLOW); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->controller->stdout('[new]', Console::BG_GREEN); |
|
111
|
|
|
} |
|
112
|
|
|
$this->controller->stdout(PHP_EOL); |
|
113
|
|
|
} |
|
114
|
|
|
$this->controller->stdout(PHP_EOL); |
|
115
|
|
|
|
|
116
|
|
|
if ($this->controller->confirm('Generate new migration?', true)) { |
|
117
|
|
|
FileHelper::createDirectory($migrationsPath); |
|
118
|
|
|
FileHelper::changeOwnership($migrationsPath, $module->migrationsFileOwnership); |
|
119
|
|
|
|
|
120
|
|
|
foreach ($generateMigrations as $file => $content) { |
|
121
|
|
|
if (!$this->writeFile($file, $content)) { |
|
122
|
|
|
$this->controller->stdout("Failed to create new migration $file." . PHP_EOL, Console::FG_RED); |
|
123
|
|
|
return ExitCode::IOERR; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
FileHelper::changeOwnership($file, $module->migrationsFileOwnership, $module->migrationsFileMode); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->controller->stdout( |
|
130
|
|
|
"Successfully generated $n " . ($n === 1 ? 'migration' : 'migrations') . '.' . PHP_EOL . $applyInfo, |
|
131
|
|
|
Console::FG_GREEN |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return ExitCode::OK; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function writeFile($file, $data) |
|
139
|
|
|
{ |
|
140
|
|
|
return file_put_contents($file, $data, LOCK_EX) !== false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function getNamespacePath($namespace) |
|
144
|
|
|
{ |
|
145
|
|
|
return str_replace('/', DIRECTORY_SEPARATOR, Yii::getAlias('@' . str_replace('\\', '/', $namespace))); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function getExistingMigrations($migrationPath) |
|
149
|
|
|
{ |
|
150
|
|
|
if (!file_exists($migrationPath)) { |
|
151
|
|
|
return []; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$migrations = []; |
|
155
|
|
|
$handle = opendir($migrationPath); |
|
156
|
|
|
while (($file = readdir($handle)) !== false) { |
|
157
|
|
|
if ($file === '.' || $file === '..') { |
|
158
|
|
|
continue; |
|
159
|
|
|
} |
|
160
|
|
|
$path = $migrationPath . DIRECTORY_SEPARATOR . $file; |
|
161
|
|
|
if (preg_match('/^(m\d{12}.*_(Oauth2_\d{5}_.+Migration).*?)\.php$/is', $file, $matches) && is_file($path)) { |
|
162
|
|
|
$migrations[$matches[2]] = $matches[1]; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
closedir($handle); |
|
166
|
|
|
|
|
167
|
|
|
ksort($migrations); |
|
168
|
|
|
|
|
169
|
|
|
return $migrations; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
protected function generateMigrationSourceCode($params) |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->controller->renderFile(Yii::getAlias($this->templateFile), $params); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $migrationsNamespace |
|
179
|
|
|
* @param string $sourceMigrationName |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function generateNewMigrationClassName($migrationsNamespace, $sourceMigrationName) |
|
183
|
|
|
{ |
|
184
|
|
|
return $migrationsNamespace |
|
185
|
|
|
. '\\' . 'M' . gmdate('ymdHis') |
|
186
|
|
|
. $this->controller->module->migrationsPrefix . '_' . $sourceMigrationName; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param string $migrationsNamespace |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function generateApplyInfo($migrationsNamespace) |
|
194
|
|
|
{ |
|
195
|
|
|
$migrateControllerId = 'migrate'; |
|
196
|
|
|
$oauth2MigrationNameSpaceFound = false; |
|
197
|
|
|
foreach (Yii::$app->controllerMap as $controllerId => $controllerConfig) { |
|
198
|
|
|
if ( |
|
199
|
|
|
!is_array($controllerConfig) |
|
200
|
|
|
|| empty($controllerConfig['class']) |
|
201
|
|
|
|| !is_a($controllerConfig['class'], BaseMigrateController::class, true) |
|
202
|
|
|
) { |
|
203
|
|
|
continue; |
|
204
|
|
|
} |
|
205
|
|
|
$migrateControllerId = $controllerId; |
|
206
|
|
|
if ( |
|
207
|
|
|
!empty($controllerConfig['migrationNamespaces']) |
|
208
|
|
|
&& in_array($migrationsNamespace, $controllerConfig['migrationNamespaces']) |
|
209
|
|
|
) { |
|
210
|
|
|
$oauth2MigrationNameSpaceFound = true; |
|
211
|
|
|
break; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
if (!$oauth2MigrationNameSpaceFound) { |
|
|
|
|
|
|
216
|
|
|
$applyInfo = 'Add the "' . addslashes($migrationsNamespace) . '" namespace to `$migrationNamespaces` of the' |
|
217
|
|
|
. ' migration controller' |
|
218
|
|
|
. ' (https://www.yiiframework.com/doc/guide/2.0/en/db-migrations#namespaced-migrations)' |
|
219
|
|
|
. ' and run '; |
|
220
|
|
|
} else { |
|
221
|
|
|
$applyInfo = 'Run '; |
|
222
|
|
|
} |
|
223
|
|
|
$applyInfo .= 'the`yii ' . $migrateControllerId . '/up` command to apply them.' . PHP_EOL; |
|
224
|
|
|
|
|
225
|
|
|
return $applyInfo; |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|