1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stu\Orm\Transaction; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Configuration; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\DriverManager; |
8
|
|
|
use Doctrine\DBAL\Logging\Middleware; |
9
|
|
|
use Doctrine\DBAL\Tools\DsnParser; |
10
|
|
|
use Noodlehaus\ConfigInterface; |
11
|
|
|
use Stu\Component\Logging\Sql\SqlLogger; |
12
|
|
|
use Stu\Module\Config\StuConfigInterface; |
13
|
|
|
|
14
|
|
|
class ConnectionFactory implements ConnectionFactoryInterface |
15
|
|
|
{ |
16
|
2 |
|
public function __construct( |
17
|
|
|
private ConfigInterface $config, |
18
|
|
|
private StuConfigInterface $stuConfig, |
19
|
|
|
private SqlLogger $sqlLogger |
20
|
|
|
) { |
21
|
2 |
|
} |
22
|
|
|
|
23
|
2 |
|
public function createConnection(): Connection |
24
|
|
|
{ |
25
|
|
|
//use sqlite database |
26
|
2 |
|
if ($this->stuConfig->getDbSettings()->useSqlite()) { |
27
|
2 |
|
$dsnParser = new DsnParser(['sqlite' => 'pdo_sqlite']); |
28
|
2 |
|
$connectionParams = $dsnParser |
29
|
2 |
|
->parse('sqlite:///:memory:'); |
30
|
|
|
|
31
|
2 |
|
return DriverManager::getConnection($connectionParams); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$configuration = null; |
35
|
|
|
if ($this->stuConfig->getDebugSettings()->getSqlLoggingSettings()->isActive()) { |
36
|
|
|
$configuration = new Configuration(); |
37
|
|
|
$configuration->setMiddlewares([new Middleware($this->sqlLogger)]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return DriverManager::getConnection([ |
41
|
|
|
'driver' => 'pdo_pgsql', |
42
|
|
|
'user' => $this->config->get('db.user'), |
43
|
|
|
'password' => $this->config->get('db.pass'), |
44
|
|
|
'dbname' => $this->config->get('db.database'), |
45
|
|
|
'host' => $this->config->get('db.host'), |
46
|
|
|
'charset' => 'utf8', |
47
|
|
|
], $configuration); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|