|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Potievdev\SlimRbac\Console\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Exception; |
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Potievdev\SlimRbac\Exception\ConfigNotFoundException; |
|
9
|
|
|
use Potievdev\SlimRbac\Exception\NotSupportedDatabaseException; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Phinx\Config\Config; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class BaseCommand |
|
17
|
|
|
* @package Potievdev\SlimRbac\Command |
|
18
|
|
|
*/ |
|
19
|
|
|
class BaseDatabaseCommand extends Command |
|
20
|
|
|
{ |
|
21
|
|
|
/** Default environment name */ |
|
22
|
|
|
const DEFAULT_ENVIRONMENT_NAME = 'rbac'; |
|
23
|
|
|
|
|
24
|
|
|
/** Default migrations table name */ |
|
25
|
|
|
const DEFAULT_MIGRATION_TABLE = 'rbac_migrations'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Config $config*/ |
|
28
|
|
|
protected $config; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string environment name */ |
|
31
|
|
|
protected $environment = self::DEFAULT_ENVIRONMENT_NAME; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string migrations table name */ |
|
34
|
|
|
protected $migrationTableName = self::DEFAULT_MIGRATION_TABLE; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Returns cli-config file path |
|
38
|
|
|
* @param InputInterface $input |
|
39
|
|
|
* @return string |
|
40
|
|
|
* @throws ConfigNotFoundException |
|
41
|
|
|
*/ |
|
42
|
|
|
private function getCliPath(InputInterface $input): string |
|
43
|
|
|
{ |
|
44
|
|
|
$filePaths = [ |
|
45
|
|
|
__DIR__ . '/../../../config/sr-config.php', |
|
46
|
|
|
__DIR__ . '/../../../../../../sr-config.php', |
|
47
|
|
|
__DIR__ . '/../../../../../../config/sr-config.php', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
if ($input->hasOption('config')) { |
|
51
|
|
|
$filePaths[] = $input->getOption('config'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
foreach ($filePaths as $path) { |
|
55
|
|
|
if (is_file($path)) { |
|
56
|
|
|
return $path; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
throw new ConfigNotFoundException( |
|
61
|
|
|
'There is not found config file.' . PHP_EOL . |
|
62
|
|
|
'You can pass path to file as option: -c FILE_PATH' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @throws ConfigNotFoundException |
|
68
|
|
|
* @throws Exception |
|
69
|
|
|
* @throws NotSupportedDatabaseException |
|
70
|
|
|
*/ |
|
71
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
72
|
|
|
{ |
|
73
|
|
|
/** @var EntityManager $entityManager */ |
|
74
|
|
|
$entityManager = require $this->getCliPath($input); |
|
75
|
|
|
|
|
76
|
|
|
$paths['migrations'] = __DIR__ . '/../../migrations'; |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$environments['default_migration_table'] = $this->migrationTableName; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
$environments['default_database'] = $this->environment; |
|
81
|
|
|
|
|
82
|
|
|
$environments[$this->environment] = $this->getAdapterConfigs($entityManager->getConnection()); |
|
83
|
|
|
|
|
84
|
|
|
$configArray['paths'] = $paths; |
|
|
|
|
|
|
85
|
|
|
$configArray['environments'] = $environments; |
|
86
|
|
|
|
|
87
|
|
|
$this->config = new Config($configArray); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @throws Exception |
|
92
|
|
|
* @throws NotSupportedDatabaseException |
|
93
|
|
|
*/ |
|
94
|
|
|
private function getAdapterConfigs(Connection $connection): array |
|
95
|
|
|
{ |
|
96
|
|
|
$platformName = $connection->getDatabasePlatform()->getName(); |
|
97
|
|
|
|
|
98
|
|
|
switch ($platformName) { |
|
99
|
|
|
case 'mysql': |
|
100
|
|
|
$adapterName = 'mysql'; |
|
101
|
|
|
break; |
|
102
|
|
|
case 'postgresql': |
|
103
|
|
|
$adapterName = 'pgsql'; |
|
104
|
|
|
break; |
|
105
|
|
|
default: |
|
106
|
|
|
throw new NotSupportedDatabaseException('Not supported database platform ' . $platformName); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return [ |
|
110
|
|
|
'adapter' => $adapterName, |
|
111
|
|
|
'name' => $connection->getDatabase(), |
|
112
|
|
|
'host' => $connection->getParams()['host'], |
|
113
|
|
|
'user' => $connection->getParams()['user'], |
|
114
|
|
|
'pass' => $connection->getParams()['password'], |
|
115
|
|
|
'port' => $connection->getParams()['port'] |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|