1 | <?php |
||
19 | class PDOProvider |
||
20 | { |
||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $protocol; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $dsn; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $username; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $password; |
||
40 | |||
41 | /** |
||
42 | * @var array |
||
43 | */ |
||
44 | private $options; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $host; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $port; |
||
55 | |||
56 | /** |
||
57 | * @var PDO|null |
||
58 | */ |
||
59 | private $pdo; |
||
60 | |||
61 | const PROTOCOL_POSTGRES = "pgsql"; |
||
62 | const PROTOCOL_MYSQL = "mysql"; |
||
63 | |||
64 | /** |
||
65 | * @param string $protocol database protocol name (one of the PROTOCOL_* class constants) |
||
66 | * @param string $dbname name of the database to connect to |
||
67 | * @param string $username |
||
68 | * @param string $password |
||
69 | * @param array|null $options PDO constructor options |
||
70 | * @param string|null $host optional hostname; defaults to "localhost" |
||
71 | * @param int|null $port optional port-number; defaults to the standard port-number for the given $db type |
||
72 | */ |
||
73 | 1 | public function __construct($protocol, $dbname, $username, $password, $options = null, $host = null, $port = null) |
|
74 | { |
||
75 | 1 | static $default_port = [ |
|
76 | self::PROTOCOL_MYSQL => 3306, |
||
77 | self::PROTOCOL_POSTGRES => 5432, |
||
78 | ]; |
||
79 | |||
80 | 1 | if (! isset($default_port[$protocol])) { |
|
81 | 1 | throw new InvalidArgumentException("unsupported DBMS type: {$protocol}"); |
|
82 | } |
||
83 | |||
84 | 1 | if ($port === null) { |
|
85 | $port = $default_port[$protocol]; |
||
86 | } |
||
87 | |||
88 | 1 | $this->protocol = $protocol; |
|
89 | 1 | $this->dsn = "{$protocol}:host={$host};port={$port};dbname={$dbname}"; |
|
90 | 1 | $this->username = $username; |
|
91 | 1 | $this->password = $password; |
|
92 | 1 | $this->options = $options ?: []; |
|
93 | 1 | $this->host = $host ?: "localhost"; |
|
94 | 1 | $this->port = $port; |
|
95 | 1 | } |
|
96 | |||
97 | /** |
||
98 | * @return PDO |
||
99 | */ |
||
100 | public function getPDO() |
||
101 | { |
||
102 | if (! isset($this->pdo)) { |
||
103 | $this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options); |
||
104 | } |
||
105 | |||
106 | return $this->pdo; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return string database procol name (one of the PROTOCOL_* class constants) |
||
111 | */ |
||
112 | 1 | public function getProtocol() |
|
113 | { |
||
114 | 1 | return $this->protocol; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 1 | public function getDSN() |
|
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | 1 | public function getUsername() |
|
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | 1 | public function getPassword() |
|
140 | |||
141 | /** |
||
142 | * @return array |
||
143 | */ |
||
144 | 1 | public function getOptions() |
|
148 | |||
149 | /** |
||
150 | * @return string |
||
151 | */ |
||
152 | 1 | public function getHost() |
|
156 | |||
157 | /** |
||
158 | * @return string |
||
159 | */ |
||
160 | 1 | public function getPort() |
|
164 | } |
||
165 |