1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rhertogh\Yii2Oauth2Server\controllers\console\migrations; |
4
|
|
|
|
5
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\migrations\base\Oauth2BaseGenerateMigrationsAction; |
6
|
|
|
use rhertogh\Yii2Oauth2Server\controllers\console\Oauth2MigrationsController; |
7
|
|
|
use rhertogh\Yii2Oauth2Server\migrations\base\Oauth2BaseMigration; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property Oauth2MigrationsController $controller |
12
|
|
|
*/ |
13
|
|
|
class Oauth2GenerateMigrationsAction extends Oauth2BaseGenerateMigrationsAction |
14
|
|
|
{ |
15
|
7 |
|
public function run() |
16
|
|
|
{ |
17
|
7 |
|
$sourcePath = $this->getMigrationsSourcePath(); |
18
|
7 |
|
$sourceNamespace = $this->getMigrationsSourceNamespace(); |
19
|
7 |
|
$sourceMigrationClasses = $this->getSourceMigrationClasses($sourcePath, $sourceNamespace); |
20
|
|
|
|
21
|
7 |
|
return $this->generateMigrations($sourceMigrationClasses); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $migrationPath |
26
|
|
|
* @param string $namespace |
27
|
|
|
* @return class-string<Oauth2BaseMigration>[] |
|
|
|
|
28
|
|
|
*/ |
29
|
8 |
|
protected function getSourceMigrationClasses($migrationPath, $namespace) |
30
|
|
|
{ |
31
|
8 |
|
if (!file_exists($migrationPath)) { |
32
|
1 |
|
throw new InvalidConfigException('Source migration path "' . $migrationPath . '" does not exist.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
$migrations = []; |
36
|
7 |
|
$handle = opendir($migrationPath); |
37
|
7 |
|
while (($file = readdir($handle)) !== false) { |
38
|
7 |
|
if ($file === '.' || $file === '..') { |
39
|
7 |
|
continue; |
40
|
|
|
} |
41
|
7 |
|
$path = $migrationPath . DIRECTORY_SEPARATOR . $file; |
42
|
7 |
|
if (preg_match('/^(Oauth2_\d{5}_.+Migration)\.php$/is', $file, $matches) && is_file($path)) { |
43
|
7 |
|
$migrations[] = $namespace . '\\' . $matches[1]; |
44
|
|
|
} |
45
|
|
|
} |
46
|
7 |
|
closedir($handle); |
47
|
|
|
|
48
|
7 |
|
sort($migrations); |
49
|
|
|
|
50
|
7 |
|
return $migrations; |
51
|
|
|
} |
52
|
|
|
|
53
|
7 |
|
protected function getMigrationsSourcePath() |
54
|
|
|
{ |
55
|
7 |
|
$reflector = new \ReflectionClass($this->controller->module); |
56
|
7 |
|
$moduleFile = $reflector->getFileName(); |
57
|
|
|
|
58
|
7 |
|
return dirname($moduleFile) . DIRECTORY_SEPARATOR . 'migrations'; |
59
|
|
|
} |
60
|
|
|
|
61
|
7 |
|
protected function getMigrationsSourceNamespace() |
62
|
|
|
{ |
63
|
7 |
|
$reflector = new \ReflectionClass($this->controller->module); |
64
|
7 |
|
return $reflector->getNamespaceName() . '\\migrations'; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|