|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\CLI\Install\Plugin; |
|
5
|
|
|
|
|
6
|
|
|
use Shlinkio\Shlink\CLI\Model\CustomizableAppConfig; |
|
7
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
8
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
10
|
|
|
|
|
11
|
|
|
class DatabaseConfigCustomizer implements ConfigCustomizerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
const DATABASE_DRIVERS = [ |
|
14
|
|
|
'MySQL' => 'pdo_mysql', |
|
15
|
|
|
'PostgreSQL' => 'pdo_pgsql', |
|
16
|
|
|
'SQLite' => 'pdo_sqlite', |
|
17
|
|
|
]; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var Filesystem |
|
21
|
|
|
*/ |
|
22
|
|
|
private $filesystem; |
|
23
|
|
|
|
|
24
|
4 |
|
public function __construct(Filesystem $filesystem) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$this->filesystem = $filesystem; |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param SymfonyStyle $io |
|
31
|
|
|
* @param CustomizableAppConfig $appConfig |
|
32
|
|
|
* @return void |
|
33
|
|
|
* @throws IOException |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig) |
|
36
|
|
|
{ |
|
37
|
4 |
|
$io->title('DATABASE'); |
|
38
|
|
|
|
|
39
|
4 |
|
if ($appConfig->hasDatabase() && $io->confirm('Do you want to keep imported database config?')) { |
|
40
|
|
|
// If the user selected to keep DB config and is configured to use sqlite, copy DB file |
|
41
|
2 |
|
if ($appConfig->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) { |
|
42
|
|
|
try { |
|
43
|
1 |
|
$this->filesystem->copy( |
|
44
|
1 |
|
$appConfig->getImportedInstallationPath() . '/' . CustomizableAppConfig::SQLITE_DB_PATH, |
|
45
|
1 |
|
CustomizableAppConfig::SQLITE_DB_PATH |
|
46
|
|
|
); |
|
47
|
|
|
} catch (IOException $e) { |
|
48
|
|
|
$io->error('It wasn\'t possible to import the SQLite database'); |
|
49
|
|
|
throw $e; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Select database type |
|
57
|
2 |
|
$params = []; |
|
58
|
2 |
|
$databases = \array_keys(self::DATABASE_DRIVERS); |
|
59
|
2 |
|
$dbType = $io->choice('Select database type', $databases, $databases[0]); |
|
60
|
2 |
|
$params['DRIVER'] = self::DATABASE_DRIVERS[$dbType]; |
|
61
|
|
|
|
|
62
|
|
|
// Ask for connection params if database is not SQLite |
|
63
|
2 |
|
if ($params['DRIVER'] !== self::DATABASE_DRIVERS['SQLite']) { |
|
64
|
2 |
|
$params['NAME'] = $io->ask('Database name', 'shlink'); |
|
65
|
2 |
|
$params['USER'] = $io->ask('Database username'); |
|
66
|
2 |
|
$params['PASSWORD'] = $io->ask('Database password'); |
|
67
|
2 |
|
$params['HOST'] = $io->ask('Database host', 'localhost'); |
|
68
|
2 |
|
$params['PORT'] = $io->ask('Database port', $this->getDefaultDbPort($params['DRIVER'])); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
$appConfig->setDatabase($params); |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
private function getDefaultDbPort(string $driver): string |
|
75
|
|
|
{ |
|
76
|
2 |
|
return $driver === 'pdo_mysql' ? '3306' : '5432'; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|