-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathENV.php
224 lines (185 loc) · 4.82 KB
/
ENV.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
declare(strict_types=1);
/**
* Gazelle\ENV
*
* The PHP singleton is considered bad design for nebulous reasons,
* but for securely loading a site config it does exactly what we need:
*
* - ensure that only one instance of itself can ever exist
* - load the instance everywhere we need to do $app->env->configValue
* - no memory penalty because of multiple app instances
* - static values in config/foo.php are immutable
* - site configs don't exist in the constants table
* - separate public and private config values
*
* @see https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php
*
*
* Oh yeah, it also supports all of Laravel's Collection methods.
* The underlying structure is a Collection not an ArrayObject.
*
* @see https://laravel.com/docs/master/collections#available-methods
*/
namespace Gazelle;
class ENV
{
# disinstantiates itself
private static ?self $instance = null;
# config option receptacles
public RecursiveCollection $public; # site meta, options, resources, etc.
private RecursiveCollection $private; # passwords, app keys, database, etc.
/**
* __functions
*/
# prevents outside construction
public function __construct()
{
# would be expensive, e.g.,
# $env = new ENV();
return;
}
# prevents multiple instances
public function __clone()
{
return trigger_error(
"clone not allowed",
E_USER_ERROR
);
}
# prevents unserializing
public function __wakeup()
{
return trigger_error(
"wakeup not allowed",
E_USER_ERROR
);
}
/** */
/**
* __get
*
* @param mixed $key the key to get
* @return mixed the value of the key
*/
public function __get(mixed $key): mixed
{
return $this->public[$key] ?? null;
}
/**
* __set
*
* @param mixed $key the key to set
* @param mixed $value the value to set
* @return void
*/
public function __set(mixed $key, mixed $value): void
{
$this->public[$key] = $this->collect($value);
}
/**
* __isset
*
* @param mixed $key the key to check
* @return bool whether the key is set
*/
public function __isset(mixed $key): bool
{
return isset($this->public[$key]);
}
/**
* __unset
*
* @param mixed $key the key to unset
* @return void
*/
public function __unset(mixed $key): void
{
unset($this->public[$key]);
}
/**
* __call
*
* @param string $method the method to call
* @param array $arguments the arguments to pass
*/
public function __call(string $method, array $arguments = []): mixed
{
if (!method_exists($this->public, $method)) {
return trigger_error(
"the method {$method} doesn't exist",
E_USER_ERROR
);
}
return $this->public->$method($arguments);
}
/** */
/**
* go
*
* Calls its self's creation or returns itself.
*
* @param array $options the options to use
* @return self
*/
public static function go(array $options = []): self
{
if (!self::$instance) {
self::$instance = new self();
self::$instance->factory($options);
}
return self::$instance;
}
/**
* factory
*
* @param array $options the options to use
* @return void
*/
private function factory(array $options = []): void
{
$this->public = new RecursiveCollection();
$this->private = new RecursiveCollection();
}
/**
* collect
*
* Converts stuff into a RecursiveCollection
*
* @param mixed $array the stuff to convert
* @return mixed a scalar or RecursiveCollection
*
* @see https://stackoverflow.com/a/54131002
*/
public function collect(mixed $array = []): mixed
{
if (is_iterable($array)) {
$return = new RecursiveCollection($array);
foreach ($return as &$item) {
$item = $this->collect($item);
}
return $return;
}
return $array;
}
/**
* private
*
* Sets a private key if $value !== null.
* Otherwise, returns the value of $key.
* This returns the value on set, not void!
*
* @param mixed $key the key to set or get
* @param mixed $value the value to set
* @return mixed the value of the key
*/
public function private(mixed $key, mixed $value = null): mixed
{
# get
if (is_null($value)) {
return $this->private[$key] ?? null;
}
# set
return $this->private[$key] = $this->collect($value);
}
} # class