| Total Complexity | 82 |
| Total Lines | 323 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ConfigManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConfigManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class ConfigManager |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Chemin complet du fichier de configuration |
||
| 14 | */ |
||
| 15 | private $targetFile; |
||
| 16 | private $Options = []; |
||
| 17 | /** |
||
| 18 | * tableau multidimensionnel contenant les donnée de configuration, initialement |
||
| 19 | * chargées depuis le fichier |
||
| 20 | */ |
||
| 21 | private $Config = []; |
||
| 22 | /** |
||
| 23 | * processeurs de fichier pris en charge par l'application |
||
| 24 | */ |
||
| 25 | private $availableDrivers = array('JSON', 'PHP','INI','YML'); |
||
| 26 | |||
| 27 | private $defaultSection = 'runtime'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param $filename |
||
| 31 | * @param $location |
||
| 32 | * @throws \Exception |
||
| 33 | */ |
||
| 34 | public function __construct( $filename, $location=null ) |
||
| 35 | { |
||
| 36 | $numargs = func_num_args(); |
||
| 37 | try |
||
| 38 | { |
||
| 39 | if($numargs > 2) |
||
| 40 | throw new Exception('SETUP ERROR: configuration manager can accept only up to 2 parameters,'.$numargs.' given!'); |
||
| 41 | $this->configureOptions($filename,$location); |
||
| 42 | $this->parseConfiguration($this->Options); |
||
| 43 | return $this; |
||
| 44 | } |
||
| 45 | catch(Exception $a) |
||
| 46 | { |
||
| 47 | trigger_error($a->getMessage(),E_USER_ERROR); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | /** |
||
| 51 | * @param $section |
||
| 52 | * @param $item |
||
| 53 | * @return array|bool|mixed |
||
| 54 | */ |
||
| 55 | public function get($section=null, $item=null) |
||
| 56 | { |
||
| 57 | if($item) $item = trim(strtolower($item)); |
||
| 58 | if($section) $section = trim(strtolower($section)); |
||
| 59 | if(!count($this->Config)) return false; |
||
| 60 | if(!$section or !strlen($section)) return $this->Config; |
||
| 61 | if($section AND $item) |
||
| 62 | { |
||
| 63 | if(!isset($this->Config[$section])) |
||
| 64 | { |
||
| 65 | $key = $item; |
||
| 66 | $item = $section; |
||
| 67 | $section = $this->defaultSection; |
||
| 68 | if(!isset($this->Config[$section][$item][$key])) |
||
| 69 | return false; |
||
| 70 | return $this->Config[$section][$item][$key]; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | elseif(!$item or !strlen($item)) |
||
| 74 | { |
||
| 75 | $item = $section; |
||
| 76 | if(isset($this->Config[$item])) return $this->Config[$item]; |
||
| 77 | $section = $this->defaultSection; |
||
| 78 | } |
||
| 79 | if(!isset($this->Config[$section][$item])) return false; |
||
| 80 | return $this->Config[$section][$item]; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param $section |
||
| 85 | * @param $item |
||
| 86 | * @param $value |
||
| 87 | * @return bool |
||
| 88 | */ |
||
| 89 | public function set($section,$item=null,$value=null) |
||
| 90 | { |
||
| 91 | ob_start(); |
||
| 92 | $numarg = func_num_args(); |
||
| 93 | $arguments=func_get_args(); |
||
| 94 | switch($numarg) |
||
| 95 | { |
||
| 96 | case 1: |
||
| 97 | if(!is_array($arguments[0])) return false; |
||
| 98 | $item=array_change_key_case($arguments[0], CASE_LOWER); $section=null; $value=null; |
||
| 99 | break; |
||
| 100 | case 2: |
||
| 101 | if(is_array($arguments[0])) return false; |
||
| 102 | $_arg = strtolower(trim($arguments[0])); |
||
| 103 | if(is_array($arguments[1])){ $section=$_arg; $item =array_change_key_case($arguments[1], CASE_LOWER);$value=null;} |
||
| 104 | else {$item = $_arg;$value=$arguments[1];$section=null;} |
||
| 105 | break; |
||
| 106 | default: |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | $section = $section? trim(strtolower($section)) : $this->defaultSection; |
||
| 110 | if(!is_array($item)) |
||
| 111 | { |
||
| 112 | if(!$value) return false; |
||
| 113 | $item=trim(strtolower($item)); |
||
| 114 | if(!isset($this->Config[$section][$item]) or !is_array($this->Config[$section][$item])): |
||
| 115 | $this->Config[$section][$item]=$value; |
||
| 116 | else: |
||
| 117 | if(!is_array($value)) $value = array($value); |
||
| 118 | $this->Config[$section][$item] = array_merge($this->Config[$section][$item],$value); |
||
| 119 | endif; |
||
| 120 | } |
||
| 121 | else |
||
| 122 | { |
||
| 123 | if($value) return false; |
||
| 124 | $item = array_change_key_case($item, CASE_LOWER); |
||
| 125 | $sectionsize = count($this->Config[$section]); |
||
| 126 | $itemsize = count($item); |
||
| 127 | if($sectionsize) |
||
| 128 | { |
||
| 129 | if($itemsize=='1') |
||
| 130 | { |
||
| 131 | if(isset($this->Config[$section][key($item)])) |
||
| 132 | $this->Config[$section][key($item)] = array_merge($this->Config[$section][key($item)],$item[key($item)]); |
||
| 133 | else if(!is_numeric(key($item))) $this->Config[$section][key($item)]=$item[key($item)]; |
||
| 134 | } |
||
| 135 | else $this->Config[$section] = array_merge($this->Config[$section],$item); |
||
| 136 | } |
||
| 137 | else $this->Config[$section] = $item; |
||
| 138 | } |
||
| 139 | $re = $this->Save(); |
||
| 140 | ob_end_clean(); |
||
| 141 | return $re; |
||
| 142 | } |
||
| 143 | /** |
||
| 144 | * @param $section |
||
| 145 | * @param $item |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | public function del($section, $item=null) |
||
| 149 | { |
||
| 150 | $section = trim(strtolower($section)); |
||
| 151 | if($item and strlen($item)) |
||
| 152 | { |
||
| 153 | $item = trim(strtolower($item)); |
||
| 154 | if(!isset($this->Config[$section])) |
||
| 155 | { |
||
| 156 | $key = $item; |
||
| 157 | $item = $section; |
||
| 158 | $section = $this->defaultSection; |
||
| 159 | if(isset($this->Config[$section][$item][$key])) |
||
| 160 | { |
||
| 161 | $itemSize=count($this->Config[$section][$item]); |
||
| 162 | if($itemSize>1) unset($this->Config[$section][$item][$key]); |
||
| 163 | else unset($this->Config[$section]); |
||
| 164 | } |
||
| 165 | } |
||
| 166 | else |
||
| 167 | { |
||
| 168 | $sectionSize=count($this->Config[$section]); |
||
| 169 | if(isset($this->Config[$section][$item])) |
||
| 170 | { |
||
| 171 | if($sectionSize>1) unset($this->Config[$section][$item]); |
||
| 172 | else unset($this->Config[$section]); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | } |
||
| 176 | else |
||
| 177 | { |
||
| 178 | $item = $section; |
||
| 179 | if(!isset($this->Config[$item])) |
||
| 180 | { |
||
| 181 | $section = $this->defaultSection; |
||
| 182 | $defaultSectionSize = count($this->Config[$section]); |
||
| 183 | if(isset($this->Config[$section][$item])) |
||
| 184 | { |
||
| 185 | if($defaultSectionSize>1) unset($this->Config[$section][$item]); |
||
| 186 | else unset($this->Config[$section]); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | else unset($this->Config[$item]); |
||
| 190 | } |
||
| 191 | return $this->Save(); |
||
| 192 | } |
||
| 193 | /** |
||
| 194 | * @param $file |
||
| 195 | * @param $location |
||
| 196 | * @return array|bool |
||
| 197 | */ |
||
| 198 | private function configureOptions($file,$location=null){ |
||
| 199 | if(!is_string($file) or ($location and !is_string($location))) |
||
| 200 | throw new Exception('SETUP ERROR: configuration manager can accept string only parameters'); |
||
| 201 | $default=[ |
||
| 202 | 'driver' => 'PHP', |
||
| 203 | 'filename' => null, |
||
| 204 | 'directory' => __DIR__, |
||
| 205 | ]; |
||
| 206 | $Options = []; |
||
| 207 | if($location) |
||
| 208 | $Options['directory']=rtrim($this->normalize($location),DIRECTORY_SEPARATOR); |
||
| 209 | else{ |
||
| 210 | if(basename($file)!==$file) |
||
| 211 | $Options['directory']= rtrim($this->normalize(pathinfo($file,PATHINFO_DIRNAME)),DIRECTORY_SEPARATOR); |
||
| 212 | } |
||
| 213 | $Options['filename'] = basename($file); |
||
| 214 | if(strpos($Options['filename'],'.')!==false) |
||
| 215 | $Options['driver'] = strtoupper(pathinfo($Options['filename'], PATHINFO_EXTENSION)); |
||
| 216 | else |
||
| 217 | $Options['filename']= $Options['filename'].'.'.strtolower($default['driver']); |
||
| 218 | if(!in_array($Options['driver'],$this->availableDrivers)) |
||
| 219 | throw new Exception('ERROR: driver "'.$Options['driver'].'" not supported'); |
||
| 220 | $this->Options = array_merge($default,$Options); |
||
| 221 | return $this->Options; |
||
| 222 | } |
||
| 223 | /** |
||
| 224 | * @param $path |
||
| 225 | * @param $relativeTo |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | private function normalize($path, $relativeTo = null) { |
||
| 251 | } |
||
| 252 | /** |
||
| 253 | * @param array $options |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | private function parseConfiguration($options=[]) |
||
| 285 | } |
||
| 286 | /** |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | private function Save() |
||
| 339 |