1 | <?php |
||
10 | class Config |
||
11 | { |
||
12 | /** |
||
13 | * @var string $configDirName Directory's name in config dir |
||
14 | */ |
||
15 | protected $configDirName = ''; |
||
16 | |||
17 | /** |
||
18 | * @var string $configDir Complete path of the readed directory |
||
19 | */ |
||
20 | protected $configDir = ''; |
||
21 | |||
22 | /** |
||
23 | * @var string[] $configFiles List of files to read |
||
24 | */ |
||
25 | protected $configFiles = []; |
||
26 | |||
27 | /** |
||
28 | * @var array $config List of config value found |
||
29 | */ |
||
30 | protected $config = []; |
||
31 | |||
32 | /** |
||
33 | * Constructor |
||
34 | * Define properties configDirName and configDir |
||
35 | * |
||
36 | * @param string $configDirName Directory's name in config dir |
||
37 | */ |
||
38 | public function __construct($configDirName) |
||
39 | { |
||
40 | $this->configDirName = $configDirName; |
||
41 | $this->configDir = CONFIG_DIR.$this->configDirName; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Search and load all config files which has been found |
||
46 | * |
||
47 | * @return void |
||
48 | */ |
||
49 | public function loadFiles() |
||
57 | |||
58 | /** |
||
59 | * Search all config files in a directory |
||
60 | * Search also in sub-directory (2nd parameter) |
||
61 | * |
||
62 | * @param string $dirPath The directory path where is run the search |
||
63 | * @param string $pathIntoFirstDir (default '') Used when this method |
||
64 | * reads a subdirectory. It's the path from the directory read during |
||
65 | * the first call to this method. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | protected function searchAllConfigFiles($dirPath, $pathIntoFirstDir = '') |
||
70 | { |
||
71 | if (!file_exists($dirPath)) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | //Remove some value in list of file |
||
76 | $listFiles = array_diff(scandir($dirPath), ['.', '..']); |
||
77 | |||
78 | foreach ($listFiles as $file) { |
||
79 | $fileKey = $pathIntoFirstDir.$file; |
||
80 | $readPath = $dirPath.'/'.$file; |
||
81 | |||
82 | if (is_file($readPath)) { |
||
83 | $this->configFiles[$fileKey] = $readPath; |
||
84 | } elseif (is_link($readPath)) { |
||
85 | $this->configFiles[$fileKey] = realpath($readPath); |
||
86 | } elseif (is_dir($readPath)) { |
||
87 | $this->searchAllConfigFiles( |
||
88 | $readPath, |
||
89 | $pathIntoFirstDir.$file.'/' |
||
90 | ); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Load a config file. |
||
97 | * Find the file's extension and call the method to parse the file |
||
98 | * |
||
99 | * @param string $fileKey The file's key. Most of the time, the path to |
||
100 | * the file from the $this->configDir value |
||
101 | * @param string $filePath The path to the file |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | protected function loadConfigFile($fileKey, $filePath) |
||
106 | { |
||
107 | $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION); |
||
108 | |||
109 | if ($fileExtension === 'json') { |
||
110 | $this->loadJsonConfigFile($fileKey, $filePath); |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | if ($fileExtension === 'php') { |
||
115 | $this->loadPhpConfigFile($fileKey, $filePath); |
||
116 | return; |
||
117 | } |
||
118 | |||
119 | //@TODO : YAML |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Load a json config file |
||
124 | * |
||
125 | * @param string $fileKey The file's key. Most of the time, the path to |
||
126 | * the file from the $this->configDir value |
||
127 | * @param string $filePath The path to the file |
||
128 | * |
||
129 | * @return void |
||
130 | * |
||
131 | * @throws Exception If there is an error from the json parser |
||
132 | */ |
||
133 | protected function loadJsonConfigFile($fileKey, $filePath) |
||
134 | { |
||
135 | $json = file_get_contents($filePath); |
||
136 | $config = json_decode($json); |
||
137 | |||
138 | if ($config === null) { |
||
139 | throw new Exception(json_last_error_msg()); |
||
140 | } |
||
141 | |||
142 | $this->config[$fileKey] = $config; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Load a php config file |
||
147 | * |
||
148 | * @param string $fileKey The file's key. Most of the time, the path to |
||
149 | * the file from the $this->configDir value |
||
150 | * @param string $filePath The path to the file |
||
151 | * |
||
152 | * @return void |
||
153 | */ |
||
154 | protected function loadPhpConfigFile($fileKey, $filePath) |
||
158 | |||
159 | /** |
||
160 | * Return a config value for a key |
||
161 | * |
||
162 | * @param string $key The asked key for the value |
||
163 | * @param string $file (default null) If many file is loaded, the file name |
||
164 | * where is the key. Is the file is into a sub-directory, the |
||
165 | * sub-directory should be present. |
||
166 | * |
||
167 | * @return mixed |
||
168 | * |
||
169 | * @throws Exception If file parameter is null and there are many files. Or |
||
170 | * if the file not exist. Or if the key not exist. |
||
171 | */ |
||
172 | public function getConfig($key, $file = null) |
||
200 | } |
||
201 |