-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepository.php
318 lines (271 loc) · 7.75 KB
/
Repository.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Orchestra\Config;
use ArrayAccess;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use Orchestra\Contracts\Config\PackageRepository;
class Repository extends NamespacedItemResolver implements ArrayAccess, ConfigContract, PackageRepository
{
use Concerns\Cascading,
Concerns\Loader;
/**
* All of the configuration items.
*
* @var array
*/
protected $items = [];
/**
* All of the registered packages.
*
* @var array
*/
protected $packages = [];
/**
* Create a new configuration repository.
*/
public function __construct(LoaderInterface $loader, string $environment)
{
$this->setLoader($loader);
$this->environment = $environment;
}
/**
* Determine if the given configuration value exists.
*
* @param string $key
*
* @return bool
*/
public function has($key)
{
$default = \microtime(true);
return $this->get($key, $default) !== $default;
}
/**
* Determine if a configuration group exists.
*
* @param string $key
*
* @return bool
*/
public function hasGroup($key)
{
[$namespace, $group] = $this->parseKey($key);
return $this->loader->exists($group, $namespace);
}
/**
* Get the specified configuration value.
*
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
[$namespace, $group, $item] = $this->parseKey($key);
// Configuration items are actually keyed by "collection", which is simply a
// combination of each namespace and groups, which allows a unique way to
// identify the arrays of configuration items for the particular files.
$collection = $this->getCollection($group, $namespace);
$this->load($group, $namespace, $collection);
if (empty($item)) {
return Arr::get($this->items, $collection, $default);
}
return Arr::get($this->items[$collection], $item, $default);
}
/**
* Set a given configuration value.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function set($key, $value = null)
{
if (\is_array($key)) {
foreach ($key as $configKey => $configValue) {
$this->setSingleItem($configKey, $configValue);
}
} else {
$this->setSingleItem($key, $value);
}
}
/**
* Prepend a value onto an array configuration value.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function prepend($key, $value)
{
$config = $this->get($key);
$this->setSingleItem($key, \array_unshift($config, $value));
}
/**
* Push a value onto an array configuration value.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function push($key, $value)
{
$config = $this->get($key);
$this->setSingleItem($key, \array_push($config, $value));
}
/**
* Set a given collections of configuration value from cache.
*
* @return $this
*/
public function setFromCache(array $items)
{
$this->items = $items;
return $this;
}
/**
* Set a given configuration value.
*
* @param string $key
* @param mixed $value
* @param bool $load
*
* @return void
*/
protected function setSingleItem($key, $value = null, $load = true)
{
[$namespace, $group, $item] = $this->parseKey($key);
$collection = $this->getCollection($group, $namespace);
// We'll need to go ahead and lazy load each configuration groups even when
// we're just setting a configuration item so that the set item does not
// get overwritten if a different item in the group is requested later.
if ($load) {
$this->load($group, $namespace, $collection);
}
if (\is_null($item)) {
$this->items[$collection] = $value;
} else {
Arr::set($this->items[$collection], $item, $value);
}
}
/**
* Load the configuration group for the key.
*
* @param string $group
* @param string $namespace
* @param string $collection
*
* @return void
*/
protected function load($group, $namespace, $collection)
{
$env = $this->environment;
// If we've already loaded this collection, we will just bail out since we do
// not want to load it again. Once items are loaded a first time they will
// stay kept in memory within this class and not loaded from disk again.
if (isset($this->items[$collection])) {
return;
}
$items = $this->loader->load($env, $group, $namespace);
// If we've already loaded this collection, we will just bail out since we do
// not want to load it again. Once items are loaded a first time they will
// stay kept in memory within this class and not loaded from disk again.
if (isset($this->afterLoad[$namespace])) {
$items = $this->callAfterLoad($namespace, $group, $items);
}
$this->items[$collection] = $items;
}
/**
* Register a package for cascading configuration.
*/
public function package(string $package, string $hint, ?string $namespace = null): void
{
$namespace = $this->getPackageNamespace($package, $namespace);
$this->packages[] = $namespace;
// First we will simply register the namespace with the repository so that it
// can be loaded. Once we have done that we'll register an after namespace
// callback so that we can cascade an application package configuration.
$this->addNamespace($namespace, $hint);
$this->afterLoading($namespace, static function (Repository $me, $group, $items) use ($package) {
$env = $me->getEnvironment();
$loader = $me->getLoader();
return $loader->cascadePackage($env, $package, $group, $items);
});
}
/**
* Get the configuration namespace for a package.
*/
protected function getPackageNamespace(string $package, ?string $namespace): string
{
if (\is_null($namespace)) {
[, $namespace] = \explode('/', $package);
}
return $namespace;
}
/**
* Get the collection identifier.
*/
protected function getCollection(string $group, ?string $namespace = null): string
{
$namespace = $namespace ?: '*';
return $namespace.'::'.$group;
}
/**
* Get all of the configuration items.
*
* @return array
*/
public function all()
{
return $this->items;
}
/**
* Determine if the given configuration option exists.
*
* @param string $key
*
* @return bool
*/
public function offsetExists($key)
{
return $this->has($key);
}
/**
* Get a configuration option.
*
* @param string $key
*
* @return mixed
*/
public function offsetGet($key)
{
return $this->get($key);
}
/**
* Set a configuration option.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function offsetSet($key, $value)
{
$this->set($key, $value);
}
/**
* Unset a configuration option.
*
* @param string $key
*
* @return void
*/
public function offsetUnset($key)
{
$this->set($key, null);
}
}