-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectConfig.php
215 lines (184 loc) · 4.85 KB
/
ConnectConfig.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
namespace dc\yukon;
//require_once('../../dc/yukon/config.php');
require_once(dirname(__FILE__).'\config.php');
// Structure of parameters used for database connection attempt.
interface iConnectConfig
{
function get_db_type(); // Return database type.
function get_charset(); // Get character set.
function get_error(); // Return error handler.
function get_db_host(); // Return host name.
function get_db_name(); // Return logical database name.
function get_db_user(); // Return user.
function get_db_password(); // Return password.
function set_charset($value); // Charset type (example: UTF-8).
function set_db_type(string $value); // Set database type.
function set_error(Error $value); // Set error handler.
function set_db_host($value); // Set host name.
function set_db_name(string $value); // Set logical database name.
function set_db_user(string $value); // Set user.
function set_db_password(string $value); // Set password.
}
class ConnectConfig implements iConnectConfig
{
private $charset = NULL; // Character set.
private $db_type = NULL; // Database host type.
private $error = NULL; // Internal exception handling toggle.
private $host = NULL; // Server name or address.
private $name = NULL; // Database name.
private $user = NULL; // User name to access database.
private $password = NULL; // Password for user to access database.
public function __construct(string $config_file = NULL, Error $error = NULL)
{
// Populate defaults.
$this->db_type = DEFAULTS::DB_TYPE;
$this->error = $this->construct_error($error);
$this->charset = DEFAULTS::CHARSET;
if($config_file)
{
$this->populate_config($config_file);
}
}
// Accessors & Mutators.
public function get_charset()
{
return $this->charset;
}
public function set_charset($value)
{
$this->charset = $value;
}
public function get_db_type()
{
return $this->db_type;
}
public function set_db_type(string $value)
{
$this->db_type = $this->db_type_string_to_const($value);
}
public function get_error()
{
return $this->error;
}
public function set_error(Error $value)
{
$this->error = $value;
}
public function get_db_host()
{
return $this->host;
}
public function set_db_host($value)
{
$this->host = $value;
}
public function get_db_name()
{
return $this->name;
}
public function set_db_name(string $value)
{
$this->name = $value;
}
public function get_db_password()
{
return $this->password;
}
public function set_db_password(string $value)
{
$this->password = $value;
}
public function get_db_user()
{
return $this->user;
}
public function set_db_user(string $value)
{
$this->user = $value;
}
private function db_type_string_to_const(string $value)
{
if($value == "MSSQL")
{
return DB_TYPES::MSSQL;
}
else if($value == "MYSQL")
{
return DB_TYPES::MYSQL;
}
}
// Constructors
private function construct_error(Error $value = NULL)
{
$result = NULL; // Final connection result.
// Verify argument is an object.
$is_object = is_object($value);
if($is_object)
{
$result = $value;
}
else
{
$result = new Error();
}
// Populate member with result.
$this->error = $result;
return $result;
}
/*
* Populates member data from supplied
* config file.
*
* 1. Reads config file secion matched to
* full class name (including namepsace).
*
* 2. Values in config are sent to matched
* mutator. Example:
*
* Config: user_name = "John Doe"
* Method: $this->set_user_name($value);
*/
public function populate_config(string $config_file)
{
/*
* If any part of this code fails we need to
* consider it fatal and stop execution.
* Throw an exception for any kind of notice
* or warning so we can catch and handle it.
*/
set_error_handler(function ($severity, $message, $file, $line) {
throw new \ErrorException($message, $severity, $severity, $file, $line);
});
/*
* Parse config into array, get class specfic
* section and pass values into members.
*/
try
{
$config_array = parse_ini_file($config_file, TRUE);
$section_array = $config_array[__CLASS__];
// Interate through each class method.
foreach(get_class_methods($this) as $method)
{
$key = str_replace('set_', '', $method);
/*
* If there is an array element with key matching
* current method name, then the current method
* is a set mutator for the element. Populate
* the set method with the element's value.
*/
if(isset($section_array[$key]))
{
$this->$method($section_array[$key]);
}
}
}
catch(\Exception $exception)
{
error_log(__CLASS__.' Fatal Error: '.$exception->getMessage());
die(__NAMESPACE__.' Fatal Error: Failed to read values from config file. Please contact administrator.');
}
}
}
?>