1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Basis;
|
4
|
|
|
|
5
|
|
|
use ArrayAccess;
|
6
|
|
|
use Dotenv\Dotenv;
|
7
|
|
|
|
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
|
|
|
}
|
35
|
7 |
|
}
|
36
|
|
|
|
37
|
|
|
function offsetExists($offset)
|
|
|
|
|
38
|
|
|
{
|
39
|
|
|
return $this->getNode($offset) != null;
|
40
|
|
|
}
|
41
|
|
|
|
42
|
7 |
|
function offsetGet($offset)
|
|
|
|
|
43
|
|
|
{
|
44
|
7 |
|
$value = $this->getNode($offset);
|
45
|
7 |
|
if(!is_object($value)) {
|
46
|
7 |
|
return $value;
|
47
|
|
|
}
|
48
|
|
|
|
49
|
1 |
|
return $this->converter->toArray($value);
|
50
|
|
|
}
|
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)
|
|
|
|
|
65
|
|
|
{
|
66
|
|
|
$path = explode('.', $offset);
|
67
|
|
|
$key = array_pop($path);
|
68
|
|
|
$parent = $this->getNode(implode('.', $path));
|
69
|
|
|
unset($parent->$key);
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
function shouldHave($offset)
|
|
|
|
|
73
|
|
|
{
|
74
|
|
|
if(!isset($this[$offset])) {
|
75
|
|
|
throw new Exception("No offset $offset");
|
76
|
|
|
}
|
77
|
|
|
}
|
78
|
|
|
|
79
|
7 |
|
private function getNode($offset, $createIfNone = false)
|
80
|
|
|
{
|
81
|
7 |
|
if (!$offset) {
|
82
|
|
|
return $this;
|
83
|
|
|
}
|
84
|
7 |
|
$current = $this;
|
85
|
7 |
|
foreach (explode('.', $offset) as $key) {
|
86
|
7 |
|
if (is_object($current)) {
|
87
|
7 |
|
if (!property_exists($current, $key)) {
|
88
|
1 |
|
if ($createIfNone) {
|
89
|
|
|
$current->$key = (object) [];
|
90
|
|
|
} else {
|
91
|
1 |
|
return;
|
92
|
|
|
}
|
93
|
|
|
}
|
94
|
7 |
|
$current = $current->$key;
|
95
|
|
|
}
|
96
|
|
|
}
|
97
|
|
|
|
98
|
7 |
|
return $current;
|
99
|
|
|
}
|
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.