1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elchroy\PotatoORM; |
4
|
|
|
|
5
|
|
|
use Elchroy\PotatoORMExceptions\FaultyConnectionException; |
6
|
|
|
use Elchroy\PotatoORMExceptions\InvalidAdaptarException; |
7
|
|
|
use PDO; |
8
|
|
|
use PDOException; |
9
|
|
|
|
10
|
|
|
class PotatoConnector |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* [$connection PDO connection to be used to communicate with the database]. |
14
|
|
|
* |
15
|
|
|
* @var [type] PDO Connection |
16
|
|
|
*/ |
17
|
|
|
public $connection; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* [$configuration The configuration data to be used to establish the connection]. |
21
|
|
|
* |
22
|
|
|
* @var [type] |
23
|
|
|
*/ |
24
|
|
|
public $configuration; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* [__construct Set up the connection with the configuration data that is provided on instantiation of the class.]. |
28
|
|
|
* |
29
|
|
|
* @param array|null $configData [description] |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $configData = null) |
32
|
|
|
{ |
33
|
|
|
$configData == null ? $config = $this->getConfigurations() : $config = $configData; |
34
|
|
|
$this->configuration = $config; |
35
|
|
|
$this->connection = $this->setConnection(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* [setConnection Set up the connection with the configuration information]. |
40
|
|
|
*/ |
41
|
|
|
public function setConnection() |
42
|
|
|
{ |
43
|
|
|
$adaptar = $this->getAdaptar(); |
44
|
|
|
$connection = $this->connect($adaptar); |
45
|
|
|
|
46
|
|
|
return $connection; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* [connect Try setting up the connection wtith given connection parameters]. |
51
|
|
|
* |
52
|
|
|
* @param [string] $adaptar [The adapter to be used witht he connection to the database] |
|
|
|
|
53
|
|
|
* @param [string] $host [The host name for the connection] |
|
|
|
|
54
|
|
|
* @param [string] $dbname [The name of the database] |
|
|
|
|
55
|
|
|
* @param [string] $username [The username to be used if it is required] |
|
|
|
|
56
|
|
|
* @param [string] $password [The [assword to be used for the connectionif required.]] |
|
|
|
|
57
|
|
|
* |
58
|
|
|
* @return [type] [A PDO connection to the databsase] |
|
|
|
|
59
|
|
|
*/ |
60
|
|
|
public function connect($adaptar) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
|
|
$connection = $this->connectDriver($adaptar); |
64
|
|
|
} catch (PDOException $e) { |
65
|
|
|
$message = $e->getMessage(); |
66
|
|
|
$this->throwFaultyConnectionException($message); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $connection; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* [connectDriver Check which driver is chosen and create a PDO connection based on the driver information.] |
74
|
|
|
* Throw an InvalidAdaptarException if the given driver is invalid, does not exist or is not compatible with PDO. |
75
|
|
|
* |
76
|
|
|
* @param [string] $adaptar [The adaptar/driver name used to create PDO connection.] |
|
|
|
|
77
|
|
|
* @param [string] $host [The hostname to be used for the PDO connection if mysql is chosen.] |
|
|
|
|
78
|
|
|
* @param [string] $dbname [The name of the database of the PDO connection.] |
|
|
|
|
79
|
|
|
* @param [string] $username [The username to be used for the PDO connection if mysql is chosen.] |
|
|
|
|
80
|
|
|
* @param [string] $password [The password to be used for the PDO connection id mysql is chosen.] |
|
|
|
|
81
|
|
|
* |
82
|
|
|
* @return [type] [description] |
|
|
|
|
83
|
|
|
*/ |
84
|
|
|
public function connectDriver($adaptar) |
85
|
|
|
{ |
86
|
|
|
switch ($adaptar) { |
87
|
|
|
case 'sqlite': |
88
|
|
|
$connection = $this->sqliteConnect($adaptar); |
89
|
|
|
break; |
90
|
|
|
case 'mysql': |
91
|
|
|
$host = $this->getHost(); |
92
|
|
|
$dbname = $this->getDBName(); |
93
|
|
|
$username = $this->getUsername(); |
94
|
|
|
$password = $this->getPassword(); |
95
|
|
|
$connection = $this->mysqlConnect($adaptar, $host, $dbname, $username, $password); |
96
|
|
|
break; |
97
|
|
|
default: |
98
|
|
|
$this->throwInvalidAdapterException($adaptar); |
99
|
|
|
} |
100
|
|
|
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
|
|
|
101
|
|
|
|
102
|
|
|
return $connection; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* [mysqlConnect Create a MySQL PDO connection with the mysql driver.]. |
107
|
|
|
* |
108
|
|
|
* @param [string] $adaptar [The adaptar name used to create PDO connection.] |
|
|
|
|
109
|
|
|
* @param [string] $host [The hostname to be used for the PDO connection.] |
|
|
|
|
110
|
|
|
* @param [string] $dbname [The name of the database of the PDO connection.] |
|
|
|
|
111
|
|
|
* @param [string] $username [The username to be used for the PDO connection.] |
|
|
|
|
112
|
|
|
* @param [string] $password [The password to be used for the PDO connection.] |
|
|
|
|
113
|
|
|
* |
114
|
|
|
* @return [type] A PDO connection done with a mysql driver |
|
|
|
|
115
|
|
|
*/ |
116
|
|
|
public function mysqlConnect($adaptar, $host, $dbname, $username, $password) |
117
|
|
|
{ |
118
|
|
|
$connection = new PDO("$adaptar:host=$host;dbname=$dbname", $username, $password); |
119
|
|
|
|
120
|
|
|
return $connection; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* [sqliteConnect Create an SQLite connection if the selected PDO drver is sqlite.]. |
125
|
|
|
* |
126
|
|
|
* @param [string] $adaptar The sqlite driver name |
|
|
|
|
127
|
|
|
* @param [type] $dbFile [The database file. If this is null, then get the database file using the getSqliteFile method.] |
|
|
|
|
128
|
|
|
* |
129
|
|
|
* @return [type] A PDO connection done with am sqlite driver. |
|
|
|
|
130
|
|
|
*/ |
131
|
|
|
public function sqliteConnect($adaptar, $dbFile = null) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$dbFile = $dbFile == null ? $this->getSqliteFile() : $dbFile; |
134
|
|
|
$connection = new PDO("sqlite:../$dbFile"); |
135
|
|
|
|
136
|
|
|
return $connection; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* [getSqliteFile Get the file location of the slqlite file if it is preferred to use an sqlite pdo connection.]. |
141
|
|
|
* |
142
|
|
|
* @param [type] $path [The path to the sqlite database file.] |
|
|
|
|
143
|
|
|
* |
144
|
|
|
* @return [type] [The path to the sqlite file if provided or a default file path if the path in the provided argument is null.] |
|
|
|
|
145
|
|
|
*/ |
146
|
|
|
public function getSqliteFile() |
147
|
|
|
{ |
148
|
|
|
$path = $this->configuration['sqlite_file']; |
149
|
|
|
|
150
|
|
|
return $path; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* [getConfigurations Get the configuration data from the file path]. |
155
|
|
|
* |
156
|
|
|
* @param [type] $filepath [The file path where the connection configuration information are located.] |
|
|
|
|
157
|
|
|
* |
158
|
|
|
* @return [array] [An array of the configuration information after parsing the information.] |
|
|
|
|
159
|
|
|
*/ |
160
|
|
|
public function getConfigurations($filepath = null) |
161
|
|
|
{ |
162
|
|
|
$file = ($filepath == null ? $this->getConfigFilePath() : $filepath); |
163
|
|
|
|
164
|
|
|
return parse_ini_file($file); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* [getConfigFilePath Get the file path of the file where the configuration lies.]. |
169
|
|
|
* |
170
|
|
|
* @return [string] [The file path of the location where the confuiguration information lie.] |
|
|
|
|
171
|
|
|
*/ |
172
|
|
|
public function getConfigFilePath($path = null) |
173
|
|
|
{ |
174
|
|
|
return $path == null ? __DIR__.'/../config.ini' : $path; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* [getAdaptar Get the name of the adapter to be used for the connection.]. |
179
|
|
|
* |
180
|
|
|
* @return [string] [The name of the adapter.] |
|
|
|
|
181
|
|
|
*/ |
182
|
|
|
public function getAdaptar() |
183
|
|
|
{ |
184
|
|
|
return $this->configuration['adaptar']; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* [getHost Get the name of the host to be used for the connection.]. |
189
|
|
|
* |
190
|
|
|
* @return [string] [The name of the host.] |
|
|
|
|
191
|
|
|
*/ |
192
|
|
|
public function getHost() |
193
|
|
|
{ |
194
|
|
|
return $this->configuration['host']; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* [getDBName Get the name of the database where information/data will eb stored.]. |
199
|
|
|
* |
200
|
|
|
* @return [string] [The name of the database.] |
|
|
|
|
201
|
|
|
*/ |
202
|
|
|
public function getDBName() |
203
|
|
|
{ |
204
|
|
|
return $this->configuration['dbname']; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* [getUsername Get the username for the connection to the database.]. |
209
|
|
|
* |
210
|
|
|
* @return [string] [The username for the connection to the database.] |
|
|
|
|
211
|
|
|
*/ |
212
|
|
|
public function getUsername() |
213
|
|
|
{ |
214
|
|
|
return $this->configuration['username']; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* [getPassword Get the password of the user of the connection.]. |
219
|
|
|
* |
220
|
|
|
* @return [string] [The passwird of the user.] |
|
|
|
|
221
|
|
|
*/ |
222
|
|
|
public function getPassword() |
223
|
|
|
{ |
224
|
|
|
return $this->configuration['password']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* [throwFaultyConnectionException Throw an exception if along the lime the connection is not setup correctly]. |
229
|
|
|
* |
230
|
|
|
* @param [string] $message [The message to be related in event of this exception.] |
|
|
|
|
231
|
|
|
* |
232
|
|
|
* @return [type] [An inherited PDO exception with a customized message.] |
|
|
|
|
233
|
|
|
*/ |
234
|
|
|
public function throwFaultyConnectionException($message) |
235
|
|
|
{ |
236
|
|
|
throw new FaultyConnectionException($message); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* [throwInvalidAdapterException Throw an exception if the adapter or driver is invalid, does not exist or is not supported by PDO. |
241
|
|
|
* |
242
|
|
|
* @param [string] $adaptar [The adaptar that should be used for the PDO connection.] |
|
|
|
|
243
|
|
|
* |
244
|
|
|
* @return [type] [An inherited PDO exception with a customized message.] |
|
|
|
|
245
|
|
|
*/ |
246
|
|
|
public function throwInvalidAdapterException($adaptar) |
247
|
|
|
{ |
248
|
|
|
$message = "Invalid Adapter $adaptar : Please provide a driver for the connection to the database."; |
249
|
|
|
throw new InvalidAdaptarException($message); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.