1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yarak\Config; |
4
|
|
|
|
5
|
|
|
use Phalcon\DI; |
6
|
|
|
|
7
|
|
|
class Config |
8
|
|
|
{ |
9
|
|
|
use PathHelpers; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Instance of self. |
13
|
|
|
* |
14
|
|
|
* @var Config |
15
|
|
|
*/ |
16
|
|
|
private static $instance; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Yarak config array. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $configArray; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Default setting values. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
const DEFAULTS = [ |
31
|
|
|
'migratorType' => 'fileDate', |
32
|
|
|
'migrationRepository' => 'database', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Private constructor. |
37
|
|
|
* |
38
|
|
|
* @param array $configArray |
39
|
|
|
*/ |
40
|
|
|
private function __construct(array $configArray) |
41
|
|
|
{ |
42
|
|
|
$this->configArray = $configArray; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get instance of self with config array set. |
47
|
|
|
* |
48
|
|
|
* @param array $configArray |
49
|
|
|
* |
50
|
|
|
* @return Config |
51
|
|
|
*/ |
52
|
|
|
public static function getInstance(array $configArray = []) |
53
|
|
|
{ |
54
|
|
|
if (empty(self::$instance)) { |
55
|
|
|
if (empty($configArray)) { |
56
|
|
|
$configArray = DI::getDefault() |
57
|
|
|
->getShared('yarak') |
58
|
|
|
->getConfigArray(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
self::$instance = new self($configArray); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return self::$instance; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get a value from the config array. |
69
|
|
|
* |
70
|
|
|
* @param string|array $value |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function get($value) |
75
|
|
|
{ |
76
|
|
|
$current = $this->configArray; |
77
|
|
|
|
78
|
|
|
foreach ($this->makeArray($value) as $configItem) { |
79
|
|
|
if (!isset($current[$configItem])) { |
80
|
|
|
return $this->getDefault($configItem); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$current = $current[$configItem]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $current; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return true if config array has given value. |
91
|
|
|
* |
92
|
|
|
* @param mixed $value |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function has($value) |
97
|
|
|
{ |
98
|
|
|
return !($this->get($value) === null); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get a setting's default value. |
103
|
|
|
* |
104
|
|
|
* @param string $value |
105
|
|
|
* |
106
|
|
|
* @return mixed|null |
107
|
|
|
*/ |
108
|
|
|
public function getDefault($value) |
109
|
|
|
{ |
110
|
|
|
if (array_key_exists($value, self::DEFAULTS)) { |
111
|
|
|
return self::DEFAULTS[$value]; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set an item in the config. |
117
|
|
|
* |
118
|
|
|
* @param mixed $keys |
119
|
|
|
* @param mixed $value |
120
|
|
|
*/ |
121
|
|
|
public function set($keys, $value) |
122
|
|
|
{ |
123
|
|
|
$temp = &$this->configArray; |
124
|
|
|
|
125
|
|
|
foreach ($this->makeArray($keys) as $key) { |
126
|
|
|
$temp = &$temp[$key]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$temp = $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Remove an item from the config. |
134
|
|
|
* |
135
|
|
|
* @param mixed $keys |
136
|
|
|
*/ |
137
|
|
|
public function remove($keys) |
138
|
|
|
{ |
139
|
|
|
$keys = $this->makeArray($keys); |
140
|
|
|
|
141
|
|
|
$temp = &$this->configArray; |
142
|
|
|
|
143
|
|
|
foreach ($keys as $key) { |
144
|
|
|
if ($key === $keys[count($keys) - 1]) { |
145
|
|
|
unset($temp[$key]); |
146
|
|
|
} else { |
147
|
|
|
$temp = &$temp[$key]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Return the config array. |
154
|
|
|
* |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
public function toArray() |
158
|
|
|
{ |
159
|
|
|
return $this->configArray; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Make a variable an array if not one already. |
164
|
|
|
* |
165
|
|
|
* @param mixed $value |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
protected function makeArray($value) |
170
|
|
|
{ |
171
|
|
|
if (!is_array($value)) { |
172
|
|
|
return [$value]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $value; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|