2017-12-15 06:38:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Installer\Services;
|
|
|
|
|
|
|
|
use Log;
|
|
|
|
use PDO;
|
|
|
|
|
|
|
|
class DatabaseService {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the PHP version that it meets the minimum requirement
|
|
|
|
* @throws \PDOException
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2017-12-15 12:32:52 +08:00
|
|
|
public function checkDbConnection($driver, $host, $port, $name, $user, $pass)
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2017-12-15 12:32:52 +08:00
|
|
|
Log::info('Testing Connection: '.$driver.'::'.$user.':'.$pass.'@'.$host.':'.$port.';'.$name);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2017-12-15 12:32:52 +08:00
|
|
|
if($driver === 'mysql') {
|
2018-01-10 10:47:03 +08:00
|
|
|
$dsn = "mysql:host=$host;port=$port;dbname=$name";
|
2017-12-15 06:38:29 +08:00
|
|
|
Log::info('Connection string: '. $dsn);
|
|
|
|
try {
|
|
|
|
$conn = new PDO($dsn, $user, $pass);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
// TODO: Needs testing
|
2017-12-15 12:32:52 +08:00
|
|
|
elseif ($driver === 'postgres') {
|
2017-12-15 06:38:29 +08:00
|
|
|
$dsn = "pgsql:host=$host;port=$port;dbname=$name";
|
|
|
|
try {
|
|
|
|
$conn = new PDO($dsn, $user, $pass);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the database by running the migration commands
|
|
|
|
*/
|
2017-12-30 06:56:46 +08:00
|
|
|
public function setupDB()
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2017-12-15 12:22:33 +08:00
|
|
|
$output = '';
|
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
if(config('database.default') === 'sqlite') {
|
|
|
|
\Artisan::call('database:create');
|
|
|
|
$output .= \Artisan::output();
|
|
|
|
}
|
2017-12-15 11:59:54 +08:00
|
|
|
|
|
|
|
\Artisan::call('migrate');
|
2017-12-15 12:22:33 +08:00
|
|
|
$output .= trim(\Artisan::output());
|
2017-12-15 11:59:54 +08:00
|
|
|
|
|
|
|
\Artisan::call('db:seed');
|
2017-12-15 12:22:33 +08:00
|
|
|
$output .= trim(\Artisan::output());
|
2017-12-15 11:59:54 +08:00
|
|
|
|
2017-12-15 12:22:33 +08:00
|
|
|
return trim($output);
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
}
|