1 | <?php |
||
9 | abstract class AbstractDBALResource implements ResourceInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var Connection |
||
13 | */ |
||
14 | protected $connection; |
||
15 | /** |
||
16 | * @var ConfigInterface |
||
17 | */ |
||
18 | protected $config; |
||
19 | |||
20 | /** |
||
21 | * @param ConfigInterface $config |
||
22 | */ |
||
23 | 31 | public function __construct(ConfigInterface $config) |
|
24 | { |
||
25 | 31 | $this->config = $config; |
|
26 | 31 | $this->open(); |
|
27 | 31 | } |
|
28 | |||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 31 | public function open() |
|
33 | { |
||
34 | $params = [ |
||
35 | 31 | 'dbname' => $this->config['db_name'], |
|
36 | 31 | 'user' => $this->config['db_user'], |
|
37 | 31 | 'password' => $this->config['db_password'], |
|
38 | 31 | 'host' => $this->config['db_host'], |
|
39 | 31 | 'port' => $this->config['db_port'], |
|
40 | 31 | ]; |
|
41 | 31 | if (isset($this->config['db_pdo'])) { |
|
42 | 13 | $params['pdo'] = $this->config['db_pdo']; |
|
43 | 13 | } |
|
44 | 31 | if (isset($this->config['db_url'])) { |
|
45 | $params['url'] = $this->config['db_url']; |
||
46 | } |
||
47 | 31 | $params['driverOptions'] = $this->getDriverOptions(); |
|
48 | 31 | $this->connection = new Connection($params, $this->getDriver()); |
|
49 | 31 | return $this->connection->connect(); |
|
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return array |
||
54 | */ |
||
55 | protected function getDriverOptions() |
||
56 | { |
||
57 | return []; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return DriverInterface |
||
62 | */ |
||
63 | abstract protected function getDriver(); |
||
64 | |||
65 | /** |
||
66 | * {@inheritdoc} |
||
67 | */ |
||
68 | 10 | public function close() |
|
72 | |||
73 | /** |
||
74 | * GC opened resource |
||
75 | */ |
||
76 | public function __destruct() |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function isActive() |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | 2 | public function startTransaction() |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | 2 | public function commit() |
|
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function rollback() |
||
114 | |||
115 | /** |
||
116 | * @return Connection |
||
117 | */ |
||
118 | 18 | public function getConnection() |
|
122 | } |
||
123 |