1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Sinergi\Config; |
4
|
|
|
|
5
|
|
|
require_once __DIR__ . "/DotenvHelper.php"; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use ArrayAccess; |
9
|
|
|
|
10
|
|
|
class Collection extends Configuration implements ArrayAccess |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param array|Configuration $config |
14
|
|
|
* @return Collection |
15
|
|
|
*/ |
16
|
|
|
public static function factory($config) |
17
|
|
|
{ |
18
|
|
|
$factory = new Factory(); |
19
|
|
|
return $factory($config); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $container = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set a config |
29
|
|
|
* |
30
|
|
|
* @param string $name |
31
|
|
|
* @param array $config |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function set($name, $config) |
35
|
|
|
{ |
36
|
|
|
if (!isset($this->container[$name])) { |
37
|
|
|
$this->container[$name] = $config; |
38
|
|
|
} else { |
39
|
|
|
$this->container[$name] = array_merge($this->container[$name], $config); |
40
|
|
|
} |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
*/ |
47
|
|
|
public function remove($name) |
48
|
|
|
{ |
49
|
|
|
unset($this->container[$name]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a config |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* @param mixed $default |
57
|
|
|
* @throws InvalidArgumentException |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public function get($name, $default = null) |
61
|
|
|
{ |
62
|
|
|
if (!is_string($name) || empty($name)) { |
63
|
|
|
throw new InvalidArgumentException("Parameter \$name passed to Config::get() is not a valid string resource"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
list($file, $key, $sub) = Parser::getKey($name); |
67
|
|
|
|
68
|
|
|
if (!isset($this->container[$file])) { |
69
|
|
|
$this->container[$file] = Loader::load( |
70
|
|
|
$this->paths, |
71
|
|
|
$this->environment, |
72
|
|
|
$file |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return Parser::getValue($this->container[$file], $key, $sub, $default); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function reset() |
83
|
|
|
{ |
84
|
|
|
$this->container = null; |
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $name |
90
|
|
|
* @return mixed |
91
|
|
|
*/ |
92
|
|
|
public function __get($name) |
93
|
|
|
{ |
94
|
|
|
return $this->offsetGet($name); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $name |
99
|
|
|
* @param array|null $args |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function __call($name, $args = null) |
103
|
|
|
{ |
104
|
|
|
return $this->offsetGet($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $name |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function __isset($name) |
112
|
|
|
{ |
113
|
|
|
return $this->offsetExists($name); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $offset |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function offsetExists($offset) |
121
|
|
|
{ |
122
|
|
|
$item = $this->get($offset); |
123
|
|
|
return isset($item); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param int $offset |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public function offsetGet($offset) |
131
|
|
|
{ |
132
|
|
|
return $this->get($offset); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param int $offset |
137
|
|
|
* @param mixed $value |
138
|
|
|
*/ |
139
|
|
|
public function offsetSet($offset, $value) |
140
|
|
|
{ |
141
|
|
|
$this->set($offset, $value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param int $offset |
146
|
|
|
*/ |
147
|
|
|
public function offsetUnset($offset) |
148
|
|
|
{ |
149
|
|
|
$this->remove($offset); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.