1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Temitope Olotin <[email protected]> |
4
|
|
|
* @license <https://opensource.org/license/MIT> MIT |
5
|
|
|
*/ |
6
|
|
|
namespace Laztopaz\EmojiRestfulAPI; |
7
|
|
|
|
8
|
|
|
use Dotenv\Dotenv; |
9
|
|
|
|
10
|
|
|
class DatabaseConnection |
11
|
|
|
{ |
12
|
|
|
private $capsule; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This constructor accept Capsule; a connection |
16
|
|
|
* string for connecting to the database. |
17
|
|
|
* |
18
|
|
|
* @param $capsule |
19
|
|
|
*/ |
20
|
|
|
public function __construct($capsule) |
21
|
|
|
{ |
22
|
|
|
$this->capsule = $capsule; |
23
|
|
|
self::loadEnv(); |
24
|
|
|
$this->setUpDatabase(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This method setup the PDO database connection and also |
29
|
|
|
* start the database connection. |
30
|
|
|
*/ |
31
|
|
|
private function setUpDatabase() |
32
|
|
|
{ |
33
|
|
|
$this->capsule->addConnection( |
34
|
|
|
[ |
35
|
|
|
'driver' => getenv('driver'), |
36
|
|
|
'host' => getenv('host'), |
37
|
|
|
'database' => getenv('database'), |
38
|
|
|
'username' => getenv('username'), |
39
|
|
|
'password' => getenv('password'), |
40
|
|
|
'charset' => 'utf8', |
41
|
|
|
'collation' => 'utf8_unicode_ci', |
42
|
|
|
'port' => getenv('port'), |
43
|
|
|
'prefix' => '', |
44
|
|
|
'strict' => true, |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$this->capsule->setAsGlobal(); |
48
|
|
|
$this->capsule->bootEloquent(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Load Dotenv to grant getenv() access to |
53
|
|
|
* environment variables in .env file. |
54
|
|
|
*/ |
55
|
|
|
public static function loadEnv() |
56
|
|
|
{ |
57
|
|
|
if (! getenv('APP_ENV')) { |
58
|
|
|
$dotenv = new Dotenv(__DIR__.'/../../'); |
59
|
|
|
$dotenv->load(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|