* upgrade to laravel 13 * phpstan * rollback to symfony 7.4 * fix: ModuleService merge * refactor: new rector rules
38 lines
742 B
PHP
38 lines
742 B
PHP
<?php
|
|
|
|
namespace App\Console\Services;
|
|
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
/**
|
|
* Class Database
|
|
*/
|
|
class Database
|
|
{
|
|
/**
|
|
* Create the base connection DSN, optionally include the DB name
|
|
*/
|
|
public function createDsn($host, $port, $name = null): string
|
|
{
|
|
$conn = config('database.default');
|
|
$dsn = sprintf('%s:host=%s;port=%s', $conn, $host, $port);
|
|
if (filled($name)) {
|
|
$dsn .= ';dbname='.$name;
|
|
}
|
|
|
|
return $dsn;
|
|
}
|
|
|
|
/**
|
|
* @throws PDOException
|
|
*/
|
|
public function createPDO($dsn, $user, $pass): PDO
|
|
{
|
|
$conn = new PDO($dsn, $user, $pass);
|
|
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
|
|
|
return $conn;
|
|
}
|
|
}
|