1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Configuration; |
4
|
|
|
|
5
|
|
|
use DBAL\Database; |
6
|
|
|
|
7
|
|
|
class Config |
8
|
|
|
{ |
9
|
|
|
protected $db; |
10
|
|
|
protected $config = []; |
11
|
|
|
protected $table_config = 'config'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Constructor |
15
|
|
|
* @param Database $db This should be an instance of the database class |
16
|
|
|
* @param string $config_table If the config table is different from the default set this here |
17
|
|
|
*/ |
18
|
5 |
|
public function __construct(Database $db, $config_table = 'config') |
19
|
|
|
{ |
20
|
5 |
|
$this->db = $db; |
21
|
5 |
|
if (func_num_args() > 1) { |
22
|
2 |
|
$this->table_config = $config_table; |
23
|
|
|
} |
24
|
5 |
|
$settings = $this->db->selectAll($this->table_config); |
25
|
5 |
|
if (is_array($settings)) { |
26
|
5 |
|
foreach ($settings as $item) { |
27
|
5 |
|
$this->config[$item['setting']] = $item['value']; |
28
|
|
|
} |
29
|
|
|
} else { |
30
|
1 |
|
echo('Please make sure the config setting have been added to the database'); |
31
|
|
|
} |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the config value for a given setting |
36
|
|
|
* @param string $setting This should be the name of the setting that you wish to retrieve |
37
|
|
|
* @return mixed The setting value if it exists will be returned |
38
|
|
|
*/ |
39
|
2 |
|
public function __get($setting) |
40
|
|
|
{ |
41
|
2 |
|
return $this->config[$setting]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return all of the settings |
46
|
|
|
* @return array All of the settings will be returned |
47
|
|
|
*/ |
48
|
3 |
|
public function getAll() |
49
|
|
|
{ |
50
|
3 |
|
return $this->config; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Updates a config setting temporarily |
55
|
|
|
* @param string $setting This should be the setting name |
56
|
|
|
* @param mixed $value The new value for the setting should be added here |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
1 |
|
public function __set($setting, $value) |
60
|
|
|
{ |
61
|
1 |
|
return $this->set($setting, $value); |
62
|
|
|
} |
63
|
|
|
/** |
64
|
|
|
* Updates a config setting temporarily |
65
|
|
|
* @param string $setting This should be the setting name |
66
|
|
|
* @param mixed $value The new value for the setting should be added here |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
2 |
|
public function set($setting, $value) |
70
|
|
|
{ |
71
|
2 |
|
$this->config[$setting] = $value; |
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set a setting in the database |
77
|
|
|
* @param string $setting This should be the setting name |
78
|
|
|
* @param mixed $value The new value for the setting should be added here |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
1 |
|
public function update($setting, $value) |
82
|
|
|
{ |
83
|
1 |
|
if ($this->db->update($this->table_config, ['value' => $value], ['setting' => $setting], 1)) { |
84
|
1 |
|
$this->set($setting, $value); |
85
|
|
|
} |
86
|
1 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|