1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Reactor\Services; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Kernel\Containers\Environment; |
19
|
|
|
use O2System\Spl\Datastructures\SplArrayObject; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Config |
23
|
|
|
* |
24
|
|
|
* @package O2System\Reactor\Services |
25
|
|
|
*/ |
26
|
|
|
class Config extends Environment |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Config::loadFile |
30
|
|
|
* |
31
|
|
|
* @param string $offset |
32
|
|
|
* @param bool $return |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function loadFile($offset, $return = false) |
37
|
|
|
{ |
38
|
|
|
$basename = pathinfo($offset, PATHINFO_BASENAME); |
39
|
|
|
$filename = studlycase($basename); |
40
|
|
|
|
41
|
|
|
$configFile = str_replace($basename, $filename, $offset); |
42
|
|
|
$offset = camelcase($basename); |
43
|
|
|
|
44
|
|
|
$configDirs = [ |
45
|
|
|
PATH_FRAMEWORK . 'Config' . DIRECTORY_SEPARATOR, |
46
|
|
|
PATH_APP . 'Config' . DIRECTORY_SEPARATOR, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
if (method_exists(modules(), 'getDirs')) { |
|
|
|
|
50
|
|
|
$configDirs = modules()->getDirs('Config', true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($configDirs as $configDir) { |
54
|
|
|
if (is_file( |
55
|
|
|
$filePath = $configDir . ucfirst( |
56
|
|
|
strtolower(ENVIRONMENT) |
|
|
|
|
57
|
|
|
) . DIRECTORY_SEPARATOR . $configFile . '.php' |
58
|
|
|
)) { |
59
|
|
|
include($filePath); |
60
|
|
|
} elseif (is_file($filePath = $configDir . DIRECTORY_SEPARATOR . $configFile . '.php')) { |
61
|
|
|
include($filePath); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($$offset)) { |
66
|
|
|
$this->addItem($offset, $$offset); |
67
|
|
|
|
68
|
|
|
unset($$offset); |
69
|
|
|
|
70
|
|
|
if ($return) { |
71
|
|
|
return $this->getItem($offset); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// ------------------------------------------------------------------------ |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Config::addItem |
84
|
|
|
* |
85
|
|
|
* Adds config item. |
86
|
|
|
* |
87
|
|
|
* @param string $offset |
88
|
|
|
* @param mixed $value |
89
|
|
|
*/ |
90
|
|
|
public function addItem($offset, $value) |
91
|
|
|
{ |
92
|
|
|
$this->offsetSet($offset, $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// ------------------------------------------------------------------------ |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Config::getItem |
99
|
|
|
* |
100
|
|
|
* Gets config item. |
101
|
|
|
* |
102
|
|
|
* @param string $offset |
103
|
|
|
* |
104
|
|
|
* @return mixed|\O2System\Spl\Datastructures\SplArrayObject |
105
|
|
|
*/ |
106
|
|
|
public function &getItem($offset) |
107
|
|
|
{ |
108
|
|
|
$item = parent::offsetGet($offset); |
109
|
|
|
|
110
|
|
|
if (is_array($item)) { |
111
|
|
|
if (is_string(key($item))) { |
112
|
|
|
$item = new SplArrayObject($item); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $item; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// ------------------------------------------------------------------------ |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Config::setItem |
123
|
|
|
* |
124
|
|
|
* Sets config item. |
125
|
|
|
* |
126
|
|
|
* @param string $offset |
127
|
|
|
* @param mixed $value |
128
|
|
|
*/ |
129
|
|
|
public function setItem($offset, $value) |
130
|
|
|
{ |
131
|
|
|
$this->offsetSet($offset, $value); |
132
|
|
|
} |
133
|
|
|
} |