|
1
|
|
|
<?php |
|
2
|
|
|
namespace Zewa; |
|
3
|
|
|
|
|
4
|
|
|
use Zewa\Exception\ConfigException; |
|
5
|
|
|
|
|
6
|
|
|
class Config |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* Loaded Configuration Items |
|
10
|
|
|
* |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $configuration = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Path to Configuration Folder |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $path; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Configuration file extension |
|
24
|
|
|
*/ |
|
25
|
|
|
const CONFIG_FILE_EXTENSION = ".php"; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Config constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param $configFolderPath |
|
31
|
|
|
*/ |
|
32
|
65 |
|
public function __construct($configFolderPath = null) |
|
33
|
|
|
{ |
|
34
|
65 |
|
if (is_null($configFolderPath)) { |
|
35
|
29 |
|
$configFolderPath = APP_PATH . DIRECTORY_SEPARATOR . 'Config'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
65 |
|
$this->setPath($configFolderPath); |
|
39
|
65 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sets the configuration folder path |
|
43
|
|
|
* |
|
44
|
|
|
* @param $path |
|
45
|
|
|
*/ |
|
46
|
65 |
|
protected function setPath($path) |
|
47
|
|
|
{ |
|
48
|
65 |
|
$this->path = $path; |
|
49
|
65 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Loads a configuration file in to memory |
|
53
|
|
|
* |
|
54
|
|
|
* @param $key |
|
55
|
|
|
* @throws ConfigException when file is missing |
|
56
|
|
|
*/ |
|
57
|
42 |
|
protected function loadConfigFile(string $key) |
|
58
|
|
|
{ |
|
59
|
42 |
|
$key = strtolower($key); |
|
60
|
42 |
|
$filename = $this->path . DIRECTORY_SEPARATOR . ucfirst($key) . Config::CONFIG_FILE_EXTENSION; |
|
61
|
|
|
|
|
62
|
42 |
|
if (file_exists($filename)) { |
|
63
|
41 |
|
$this->configuration[$key] = require $filename; |
|
64
|
41 |
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
throw new ConfigException('Unable to load resource: ' . $filename); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get Configuration Item |
|
72
|
|
|
* |
|
73
|
|
|
* @param $key |
|
74
|
|
|
* |
|
75
|
|
|
* @return \stdClass |
|
76
|
|
|
* @throws ConfigException when config file not found |
|
77
|
|
|
*/ |
|
78
|
43 |
|
public function get(string $key) |
|
79
|
|
|
{ |
|
80
|
43 |
|
$key = strtolower($key); |
|
81
|
|
|
|
|
82
|
43 |
|
if (empty($this->configuration[$key])) { |
|
83
|
42 |
|
$this->loadConfigFile($key); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
42 |
|
return $this->configuration[$key]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param string $key |
|
91
|
|
|
* @param $value mixed array|string |
|
92
|
|
|
*/ |
|
93
|
1 |
|
public function set(string $key, $value) |
|
94
|
|
|
{ |
|
95
|
1 |
|
$key = strtolower($key); |
|
96
|
1 |
|
$this->configuration[$key] = $value; |
|
97
|
1 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|