1 | <?php |
||
8 | class Config implements ArrayAccess |
||
9 | { |
||
10 | private $converter; |
||
11 | |||
12 | 7 | function __construct(Filesystem $fs, Converter $converter) |
|
|
|||
13 | { |
||
14 | 7 | $this->converter = $converter; |
|
15 | |||
16 | 7 | if($fs->exists('.env')) { |
|
17 | $dotenv = new Dotenv($fs->path()); |
||
18 | $dotenv->load(); |
||
19 | } |
||
20 | |||
21 | 7 | foreach ($fs->listFiles('resources/config') as $file) { |
|
22 | 7 | $values = $this->converter->toObject(include $fs->getPath("resources/config/$file")); |
|
23 | 7 | $current = $this; |
|
24 | 7 | $name = substr($file, 0, -4); |
|
25 | 7 | if (stristr($name, '/')) { |
|
26 | $namespace = explode('/', $name); |
||
27 | $name = array_pop($namespace); |
||
28 | foreach ($namespace as $key) { |
||
29 | $current->$key = (object) []; |
||
30 | $current = $current->$key; |
||
31 | } |
||
32 | } |
||
33 | 7 | $current->$name = $values; |
|
34 | 7 | } |
|
35 | 7 | } |
|
36 | |||
37 | function offsetExists($offset) |
||
41 | |||
42 | 7 | function offsetGet($offset) |
|
51 | |||
52 | 1 | function offsetSet($offset, $value) |
|
53 | { |
||
54 | 1 | $path = explode('.', $offset); |
|
55 | 1 | $key = array_pop($path); |
|
56 | 1 | $parent = $this->getNode(implode('.', $path), true); |
|
57 | 1 | if (is_array($value) || is_object($value)) { |
|
58 | $value = $this->converter->toObject($value); |
||
59 | } |
||
60 | |||
61 | 1 | return $parent->$key = $value; |
|
62 | } |
||
63 | |||
64 | function offsetUnset($offset) |
||
71 | |||
72 | function shouldHave($offset) |
||
78 | |||
79 | 7 | private function getNode($offset, $createIfNone = false) |
|
80 | { |
||
100 | } |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.