1 | <?php |
||
10 | class Configuration |
||
11 | { |
||
12 | /** |
||
13 | * @var array $dirs where to look for configuration items. |
||
14 | */ |
||
15 | protected $dirs = []; |
||
16 | |||
17 | |||
18 | |||
19 | /** |
||
20 | * @var array $mapping mapping items to specific configuration file, mainly |
||
21 | * useful for testing various configuration files. |
||
22 | */ |
||
23 | protected $mapping = []; |
||
24 | |||
25 | |||
26 | |||
27 | /** |
||
28 | * Set a specific configuration file to load for a particluar item. |
||
29 | * |
||
30 | * @param string $item the item to map. |
||
31 | * @param string $file file to load configuration from. |
||
32 | * |
||
33 | * @return self to allow chaining. |
||
34 | */ |
||
35 | 1 | public function setMapping(string $item, string $file) : object |
|
40 | |||
41 | |||
42 | |||
43 | /** |
||
44 | * Set the directories where to look for configuration |
||
45 | * items (files, directories) to load. |
||
46 | * |
||
47 | * @throws Exception when the path to any of the directories are incorrect. |
||
48 | * |
||
49 | * @param array $dirs with the path to the config directories to search in. |
||
50 | * |
||
51 | * @return self to allow chaining. |
||
52 | */ |
||
53 | 8 | public function setBaseDirectories(array $dirs): object |
|
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * Read configuration from file or directory, if a file, look though all |
||
73 | * base dirs and use the first configuration that is found. A configuration |
||
74 | * item can be combined from a file and a directory, when available in the |
||
75 | * same base directory. |
||
76 | * |
||
77 | * The resulting configuration is always an array, its structure contains |
||
78 | * values from each individual configuration file, like this. |
||
79 | * |
||
80 | * $config = [ |
||
81 | * "file" => filename for file.php, |
||
82 | * "config" => result returned from file.php, |
||
83 | * "items" => [ |
||
84 | * [ |
||
85 | * "file" => filename for dir/file1.php, |
||
86 | * "config" => result returned from dir/file1.php, |
||
87 | * ], |
||
88 | * [ |
||
89 | * "file" => filename for dir/file2.php, |
||
90 | * "config" => result returned from dir/file2.php, |
||
91 | * ], |
||
92 | * ]. |
||
93 | * ] |
||
94 | * |
||
95 | * The configuration files in the directory are loaded per alphabetical |
||
96 | * order. |
||
97 | * |
||
98 | * @param string $item is a name representing the module and is used to |
||
99 | * combine the path to search for. |
||
100 | * |
||
101 | * @throws Exception when configuration item can not be found. |
||
102 | * @throws Exception when $dirs are empty. |
||
103 | * |
||
104 | * @return array with returned value from the loaded configuration. |
||
105 | */ |
||
106 | 6 | public function load(string $item) : array |
|
149 | |||
150 | |||
151 | |||
152 | /** |
||
153 | * Read configuration a directory, loop through all files and add |
||
154 | * them into the $config array as: |
||
155 | * [ |
||
156 | * [ |
||
157 | * "file" => filename for dir/file1.php, |
||
158 | * "config" => result returned from dir/file1.php, |
||
159 | * ], |
||
160 | * [ |
||
161 | * "file" => filename for dir/file2.php, |
||
162 | * "config" => result returned from dir/file2.php, |
||
163 | * ], |
||
164 | * ]. |
||
165 | * |
||
166 | * @param string $path is the path to the directory containing config files. |
||
167 | * |
||
168 | * @return array with configuration for each file. |
||
169 | */ |
||
170 | 2 | public function loadFromDir(string $path): array |
|
182 | } |
||
183 |