1 | <?php |
||
10 | class Configuration |
||
11 | { |
||
12 | /** |
||
13 | * @var array $config to save the latest loaded config to ease |
||
14 | * access. |
||
15 | */ |
||
16 | protected $config = []; |
||
17 | |||
18 | |||
19 | |||
20 | /** |
||
21 | * @var array $dirs where to look for configuration items. |
||
22 | */ |
||
23 | protected $dirs = []; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * @var array $mapping mapping items to specific configuration file, mainly |
||
29 | * useful for testing various configuration files. |
||
30 | */ |
||
31 | protected $mapping = []; |
||
32 | |||
33 | |||
34 | |||
35 | /** |
||
36 | * Set a specific configuration file to load for a particluar item. |
||
37 | * |
||
38 | * @param string $item the item to map. |
||
39 | * @param string $file file to load configuration from. |
||
40 | * |
||
41 | * @return self to allow chaining. |
||
42 | */ |
||
43 | public function setMapping(string $item, string $file) : object |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * Set the directories where to look for configuration |
||
53 | * items (files, directories) to load. |
||
54 | * |
||
55 | * @throws Exception when the path to any of the directories are incorrect. |
||
56 | * |
||
57 | * @param array $dirs with the path to the config directories to search in. |
||
58 | * |
||
59 | * @return self to allow chaining. |
||
60 | */ |
||
61 | public function setBaseDirectories(array $dirs): object |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * Read configuration from file or directory, if a file, look though all |
||
77 | * base dirs and use the first configuration that is found. A configuration |
||
78 | * item can be combined from a file and a directory, when available in the |
||
79 | * same base directory. |
||
80 | * |
||
81 | * The resulting configuration is always an array, its structure contains |
||
82 | * values from each individual configuration file, like this. |
||
83 | * |
||
84 | * $config = [ |
||
85 | * "file" => filename for file.php, |
||
86 | * "config" => result returned from file.php, |
||
87 | * "items" => [ |
||
88 | * [ |
||
89 | * "file" => filename for dir/file1.php, |
||
90 | * "config" => result returned from dir/file1.php, |
||
91 | * ], |
||
92 | * [ |
||
93 | * "file" => filename for dir/file2.php, |
||
94 | * "config" => result returned from dir/file2.php, |
||
95 | * ], |
||
96 | * ]. |
||
97 | * ] |
||
98 | * |
||
99 | * The configuration files in the directory are loaded per alphabetical |
||
100 | * order. |
||
101 | * |
||
102 | * @param string $item is a name representing the module and is used to |
||
103 | * combine the path to search for. |
||
104 | * |
||
105 | * @throws Exception when configuration item can not be found. |
||
106 | * @throws Exception when $dirs are empty. |
||
107 | * |
||
108 | * @return array with returned value from the loaded configuration. |
||
109 | */ |
||
110 | public function load(string $item) : array |
||
166 | |||
167 | |||
168 | |||
169 | /** |
||
170 | * Read configuration a directory, loop through all files and add |
||
171 | * them into the $config array as: |
||
172 | * [ |
||
173 | * [ |
||
174 | * "file" => filename for dir/file1.php, |
||
175 | * "config" => result returned from dir/file1.php, |
||
176 | * ], |
||
177 | * [ |
||
178 | * "file" => filename for dir/file2.php, |
||
179 | * "config" => result returned from dir/file2.php, |
||
180 | * ], |
||
181 | * ]. |
||
182 | * |
||
183 | * @param string $path is the path to the directory containing config files. |
||
184 | * |
||
185 | * @return array with configuration for each file. |
||
186 | */ |
||
187 | public function loadFromDir(string $path): array |
||
199 | |||
200 | |||
201 | |||
202 | /** |
||
203 | * Helper function for reading values from the configuration and apply |
||
204 | * default values where configuration item is missing. This |
||
205 | * helper only works when there are single configuration files, |
||
206 | * it does not work when multiple configuration files are loaded |
||
207 | * from a directory. |
||
208 | * |
||
209 | * @param string $key matching a key in the config array. |
||
210 | * @param string $default value returned when config item is not found. |
||
211 | * |
||
212 | * @return mixed or null if key does not exists. |
||
213 | */ |
||
214 | public function getConfig($key, $default = null) |
||
218 | } |
||
219 |