Rewrite how mysql db is created to avoid external processes #132
This commit is contained in:
50
app/Console/Services/Database.php
Normal file
50
app/Console/Services/Database.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Services;
|
||||
|
||||
use PDO;
|
||||
use Doctrine\DBAL\Driver\PDOException;
|
||||
|
||||
/**
|
||||
* Class Database
|
||||
* @package App\Console\Services
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
/**
|
||||
* Create the base connection DSN, optionally include the DB name
|
||||
* @param $host
|
||||
* @param $port
|
||||
* @param null $name
|
||||
* @return string
|
||||
*/
|
||||
public function createDsn($host, $port, $name=null)
|
||||
{
|
||||
$conn = config('database.default');
|
||||
$dsn = "$conn:host=$host;port=$port";
|
||||
if(filled($name)) {
|
||||
$dsn .= ';dbname='.$name;
|
||||
}
|
||||
|
||||
return $dsn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $dsn
|
||||
* @param $user
|
||||
* @param $pass
|
||||
* @return PDO
|
||||
* @throws \PDOException
|
||||
*/
|
||||
public function createPDO($dsn, $user, $pass)
|
||||
{
|
||||
try {
|
||||
$conn = new PDO($dsn, $user, $pass);
|
||||
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
||||
} catch (\PDOException $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $conn;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user