|
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\import\Oauth2_FilshYii2Oauth2ServerImportMigration; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\InvalidArgumentException; |
|
10
|
|
|
use yii\base\InvalidConfigException; |
|
11
|
|
|
use yii\console\ExitCode; |
|
12
|
|
|
use yii\db\Connection; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @property Oauth2MigrationsController $controller |
|
16
|
|
|
*/ |
|
17
|
|
|
class Oauth2GenerateImportMigrationAction extends Oauth2BaseGenerateMigrationsAction |
|
18
|
|
|
{ |
|
19
|
|
|
public function run($origin) |
|
20
|
|
|
{ |
|
21
|
|
|
$imports = [ |
|
22
|
|
|
'filsh' => [$this, 'generatedFilshImport'], |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
if (!array_key_exists($origin, $imports)) { |
|
26
|
|
|
throw new InvalidArgumentException('Unknown import origin "' . $origin . '", available origins: ' |
|
27
|
|
|
. implode(', ', array_keys($imports)) . '.' |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return call_user_func($imports[$origin]); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Generate a migration to import data from https://github.com/filsh/yii2-oauth2-server |
|
36
|
|
|
* @return int |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function generatedFilshImport() |
|
39
|
|
|
{ |
|
40
|
|
|
$controller = $this->controller; |
|
41
|
|
|
|
|
42
|
|
|
if (!$controller->confirm('Generate migration for "filsh/yii2-oauth2-server" data import?', true)) { |
|
43
|
|
|
return ExitCode::OK; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$db = Yii::$app->get($controller->db); |
|
47
|
|
|
if (!($db instanceof Connection)) { |
|
48
|
|
|
throw new InvalidConfigException(get_class($controller) |
|
49
|
|
|
. '::$db must reference an Yii::$app component that is a ' . Connection::class); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$defaultClientTable = '{{%oauth_clients}}'; |
|
53
|
|
|
if (!$db->getTableSchema($defaultClientTable)) { |
|
54
|
|
|
$defaultClientTable = null; |
|
55
|
|
|
} |
|
56
|
|
|
$clientTable = $this->controller->prompt('Original clients table name?', [ |
|
57
|
|
|
'required' => true, |
|
58
|
|
|
'default' => $defaultClientTable, |
|
59
|
|
|
'validator' => function ($input, &$error) use ($db) { |
|
60
|
|
|
if (!$db->getTableSchema($input)) { |
|
61
|
|
|
$error = 'Table "' . $input . '" not found.'; |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
return true; |
|
65
|
|
|
}, |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
return $this->generateMigrations([ |
|
69
|
|
|
Oauth2_FilshYii2Oauth2ServerImportMigration::class => [ |
|
70
|
|
|
'clientTable' => $clientTable, |
|
71
|
|
|
], |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|