2017-12-15 06:38:29 +08:00
|
|
|
<?php
|
|
|
|
|
2019-12-13 04:07:35 +08:00
|
|
|
namespace App\Services\Installer;
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Service;
|
2019-12-02 22:57:35 +08:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2017-12-15 06:38:29 +08:00
|
|
|
use PDO;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
class DatabaseService extends Service
|
|
|
|
{
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Check the PHP version that it meets the minimum requirement
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-02-26 05:27:20 +08:00
|
|
|
* @param $driver
|
|
|
|
* @param $host
|
|
|
|
* @param $port
|
|
|
|
* @param $name
|
|
|
|
* @param $user
|
|
|
|
* @param $pass
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
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
|
|
|
{
|
2018-02-26 05:27:20 +08:00
|
|
|
Log::info('Testing Connection: '.$driver.'::'.$user.':<hidden>@'.$host.':'.$port.';'.$name);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2018-01-10 10:47:03 +08:00
|
|
|
// TODO: Needs testing
|
2019-12-02 22:57:35 +08:00
|
|
|
if ($driver === 'postgres') {
|
2017-12-15 06:38:29 +08:00
|
|
|
$dsn = "pgsql:host=$host;port=$port;dbname=$name";
|
2019-08-27 00:32:46 +08:00
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
try {
|
|
|
|
$conn = new PDO($dsn, $user, $pass);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2019-12-02 22:57:35 +08:00
|
|
|
|
|
|
|
return true;
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
|
2019-12-02 22:57:35 +08:00
|
|
|
// Default MySQL
|
|
|
|
$dsn = "mysql:host=$host;port=$port;dbname=$name";
|
|
|
|
Log::info('Connection string: '.$dsn);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$conn = new PDO($dsn, $user, $pass);
|
|
|
|
} catch (\PDOException $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2017-12-15 06:38:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the database by running the migration commands
|
2018-02-26 05:13:04 +08:00
|
|
|
* Only run the setup for sqlite, otherwise, we're assuming
|
|
|
|
* that the MySQL database has already been created
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
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 = '';
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
if (config('database.default') === 'sqlite') {
|
2019-12-02 22:57:35 +08:00
|
|
|
Artisan::call('database:create');
|
|
|
|
$output .= Artisan::output();
|
2018-01-10 10:47:03 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|