1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Potievdev\SlimRbac\Console\Command; |
4
|
|
|
|
5
|
|
|
use Phinx\Config\Config; |
6
|
|
|
use Potievdev\SlimRbac\Component\Config\RbacConfig; |
7
|
|
|
use Potievdev\SlimRbac\Exception\ConfigNotFoundException; |
8
|
|
|
use Potievdev\SlimRbac\Exception\NotSupportedDatabaseException; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BaseCommand |
15
|
|
|
* @package Potievdev\SlimRbac\Command |
16
|
|
|
*/ |
17
|
|
|
class BaseDatabaseCommand extends Command |
18
|
|
|
{ |
19
|
|
|
/** Default environment name */ |
20
|
|
|
const DEFAULT_ENVIRONMENT_NAME = 'rbac'; |
21
|
|
|
|
22
|
|
|
/** Default migrations table name */ |
23
|
|
|
const DEFAULT_MIGRATION_TABLE = 'rbac_migrations'; |
24
|
|
|
|
25
|
|
|
/** Migration files path */ |
26
|
|
|
const MIGRATION_PATH = __DIR__ . '/../../../migrations'; |
27
|
|
|
|
28
|
|
|
/** @var Config $config */ |
29
|
|
|
protected $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @throws NotSupportedDatabaseException |
33
|
|
|
* @throws ConfigNotFoundException |
34
|
|
|
*/ |
35
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
|
|
$rbacConfig = RbacConfig::createFromConfigFile(); |
38
|
|
|
|
39
|
|
|
$configArray = [ |
40
|
|
|
'paths' => [ |
41
|
|
|
'migrations' => self::MIGRATION_PATH |
42
|
|
|
], |
43
|
|
|
'environments' => [ |
44
|
|
|
'default_migration_table' => self::DEFAULT_MIGRATION_TABLE, |
45
|
|
|
'default_database' => self::DEFAULT_ENVIRONMENT_NAME, |
46
|
|
|
self::DEFAULT_ENVIRONMENT_NAME => $this->getAdapterConfigs($rbacConfig) |
47
|
|
|
] |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
$this->config = new Config($configArray); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws NotSupportedDatabaseException |
55
|
|
|
*/ |
56
|
|
|
private function getAdapterConfigs(RbacConfig $rbacConfig): array |
57
|
|
|
{ |
58
|
|
|
$platformName = $rbacConfig->getDatabaseDriver(); |
59
|
|
|
|
60
|
|
|
switch ($platformName) { |
61
|
|
|
case 'pdo_mysql': |
62
|
|
|
$adapterName = 'mysql'; |
63
|
|
|
break; |
64
|
|
|
case 'pdo_postgres': |
65
|
|
|
$adapterName = 'pgsql'; |
66
|
|
|
break; |
67
|
|
|
default: |
68
|
|
|
throw NotSupportedDatabaseException::notSupportedPlatform($platformName); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'adapter' => $adapterName, |
73
|
|
|
'name' => $rbacConfig->getDatabaseName(), |
74
|
|
|
'host' => $rbacConfig->getDatabaseHost(), |
75
|
|
|
'user' => $rbacConfig->getDatabaseUser(), |
76
|
|
|
'pass' => $rbacConfig->getDatabasePassword(), |
77
|
|
|
'port' => $rbacConfig->getDatabasePort(), |
78
|
|
|
'charset' => $rbacConfig->getDatabaseCharset(), |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|