| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * ConfigReader.php - Jaxon config reader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package jaxon-core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Thierry Feuzeu <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright 2022 Thierry Feuzeu <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @link https://github.com/jaxon-php/jaxon-core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Jaxon\Utils\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function trim; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function pathinfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | class ConfigReader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Read options from a config file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $sConfigFile The full path to the config file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @throws Exception\FileAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @throws Exception\FileExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @throws Exception\FileContent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @throws Exception\YamlExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function read(string $sConfigFile): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         if(!($sConfigFile = trim($sConfigFile))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $sExt = pathinfo($sConfigFile, PATHINFO_EXTENSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         switch($sExt) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         case 'php': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $aConfigOptions = File\Php::read($sConfigFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         case 'yaml': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         case 'yml': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $aConfigOptions = File\Yaml::read($sConfigFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         case 'json': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $aConfigOptions = File\Json::read($sConfigFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             throw new Exception\FileExtension($sConfigFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $aConfigOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Read options from a config file and setup the library | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param Config $xConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param string $sConfigFile The full path to the config file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $sConfigSection The section of the config file to be loaded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws Exception\DataDepth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws Exception\FileAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @throws Exception\FileExtension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @throws Exception\FileContent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws Exception\YamlExtension | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     public function load(Config $xConfig, string $sConfigFile, string $sConfigSection = '') | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         // Read the options and save in the config. | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         $xConfig->setOptions($this->read($sConfigFile), $sConfigSection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |