|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebServCo\Framework\Libraries; |
|
3
|
|
|
|
|
4
|
|
|
final class Config extends \WebServCo\Framework\AbstractLibrary |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* Stores configuration data. |
|
8
|
|
|
*/ |
|
9
|
|
|
private $config = []; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Application environment. |
|
13
|
|
|
*/ |
|
14
|
|
|
private $env; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Add base setting data. |
|
18
|
|
|
* |
|
19
|
|
|
* Keys will be preserved. |
|
20
|
|
|
* Existing values will be overwritten. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $setting Name of setting to load. |
|
23
|
|
|
* @param mixed $data Data to add. |
|
24
|
|
|
*/ |
|
25
|
|
|
final public function add($setting, $data) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->config = \WebServCo\Framework\ArrayStorage::append( |
|
28
|
|
|
$this->config, |
|
29
|
|
|
[$setting => $data] |
|
30
|
|
|
); |
|
31
|
|
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Load configuration data from a file. |
|
36
|
|
|
* |
|
37
|
|
|
* Data is appended to any existing data. |
|
38
|
|
|
* @param string $setting Name of setting to load. |
|
39
|
|
|
* @param string $path Directory where the file is located. |
|
|
|
|
|
|
40
|
|
|
* File name must be <$setting>.php |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
final public function load($setting, $pathProject) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$pathFull = "{$pathProject}config/".$this->getEnv()."/{$setting}.php"; |
|
46
|
|
|
if (!is_readable($pathFull)) { |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
$data = (include $pathFull); |
|
50
|
|
|
return is_array($data) ? $data : false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Sets a configuration value. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $setting Can be an array, a string, |
|
57
|
|
|
* or a special formatted string |
|
58
|
|
|
* (eg 'app|path|project'). |
|
59
|
|
|
* @param mixed $value The value to be stored. |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool True on success and false on failure. |
|
62
|
|
|
*/ |
|
63
|
|
|
final public function set($setting, $value) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
if (empty($setting)) { |
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
$this->config = \WebServCo\Framework\ArrayStorage::set($this->config, $setting, $value); |
|
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
final public function get($setting, $defaultValue = false) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
return \WebServCo\Framework\ArrayStorage::get($this->config, $setting, $defaultValue); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set application environment value. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $env |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
|
|
final public function setEnv($env = null) |
|
|
|
|
|
|
85
|
|
|
{ |
|
86
|
|
|
if (in_array($env, \WebServCo\Framework\Environment::getOptions())) { |
|
87
|
|
|
$this->env = $env; |
|
88
|
|
|
} else { |
|
89
|
|
|
$this->env = \WebServCo\Framework\Environment::ENV_DEV; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get application environment value. |
|
97
|
|
|
* |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
|
|
final public function getEnv() |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
return $this->env ?: \WebServCo\Framework\Environment::ENV_DEV; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|