/** * @param $databaseName * @param \PDO $pdo * @return bool */ public function continueWithExistingTables($databaseName, \PDO $pdo) { $service = new DatabaseService($pdo); $tableCount = $service->getTableCount(); if ($tableCount == 0) { return true; } $question = new ConfirmationQuestion(sprintf('The database %s already contains %s tables. Continue? (yes/no) [no]', $databaseName, $tableCount), false); return $this->askQuestion($question); }
/** * @param IOHelper $IOHelper * @param DatabaseConnectionInformation $defaultConnectionInformation * @return DatabaseConnectionInformation */ protected function interactDatabaseConfig(IOHelper $IOHelper, DatabaseConnectionInformation $defaultConnectionInformation = null) { if ($defaultConnectionInformation === null) { $defaultConnectionInformation = new DatabaseConnectionInformation(['hostname' => 'localhost', 'port' => 3306]); } $IOHelper->writeln("<info>=== Database configuration ===</info>"); $databaseInteractor = new DatabaseInteractor($IOHelper); $databaseConnectionInformation = $databaseInteractor->askDatabaseConnectionInformation($defaultConnectionInformation); $databaseFactory = new DatabaseFactory(); do { $pdo = null; try { $pdo = $databaseFactory->createPDOConnection($databaseConnectionInformation); } catch (\PDOException $e) { $IOHelper->writeln(''); $IOHelper->writeln(sprintf("Got database error: %s", $e->getMessage())); $IOHelper->writeln(''); $databaseConnectionInformation = $databaseInteractor->askDatabaseConnectionInformation($databaseConnectionInformation); } } while (!$pdo); $databaseService = new DatabaseService($pdo); $databaseNames = $databaseService->getAvailableDatabaseNames(); $defaultChoice = null; if ($defaultConnectionInformation->databaseName) { if (in_array($defaultConnectionInformation->databaseName, $databaseNames)) { $defaultChoice = array_search($defaultConnectionInformation->databaseName, $databaseNames); } } $choices = $databaseNames; array_unshift($choices, '[create new database]'); $question = new ChoiceQuestion('Please select your database', $choices, $defaultChoice); $question->setErrorMessage('Database %s is invalid.'); $databaseName = $databaseInteractor->askQuestion($question); if ($databaseName === $choices[0]) { $databaseName = $databaseInteractor->createDatabase($pdo); } $pdo->exec("USE {$databaseName}"); if (!$databaseInteractor->continueWithExistingTables($databaseName, $pdo)) { $IOHelper->writeln("Installation aborted."); exit; } $databaseConnectionInformation->databaseName = $databaseName; /** @var $configWriter ConfigWriter */ $configWriter = $this->container->offsetGet('config.writer'); $configWriter->writeConfig($databaseConnectionInformation); return $databaseConnectionInformation; }