| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Anax\Configure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Trait implementing reading from config file and config directory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * and storing options in $this->config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | trait Configure2Trait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var [] $config store the configuration in this array. | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $config = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Read configuration from file or array, if a file, first check in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * ANAX_APP_PATH/config and then in ANAX_INSTALL_PATH/config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param array|string $what is an array with key/value config options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      *                           or a file to be included which returns such | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      *                           an array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @throws Exception when argument if not a file nor an array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return self for chaining. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     public function configure($what) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         if (is_array($what)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             $this->config = $what; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $paths = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         if (defined("ANAX_APP_PATH")) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             $paths[] = ANAX_APP_PATH . "/config/$what"; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         if (defined("ANAX_INSTALL_PATH")) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             $paths[] = ANAX_INSTALL_PATH . "/config/$what"; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $paths[] = $what; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         $found = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         foreach ($paths as $path) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             if (is_readable($path)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 $found = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 $this->config = require $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             $parts = pathinfo($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $dir = $parts["dirname"] . "/" . $parts["filename"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             if (is_dir($dir)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 foreach (glob("$dir/*.php") as $file) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                     $found = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                     $config = require $file; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                     if (!is_array($config)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                         $config = [$config]; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                     $this->config["items"][basename($file)] = $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             if ($found) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         throw new Exception("Configure item '$what' is not an array nor a readable file."); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Helper function for reading values from the configuration and appy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * default values where configuration item is missing. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param string $key     matching a key in the config array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $default value returned when config item is not found. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return mixed or null if key does not exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function getConfig($key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return isset($this->config[$key]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ? $this->config[$key] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             : $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.