| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace FaizShukri\Quran\Supports; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 5 |  |  | class Config | 
            
                                                                        
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |     private $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     public $configFile; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     public $dataDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 | 74 |  |     public function __construct(array $config = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 74 |  |         $this->config = $this->buildConfig($config); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 | 37 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * Build a config array. Merge user defined config with our default config. | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @param array $config User defined config | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @return array New configuration array | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 74 |  |     private function buildConfig(array $config = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 74 |  |         if (function_exists('config_path') && file_exists(config_path('quran.php'))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             $this->configFile = config_path('quran.php'); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 74 |  |             $this->configFile = realpath(__DIR__ . '/../../config/quran.php'); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         // Merge our config with user config | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 74 |  |         $result = array_replace_recursive((include $this->configFile), $config); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         // If function storage_path is exist (laravel), we update the path to laravel's storage path | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 74 |  |         if (function_exists('storage_path')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->dataDir =  storage_path('app/' . $result['storage_path']); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 74 |  |             $this->dataDir =  realpath(__DIR__ . '/../..') . '/' . $result['storage_path']; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 74 |  |         $result['storage_path'] = $this->dataDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 74 |  |         return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * Get the config variable. | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param string $val Variable name | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @return array|string Variable value | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 92 |  |     public function get($val, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 92 |  |         $configs = explode('.', $val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 92 |  |         $key = array_shift($configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 92 |  |         $first = isset($this->config[$key]) ? $this->config[$key] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 92 |  |         foreach ($configs as $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 68 |  |             $first = isset($first[$config]) ? $first[$config] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 | 92 |  |         return isset($first) ? $first : $default; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * Return all configurations. | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 2 |  |     public function all() | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 2 |  |         return $this->config; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |     public function addTranslation($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |         if (in_array($id, $this->config['translations'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |         $configString = file_get_contents($this->configFile, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         $result = preg_replace('/([\'\"]translations[\'\"]\s+\=\>\s+\[[a-z\"\'\.\,\s]+)/i', "\\1, '$id'", $configString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 2 |  |         file_put_contents($this->configFile, $result); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 | 2 |  |         array_push($this->config['translations'], $id); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 85 | 1 |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 87 |  |  |  |