/** * @return ViewModel */ public function databaseAction() { /** @var \Install\Service\Install $installService */ $installService = $this->getServiceLocator()->get('Install\\Service\\Install'); $sessionProgress = new Container('progress_tracker'); $sessionProgress->offsetSet('current_step', 'database'); $previousStep = $installService->checkPreviousStep(); if (null !== $previousStep) { return $this->redirect()->toRoute('install/default', ['controller' => 'index', 'action' => $previousStep]); } $sessionProgress->offsetSet('database', Install::TODO); $sessionForms = new Container('forms'); $this->setProgress(); if ($this->getRequest()->isPost()) { $dbForm = new DbConnection(); $dbForm->setInputFilter(new DbConnectionInputFilter()); $dbForm->setData($this->getRequest()->getPost()); if ($dbForm->isValid()) { $sessionForms->offsetSet('dbForm', $dbForm->getData()); $sessionProgress->offsetSet('database', Install::DONE); try { $installService->checkDbConnection($dbForm); $installService->createDbConfig($dbForm); return $this->redirect()->toRoute('install/default', ['controller' => 'index', 'action' => 'mail']); } catch (\PDOException $e) { $dbForm->get('host')->setMessages([$e->getMessage()]); } catch (\Exception $e) { $dbForm->get('port')->setMessages([$e->getMessage()]); } } } else { $dbForm = new DbConnection(); if (null !== $sessionForms->offsetGet('dbForm')) { $dbForm->setData($sessionForms->offsetGet('dbForm')); } } return new ViewModel(['dbForm' => $dbForm]); }
/** * @param DbConnection $dbForm */ public function checkDbConnection(DbConnection $dbForm) { $dbname = $dbForm->getData()['dbname']; $host = $dbForm->getData()['host']; $port = $dbForm->getData()['port']; $dsn = "mysql:dbname={$dbname};host={$host};port={$port}"; $user = $dbForm->getData()['user']; $password = $dbForm->getData()['password']; $connection = new \PDO($dsn, $user, $password); }