1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package A simple ORM that performs basic CRUD operations |
4
|
|
|
* @author Surajudeen AKANDE <[email protected]> |
5
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
6
|
|
|
* @link http://www.github.com/andela-sakande |
7
|
|
|
* */ |
8
|
|
|
|
9
|
|
|
namespace Sirolad\DB; |
10
|
|
|
|
11
|
|
|
use PDO; |
12
|
|
|
use Dotenv\Dotenv; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class manages the database connection for PotatoORM |
16
|
|
|
* It loads environmental variables from .env file |
17
|
|
|
* It has been proven to connect with MySQL and PgSQL databases. |
18
|
|
|
* */ |
19
|
|
|
class DBConnect |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
* */ |
24
|
|
|
protected $host; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
* */ |
28
|
|
|
protected $user; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
* */ |
32
|
|
|
protected $pass; |
33
|
|
|
/** |
34
|
|
|
* @var integer |
35
|
|
|
* */ |
36
|
|
|
protected $dbport; |
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
* */ |
40
|
|
|
protected $dbtype; |
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
* */ |
44
|
|
|
protected $dbname; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* This method makes connection to the database on getting the necessary parameters. |
48
|
|
|
* @return connection to database |
49
|
|
|
* */ |
50
|
|
|
public function getConnection() |
51
|
|
|
{ |
52
|
|
|
$this->loader(); |
53
|
|
|
try { |
54
|
|
|
if ($this->dbtype === 'pgsql') { |
55
|
|
|
$conn = new PDO($this->dbtype . ':host=' . $this->host . ';port=' . $this->dbport . ';dbname=' . $this->dbname . ';user=' . $this->user . ';password=' . $this->pass); |
56
|
|
|
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
57
|
|
|
$conn->setAttribute(PDO::ATTR_PERSISTENT, false); |
58
|
|
|
} elseif ($this->dbtype === 'mysql') { |
59
|
|
|
$conn = new PDO($this->dbtype . ':host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8mb4', $this->user, $this->pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
60
|
|
|
PDO::ATTR_PERSISTENT => false]); |
61
|
|
|
} |
62
|
|
|
} catch (PDOException $e) { |
|
|
|
|
63
|
|
|
return $e->getMessage(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $conn; |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Loads up the database configuration options from the .env file |
71
|
|
|
*/ |
72
|
|
|
public function loader() |
73
|
|
|
{ |
74
|
|
|
$this->loadDotenv(); |
75
|
|
|
$this->host = getenv('DB_HOST'); |
76
|
|
|
$this->user = getenv('DB_USERNAME'); |
77
|
|
|
$this->pass = getenv('DB_PASSWORD'); |
78
|
|
|
$this->dbport = getenv('DB_PORT'); |
|
|
|
|
79
|
|
|
$this->dbname = getenv('DB_DATABASE'); |
80
|
|
|
$this->dbtype = getenv('DB_ENGINE'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Makes connection to the env file in the root folder |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function loadDotenv() |
88
|
|
|
{ |
89
|
|
|
$dotEnv = new Dotenv(__DIR__.'/../..'); |
90
|
|
|
$dotEnv->load(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.