1 | <?php |
||
10 | class Config |
||
11 | { |
||
12 | /** |
||
13 | * @const ERR_JSON_PARSE Exception code if the parse of a json file fail. |
||
14 | */ |
||
15 | const ERR_JSON_PARSE = 1302001; |
||
16 | |||
17 | /** |
||
18 | * @const ERR_GETVALUE_FILE_NOT_INDICATED Exception code if the file to use |
||
19 | * is not indicated into the method getValue(). |
||
20 | * (only if there are many config files) |
||
21 | */ |
||
22 | const ERR_GETVALUE_FILE_NOT_INDICATED = 1302002; |
||
23 | |||
24 | /** |
||
25 | * @const ERR_GETVALUE_FILE_NOT_FOUND Exception code if the file to use is |
||
26 | * not found. |
||
27 | */ |
||
28 | const ERR_FILE_NOT_FOUND = 1302003; |
||
29 | |||
30 | /** |
||
31 | * @const ERR_GETVALUE_KEY_NOT_FOUND Exception code if the asked config key |
||
32 | * not exist. |
||
33 | */ |
||
34 | const ERR_KEY_NOT_FOUND = 1303004; |
||
35 | |||
36 | /** |
||
37 | * @const ERR_VALUE_FORMAT Exception code if the value for a filename is |
||
38 | * not an array or an object. |
||
39 | */ |
||
40 | const ERR_VALUE_FORMAT = 1303005; |
||
41 | |||
42 | /** |
||
43 | * @var string $configDirName Directory's name in config dir |
||
44 | */ |
||
45 | protected $configDirName = ''; |
||
46 | |||
47 | /** |
||
48 | * @var string $configDir Complete path of the readed directory |
||
49 | */ |
||
50 | protected $configDir = ''; |
||
51 | |||
52 | /** |
||
53 | * @var string[] $configFiles List of files to read |
||
54 | */ |
||
55 | protected $configFiles = []; |
||
56 | |||
57 | /** |
||
58 | * @var array $config List of config value found |
||
59 | */ |
||
60 | protected $config = []; |
||
61 | |||
62 | /** |
||
63 | * Constructor |
||
64 | * Define properties configDirName and configDir |
||
65 | * |
||
66 | * @param string $configDirName Directory's name in config dir |
||
67 | */ |
||
68 | public function __construct($configDirName) |
||
73 | |||
74 | /** |
||
75 | * Getter accessor to $config property |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | public function getConfig() |
||
83 | |||
84 | /** |
||
85 | * Getter accessor to a value into the $config property |
||
86 | * |
||
87 | * @param string $file |
||
88 | * |
||
89 | * @return mixed |
||
90 | */ |
||
91 | public function getConfigForFile($file) |
||
95 | |||
96 | /** |
||
97 | * Search and load all config files which has been found |
||
98 | * |
||
99 | * @return void |
||
100 | */ |
||
101 | public function loadFiles() |
||
109 | |||
110 | /** |
||
111 | * Search all config files in a directory |
||
112 | * Search also in sub-directory (2nd parameter) |
||
113 | * |
||
114 | * @param string $dirPath The directory path where is run the search |
||
115 | * @param string $pathIntoFirstDir (default '') Used when this method |
||
116 | * reads a subdirectory. It's the path of the readed directory during |
||
117 | * the first call to this method. |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | protected function searchAllConfigFiles($dirPath, $pathIntoFirstDir = '') |
||
146 | |||
147 | /** |
||
148 | * Load a config file. |
||
149 | * Find the file's extension and call the method to parse the file |
||
150 | * |
||
151 | * @param string $fileKey The file's key. Most of the time, the path to |
||
152 | * the file from the $this->configDir value |
||
153 | * @param string $filePath The path to the file |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | protected function loadConfigFile($fileKey, $filePath) |
||
173 | |||
174 | /** |
||
175 | * Load a json config file |
||
176 | * |
||
177 | * @param string $fileKey The file's key. Most of the time, the path to |
||
178 | * the file from the $this->configDir value |
||
179 | * @param string $filePath The path to the file |
||
180 | * |
||
181 | * @return void |
||
182 | * |
||
183 | * @throws Exception If there is an error from the json parser |
||
184 | */ |
||
185 | protected function loadJsonConfigFile($fileKey, $filePath) |
||
199 | |||
200 | /** |
||
201 | * Load a php config file |
||
202 | * |
||
203 | * @param string $fileKey The file's key. Most of the time, the path to |
||
204 | * the file from the $this->configDir value |
||
205 | * @param string $filePath The path to the file |
||
206 | * |
||
207 | * @return void |
||
208 | */ |
||
209 | protected function loadPhpConfigFile($fileKey, $filePath) |
||
213 | |||
214 | /** |
||
215 | * Return a config value for a key |
||
216 | * |
||
217 | * @param string $key The asked key for the value |
||
218 | * @param string $file (default null) If many file is loaded, the file name |
||
219 | * where is the key. Is the file is into a sub-directory, the |
||
220 | * sub-directory should be present. |
||
221 | * |
||
222 | * @return mixed |
||
223 | * |
||
224 | * @throws Exception If file parameter is null and there are many files. Or |
||
225 | * if the file not exist. Or if the key not exist. |
||
226 | */ |
||
227 | public function getValue($key, $file = null) |
||
260 | |||
261 | /** |
||
262 | * Setter to modify the all config value for a specific filename. |
||
263 | * |
||
264 | * @param string $filename The filename config to modify |
||
265 | * @param array|\stdClass $config The new config value |
||
266 | * |
||
267 | * @return $this |
||
268 | * |
||
269 | * @throws \Exception If the new value if not an array or an object. |
||
270 | */ |
||
271 | public function setConfigForFile($filename, $config) |
||
284 | |||
285 | /** |
||
286 | * Setter to modify a config key into the config of a filename |
||
287 | * |
||
288 | * @param string $filename The filename config to modify |
||
289 | * @param string $configKey The name of the key to modify |
||
290 | * @param mixed $configValue The new value for the config key |
||
291 | * |
||
292 | * @return $this |
||
293 | * |
||
294 | * @throws \Exception If the key has not been found |
||
295 | */ |
||
296 | public function setConfigKeyForFile($filename, $configKey, $configValue) |
||
315 | } |
||
316 |