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
|
|
|
|
*/
|
|
|
|
public function checkDbConnection($type, $host, $port, $name, $user, $pass)
|
|
|
|
{
|
|
|
|
Log::info('Testing Connection: '.$type.'::'.$user.':'.$pass.'@'.$host.':'.$port.';'.$name);
|
|
|
|
|
|
|
|
if($type === 'mysql') {
|
2017-12-15 11:34:11 +08:00
|
|
|
$dsn = "mysql:host=$host;port=$port;";
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needs testing
|
|
|
|
elseif ($type === 'postgres') {
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function setupDB()
|
|
|
|
{
|
2017-12-15 11:59:54 +08:00
|
|
|
$output = "";
|
|
|
|
#\Artisan::call('database:create');
|
|
|
|
#$output .= \Artisan::output();
|
|
|
|
|
|
|
|
\Artisan::call('migrate');
|
|
|
|
$output .= \Artisan::output();
|
|
|
|
|
|
|
|
\Artisan::call('db:seed');
|
|
|
|
$output .= \Artisan::output();
|
|
|
|
|
|
|
|
return $output;
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
}
|