1 | <?php |
||
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) |
|
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 | '<question>Do you want to keep imported database config? (Y/n):</question> ' |
||
55 | 4 | ))) { |
|
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 | CustomizableAppConfig::SQLITE_DB_PATH |
||
62 | 1 | ); |
|
63 | 1 | } catch (IOException $e) { |
|
64 | $output->writeln('<error>It wasn\'t possible to import the SQLite database</error>'); |
||
65 | throw $e; |
||
66 | } |
||
67 | 1 | } |
|
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 | 2 | )); |
|
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 | 2 | } |
|
90 | |||
91 | 2 | $appConfig->setDatabase($params); |
|
92 | 2 | } |
|
93 | |||
94 | 2 | private function getDefaultDbPort($driver) |
|
98 | } |
||
99 |