|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* @license MIT |
|
4
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This file is part of the EmmetBlue project, please read the license document |
|
7
|
|
|
* available in the root level of the project |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace EmmetBlue\Core\Factory; |
|
10
|
|
|
|
|
11
|
|
|
use EmmetBlue\Core\Connection\ConnectionAdapter as Connection; |
|
12
|
|
|
use EmmetBlue\Core\Constant; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class DatabaseConnectionFactory. |
|
16
|
|
|
* Instantiates a new instance of ConnectableInterface |
|
17
|
|
|
* |
|
18
|
|
|
* @author Samuel Adeshina <[email protected]> |
|
19
|
|
|
* |
|
20
|
|
|
* @since v0.0.1 08/06/2016 14:20 |
|
21
|
|
|
*/ |
|
22
|
|
|
class DatabaseConnectionFactory |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var \PDO $connectionObject |
|
26
|
|
|
*/ |
|
27
|
|
|
private static $connectionObject; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Gets the config values defined in the database-config.json file |
|
31
|
|
|
* and uses the values to create a new connection object. |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function bootstrap() |
|
34
|
|
|
{ |
|
35
|
|
|
$databaseConfigJson = file_get_contents(Constant::getGlobals()["config-dir"]["database-config"]); |
|
36
|
|
|
|
|
37
|
|
|
$databaseConfig = json_decode($databaseConfigJson); |
|
38
|
|
|
|
|
39
|
|
|
$adapter = $databaseConfig->adapter; |
|
40
|
|
|
$server = $databaseConfig->server; |
|
41
|
|
|
$database = $databaseConfig->database; |
|
42
|
|
|
$username = $databaseConfig->username; |
|
43
|
|
|
$password = $databaseConfig->password; |
|
44
|
|
|
|
|
45
|
|
|
self::$connectionObject = new Connection($adapter, [$server, $database], $username, $password); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if ($databaseConfig->showError) |
|
48
|
|
|
{ |
|
49
|
|
|
self::$connectionObject->getConnection()->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns a new connection object |
|
55
|
|
|
* |
|
56
|
|
|
* @return \PDO |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getConnection() : \PDO |
|
59
|
|
|
{ |
|
60
|
|
|
self::bootstrap(); |
|
61
|
|
|
return self::$connectionObject->getConnection(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..