|
1
|
|
|
<?php |
|
2
|
|
|
namespace Shlinkio\Shlink\CLI\Install\Plugin; |
|
3
|
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation as DI; |
|
5
|
|
|
use Shlinkio\Shlink\CLI\Model\CustomizableAppConfig; |
|
6
|
|
|
use Symfony\Component\Console\Exception\RuntimeException; |
|
7
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
11
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
12
|
|
|
use Symfony\Component\Filesystem\Exception\IOException; |
|
13
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
14
|
|
|
|
|
15
|
|
|
class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin |
|
16
|
|
|
{ |
|
17
|
|
|
const DATABASE_DRIVERS = [ |
|
18
|
|
|
'MySQL' => 'pdo_mysql', |
|
19
|
|
|
'PostgreSQL' => 'pdo_pgsql', |
|
20
|
|
|
'SQLite' => 'pdo_sqlite', |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Filesystem |
|
25
|
|
|
*/ |
|
26
|
|
|
private $filesystem; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* DatabaseConfigCustomizerPlugin constructor. |
|
30
|
|
|
* @param QuestionHelper $questionHelper |
|
31
|
|
|
* @param Filesystem $filesystem |
|
32
|
|
|
* |
|
33
|
|
|
* @DI\Inject({QuestionHelper::class, Filesystem::class}) |
|
34
|
|
|
*/ |
|
35
|
4 |
|
public function __construct(QuestionHelper $questionHelper, Filesystem $filesystem) |
|
36
|
|
|
{ |
|
37
|
4 |
|
parent::__construct($questionHelper); |
|
38
|
4 |
|
$this->filesystem = $filesystem; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param InputInterface $input |
|
43
|
|
|
* @param OutputInterface $output |
|
44
|
|
|
* @param CustomizableAppConfig $appConfig |
|
45
|
|
|
* @return void |
|
46
|
|
|
* @throws IOException |
|
47
|
|
|
* @throws RuntimeException |
|
48
|
|
|
*/ |
|
49
|
4 |
|
public function process(InputInterface $input, OutputInterface $output, CustomizableAppConfig $appConfig) |
|
50
|
|
|
{ |
|
51
|
4 |
|
$this->printTitle($output, 'DATABASE'); |
|
52
|
|
|
|
|
53
|
4 |
|
if ($appConfig->hasDatabase() && $this->questionHelper->ask($input, $output, new ConfirmationQuestion( |
|
54
|
4 |
|
'<question>Do you want to keep imported database config? (Y/n):</question> ' |
|
55
|
|
|
))) { |
|
56
|
|
|
// If the user selected to keep DB config and is configured to use sqlite, copy DB file |
|
57
|
2 |
|
if ($appConfig->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) { |
|
58
|
|
|
try { |
|
59
|
1 |
|
$this->filesystem->copy( |
|
60
|
1 |
|
$appConfig->getImportedInstallationPath() . '/' . CustomizableAppConfig::SQLITE_DB_PATH, |
|
61
|
1 |
|
CustomizableAppConfig::SQLITE_DB_PATH |
|
62
|
|
|
); |
|
63
|
|
|
} catch (IOException $e) { |
|
64
|
|
|
$output->writeln('<error>It wasn\'t possible to import the SQLite database</error>'); |
|
65
|
|
|
throw $e; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Select database type |
|
73
|
2 |
|
$params = []; |
|
74
|
2 |
|
$databases = array_keys(self::DATABASE_DRIVERS); |
|
75
|
2 |
|
$dbType = $this->questionHelper->ask($input, $output, new ChoiceQuestion( |
|
76
|
2 |
|
'<question>Select database type (defaults to ' . $databases[0] . '):</question>', |
|
77
|
2 |
|
$databases, |
|
78
|
|
|
0 |
|
79
|
|
|
)); |
|
80
|
2 |
|
$params['DRIVER'] = self::DATABASE_DRIVERS[$dbType]; |
|
81
|
|
|
|
|
82
|
|
|
// Ask for connection params if database is not SQLite |
|
83
|
2 |
|
if ($params['DRIVER'] !== self::DATABASE_DRIVERS['SQLite']) { |
|
84
|
2 |
|
$params['NAME'] = $this->ask($input, $output, 'Database name', 'shlink'); |
|
85
|
2 |
|
$params['USER'] = $this->ask($input, $output, 'Database username'); |
|
86
|
2 |
|
$params['PASSWORD'] = $this->ask($input, $output, 'Database password'); |
|
87
|
2 |
|
$params['HOST'] = $this->ask($input, $output, 'Database host', 'localhost'); |
|
88
|
2 |
|
$params['PORT'] = $this->ask($input, $output, 'Database port', $this->getDefaultDbPort($params['DRIVER'])); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
$appConfig->setDatabase($params); |
|
92
|
2 |
|
} |
|
93
|
|
|
|
|
94
|
2 |
|
private function getDefaultDbPort($driver) |
|
95
|
|
|
{ |
|
96
|
2 |
|
return $driver === 'pdo_mysql' ? '3306' : '5432'; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|