1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System 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\Framework\Containers; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Spl\DataStructures\SplArrayObject; |
||
19 | use O2System\Spl\Traits\Collectors\FilePathCollectorTrait; |
||
20 | |||
21 | /** |
||
22 | * Class Config |
||
23 | * |
||
24 | * @package O2System\Framework\Containers |
||
25 | */ |
||
26 | class Config extends Environment |
||
27 | { |
||
28 | use FilePathCollectorTrait; |
||
29 | |||
30 | /** |
||
31 | * Config::$loaded |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $loaded = []; |
||
36 | |||
37 | // ------------------------------------------------------------------------ |
||
38 | |||
39 | /** |
||
40 | * Config::__construct |
||
41 | */ |
||
42 | public function __construct() |
||
43 | { |
||
44 | $this->setFileDirName('Config'); |
||
45 | $this->addFilePath(PATH_FRAMEWORK); |
||
46 | $this->addFilePath(PATH_APP); |
||
47 | } |
||
48 | |||
49 | // ------------------------------------------------------------------------ |
||
50 | |||
51 | /** |
||
52 | * Config::loadFile |
||
53 | * |
||
54 | * @param string $offset |
||
55 | * @param bool $return |
||
56 | * |
||
57 | * @return mixed |
||
58 | */ |
||
59 | public function loadFile($offset, $return = false) |
||
60 | { |
||
61 | $basename = pathinfo($offset, PATHINFO_BASENAME); |
||
62 | $filename = studlycase($basename); |
||
63 | |||
64 | $configFile = str_replace($basename, $filename, $offset); |
||
65 | $offset = camelcase($basename); |
||
66 | |||
67 | foreach ($this->filePaths as $configFilePath) { |
||
68 | if (is_file( |
||
69 | $filePath = $configFilePath . ucfirst( |
||
70 | strtolower(ENVIRONMENT) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
71 | ) . DIRECTORY_SEPARATOR . $configFile . '.php' |
||
72 | )) { |
||
73 | include($filePath); |
||
74 | } elseif (is_file($filePath = $configFilePath . DIRECTORY_SEPARATOR . $configFile . '.php')) { |
||
75 | include($filePath); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | if (isset($$offset)) { |
||
80 | if ( ! in_array($offset, $this->loaded)) { |
||
81 | array_push($this->loaded, $offset); |
||
82 | } |
||
83 | |||
84 | $this->addItem($offset, $$offset); |
||
85 | |||
86 | unset($$offset); |
||
87 | |||
88 | if ($return) { |
||
89 | return $this->getItem($offset); |
||
90 | } |
||
91 | |||
92 | return true; |
||
93 | } |
||
94 | |||
95 | return false; |
||
96 | } |
||
97 | |||
98 | // ------------------------------------------------------------------------ |
||
99 | |||
100 | /** |
||
101 | * Config::addItem |
||
102 | * |
||
103 | * Adds config item. |
||
104 | * |
||
105 | * @param string $offset |
||
106 | * @param mixed $value |
||
107 | */ |
||
108 | public function addItem($offset, $value) |
||
109 | { |
||
110 | $this->offsetSet($offset, $value); |
||
111 | } |
||
112 | |||
113 | // ------------------------------------------------------------------------ |
||
114 | |||
115 | /** |
||
116 | * Config::getItem |
||
117 | * |
||
118 | * Gets config item. |
||
119 | * |
||
120 | * @param string $offset |
||
121 | * |
||
122 | * @return mixed|\O2System\Spl\DataStructures\SplArrayObject |
||
123 | */ |
||
124 | public function &getItem($offset) |
||
125 | { |
||
126 | $item = parent::offsetGet($offset); |
||
127 | |||
128 | if (is_array($item)) { |
||
129 | if (is_string(key($item))) { |
||
130 | $item = new SplArrayObject($item); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | return $item; |
||
135 | } |
||
136 | |||
137 | // ------------------------------------------------------------------------ |
||
138 | |||
139 | /** |
||
140 | * Config::setItem |
||
141 | * |
||
142 | * Sets config item. |
||
143 | * |
||
144 | * @param string $offset |
||
145 | * @param mixed $value |
||
146 | */ |
||
147 | public function setItem($offset, $value) |
||
148 | { |
||
149 | $this->offsetSet($offset, $value); |
||
150 | } |
||
151 | |||
152 | // ------------------------------------------------------------------------ |
||
153 | |||
154 | /** |
||
155 | * Config::reload |
||
156 | */ |
||
157 | public function reload() |
||
158 | { |
||
159 | if(count($this->loaded)) { |
||
160 | foreach($this->loaded as $filename) { |
||
161 | $this->loadFile($filename); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | } |