1 | <?php |
||
19 | class PDOProvider |
||
20 | { |
||
21 | const PROTOCOL_POSTGRES = "pgsql"; |
||
22 | const PROTOCOL_MYSQL = "mysql"; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $protocol; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $dsn; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $username; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $password; |
||
43 | |||
44 | /** |
||
45 | * @var array |
||
46 | */ |
||
47 | private $options; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $host; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $port; |
||
58 | |||
59 | /** |
||
60 | * @var PDO|null |
||
61 | */ |
||
62 | private $pdo; |
||
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 | 1 | $port = $default_port[$protocol]; |
|
86 | } |
||
87 | |||
88 | 1 | if ($host === null) { |
|
89 | 1 | $host = "localhost"; |
|
90 | } |
||
91 | |||
92 | 1 | $this->protocol = $protocol; |
|
93 | 1 | $this->dsn = "{$protocol}:host={$host};port={$port};dbname={$dbname}"; |
|
94 | 1 | $this->username = $username; |
|
95 | 1 | $this->password = $password; |
|
96 | 1 | $this->options = $options ?: []; |
|
97 | 1 | $this->host = $host; |
|
98 | 1 | $this->port = $port; |
|
99 | 1 | } |
|
100 | |||
101 | /** |
||
102 | * @return PDO |
||
103 | */ |
||
104 | 1 | public function getPDO() |
|
105 | { |
||
106 | 1 | if (! isset($this->pdo)) { |
|
107 | 1 | $this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options); |
|
108 | } |
||
109 | |||
110 | 1 | return $this->pdo; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return string database procol name (one of the PROTOCOL_* class constants) |
||
115 | */ |
||
116 | 1 | public function getProtocol() |
|
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | 1 | public function getDSN() |
|
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | 1 | public function getUsername() |
|
136 | |||
137 | /** |
||
138 | * @return string |
||
139 | */ |
||
140 | 1 | public function getPassword() |
|
144 | |||
145 | /** |
||
146 | * @return array |
||
147 | */ |
||
148 | 1 | public function getOptions() |
|
152 | |||
153 | /** |
||
154 | * @return string |
||
155 | */ |
||
156 | 1 | public function getHost() |
|
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | 1 | public function getPort() |
|
168 | } |
||
169 |