@@ -10,18 +10,18 @@ discard block |
||
| 10 | 10 | class ConfigManager |
| 11 | 11 | { |
| 12 | 12 | /** |
| 13 | - * Chemin complet du fichier de configuration |
|
| 14 | - */ |
|
| 13 | + * Chemin complet du fichier de configuration |
|
| 14 | + */ |
|
| 15 | 15 | private $targetFile; |
| 16 | 16 | private $Options = []; |
| 17 | 17 | /** |
| 18 | - * tableau multidimensionnel contenant les donnée de configuration, initialement |
|
| 19 | - * chargées depuis le fichier |
|
| 20 | - */ |
|
| 18 | + * tableau multidimensionnel contenant les donnée de configuration, initialement |
|
| 19 | + * chargées depuis le fichier |
|
| 20 | + */ |
|
| 21 | 21 | private $Config = []; |
| 22 | 22 | /** |
| 23 | - * processeurs de fichier pris en charge par l'application |
|
| 24 | - */ |
|
| 23 | + * processeurs de fichier pris en charge par l'application |
|
| 24 | + */ |
|
| 25 | 25 | private $availableDrivers = array('JSON', 'PHP','INI','YML'); |
| 26 | 26 | |
| 27 | 27 | private $defaultSection = 'runtime'; |
@@ -47,11 +47,11 @@ discard block |
||
| 47 | 47 | trigger_error($a->getMessage(),E_USER_ERROR); |
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | - /** |
|
| 51 | - * @param $section |
|
| 52 | - * @param $item |
|
| 53 | - * @return array|bool|mixed |
|
| 54 | - */ |
|
| 50 | + /** |
|
| 51 | + * @param $section |
|
| 52 | + * @param $item |
|
| 53 | + * @return array|bool|mixed |
|
| 54 | + */ |
|
| 55 | 55 | public function get($section=null, $item=null) |
| 56 | 56 | { |
| 57 | 57 | if($item) $item = trim(strtolower($item)); |
@@ -80,12 +80,12 @@ discard block |
||
| 80 | 80 | return $this->Config[$section][$item]; |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @param $section |
|
| 85 | - * @param $item |
|
| 86 | - * @param $value |
|
| 87 | - * @return bool |
|
| 88 | - */ |
|
| 83 | + /** |
|
| 84 | + * @param $section |
|
| 85 | + * @param $item |
|
| 86 | + * @param $value |
|
| 87 | + * @return bool |
|
| 88 | + */ |
|
| 89 | 89 | public function set($section,$item=null,$value=null) |
| 90 | 90 | { |
| 91 | 91 | ob_start(); |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | case 1: |
| 97 | 97 | if(!is_array($arguments[0])) return false; |
| 98 | 98 | $item=array_change_key_case($arguments[0], CASE_LOWER); $section=null; $value=null; |
| 99 | - break; |
|
| 99 | + break; |
|
| 100 | 100 | case 2: |
| 101 | 101 | if(is_array($arguments[0])) return false; |
| 102 | 102 | $_arg = strtolower(trim($arguments[0])); |
@@ -215,9 +215,9 @@ discard block |
||
| 215 | 215 | $Options['driver'] = strtoupper(pathinfo($Options['filename'], PATHINFO_EXTENSION)); |
| 216 | 216 | else |
| 217 | 217 | $Options['filename']= $Options['filename'].'.'.strtolower($default['driver']); |
| 218 | - if(!in_array($Options['driver'],$this->availableDrivers)) |
|
| 218 | + if(!in_array($Options['driver'],$this->availableDrivers)) |
|
| 219 | 219 | throw new Exception('ERROR: driver "'.$Options['driver'].'" not supported'); |
| 220 | - $this->Options = array_merge($default,$Options); |
|
| 220 | + $this->Options = array_merge($default,$Options); |
|
| 221 | 221 | return $this->Options; |
| 222 | 222 | } |
| 223 | 223 | /** |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | } |
| 250 | 250 | return $path; |
| 251 | 251 | } |
| 252 | - /** |
|
| 252 | + /** |
|
| 253 | 253 | * @param array $options |
| 254 | 254 | * @return mixed |
| 255 | 255 | */ |
@@ -286,15 +286,15 @@ discard block |
||
| 286 | 286 | /** |
| 287 | 287 | * @return bool |
| 288 | 288 | */ |
| 289 | - private function Save() |
|
| 289 | + private function Save() |
|
| 290 | + { |
|
| 291 | + try { |
|
| 292 | + if( !is_writeable( $this->targetFile ) ) |
|
| 290 | 293 | { |
| 291 | - try { |
|
| 292 | - if( !is_writeable( $this->targetFile ) ) |
|
| 293 | - { |
|
| 294 | - if(@chmod($this->targetFile,0775)) |
|
| 295 | - throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 296 | - } |
|
| 297 | - $content = null; |
|
| 294 | + if(@chmod($this->targetFile,0775)) |
|
| 295 | + throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 296 | + } |
|
| 297 | + $content = null; |
|
| 298 | 298 | switch($this->Options['driver']) |
| 299 | 299 | { |
| 300 | 300 | case 'JSON': |
@@ -325,11 +325,11 @@ discard block |
||
| 325 | 325 | break; |
| 326 | 326 | } |
| 327 | 327 | file_put_contents($this->targetFile, $content, LOCK_EX); |
| 328 | - if(@chmod($this->targetFile,0644)) |
|
| 329 | - throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 330 | - } |
|
| 331 | - catch (Exception $e) |
|
| 332 | - { trigger_error($e->getMessage(),E_USER_ERROR);} |
|
| 328 | + if(@chmod($this->targetFile,0644)) |
|
| 329 | + throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 330 | + } |
|
| 331 | + catch (Exception $e) |
|
| 332 | + { trigger_error($e->getMessage(),E_USER_ERROR);} |
|
| 333 | 333 | return true; |
| 334 | 334 | } |
| 335 | 335 | /** |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | /** |
| 23 | 23 | * processeurs de fichier pris en charge par l'application |
| 24 | 24 | */ |
| 25 | - private $availableDrivers = array('JSON', 'PHP','INI','YML'); |
|
| 25 | + private $availableDrivers = array('JSON', 'PHP', 'INI', 'YML'); |
|
| 26 | 26 | |
| 27 | 27 | private $defaultSection = 'runtime'; |
| 28 | 28 | |
@@ -31,20 +31,20 @@ discard block |
||
| 31 | 31 | * @param $location |
| 32 | 32 | * @throws \Exception |
| 33 | 33 | */ |
| 34 | - public function __construct( $filename, $location=null ) |
|
| 34 | + public function __construct($filename, $location = null) |
|
| 35 | 35 | { |
| 36 | 36 | $numargs = func_num_args(); |
| 37 | 37 | try |
| 38 | 38 | { |
| 39 | - if($numargs > 2) |
|
| 39 | + if ($numargs > 2) |
|
| 40 | 40 | throw new Exception('SETUP ERROR: configuration manager can accept only up to 2 parameters,'.$numargs.' given!'); |
| 41 | - $this->configureOptions($filename,$location); |
|
| 41 | + $this->configureOptions($filename, $location); |
|
| 42 | 42 | $this->parseConfiguration($this->Options); |
| 43 | 43 | return $this; |
| 44 | 44 | } |
| 45 | - catch(Exception $a) |
|
| 45 | + catch (Exception $a) |
|
| 46 | 46 | { |
| 47 | - trigger_error($a->getMessage(),E_USER_ERROR); |
|
| 47 | + trigger_error($a->getMessage(), E_USER_ERROR); |
|
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | /** |
@@ -52,31 +52,31 @@ discard block |
||
| 52 | 52 | * @param $item |
| 53 | 53 | * @return array|bool|mixed |
| 54 | 54 | */ |
| 55 | - public function get($section=null, $item=null) |
|
| 55 | + public function get($section = null, $item = null) |
|
| 56 | 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) |
|
| 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 | 62 | { |
| 63 | - if(!isset($this->Config[$section])) |
|
| 63 | + if (!isset($this->Config[$section])) |
|
| 64 | 64 | { |
| 65 | 65 | $key = $item; |
| 66 | 66 | $item = $section; |
| 67 | 67 | $section = $this->defaultSection; |
| 68 | - if(!isset($this->Config[$section][$item][$key])) |
|
| 68 | + if (!isset($this->Config[$section][$item][$key])) |
|
| 69 | 69 | return false; |
| 70 | 70 | return $this->Config[$section][$item][$key]; |
| 71 | 71 | } |
| 72 | 72 | } |
| 73 | - elseif(!$item or !strlen($item)) |
|
| 73 | + elseif (!$item or !strlen($item)) |
|
| 74 | 74 | { |
| 75 | 75 | $item = $section; |
| 76 | - if(isset($this->Config[$item])) return $this->Config[$item]; |
|
| 76 | + if (isset($this->Config[$item])) return $this->Config[$item]; |
|
| 77 | 77 | $section = $this->defaultSection; |
| 78 | 78 | } |
| 79 | - if(!isset($this->Config[$section][$item])) return false; |
|
| 79 | + if (!isset($this->Config[$section][$item])) return false; |
|
| 80 | 80 | return $this->Config[$section][$item]; |
| 81 | 81 | } |
| 82 | 82 | |
@@ -86,53 +86,53 @@ discard block |
||
| 86 | 86 | * @param $value |
| 87 | 87 | * @return bool |
| 88 | 88 | */ |
| 89 | - public function set($section,$item=null,$value=null) |
|
| 89 | + public function set($section, $item = null, $value = null) |
|
| 90 | 90 | { |
| 91 | 91 | ob_start(); |
| 92 | 92 | $numarg = func_num_args(); |
| 93 | - $arguments=func_get_args(); |
|
| 94 | - switch($numarg) |
|
| 93 | + $arguments = func_get_args(); |
|
| 94 | + switch ($numarg) |
|
| 95 | 95 | { |
| 96 | 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; |
|
| 97 | + if (!is_array($arguments[0])) return false; |
|
| 98 | + $item = array_change_key_case($arguments[0], CASE_LOWER); $section = null; $value = null; |
|
| 99 | 99 | break; |
| 100 | 100 | case 2: |
| 101 | - if(is_array($arguments[0])) return false; |
|
| 101 | + if (is_array($arguments[0])) return false; |
|
| 102 | 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;} |
|
| 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 | 105 | break; |
| 106 | 106 | default: |
| 107 | 107 | break; |
| 108 | 108 | } |
| 109 | - $section = $section? trim(strtolower($section)) : $this->defaultSection; |
|
| 110 | - if(!is_array($item)) |
|
| 109 | + $section = $section ? trim(strtolower($section)) : $this->defaultSection; |
|
| 110 | + if (!is_array($item)) |
|
| 111 | 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; |
|
| 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 | 116 | else: |
| 117 | - if(!is_array($value)) $value = array($value); |
|
| 118 | - $this->Config[$section][$item] = array_merge($this->Config[$section][$item],$value); |
|
| 117 | + if (!is_array($value)) $value = array($value); |
|
| 118 | + $this->Config[$section][$item] = array_merge($this->Config[$section][$item], $value); |
|
| 119 | 119 | endif; |
| 120 | 120 | } |
| 121 | 121 | else |
| 122 | 122 | { |
| 123 | - if($value) return false; |
|
| 123 | + if ($value) return false; |
|
| 124 | 124 | $item = array_change_key_case($item, CASE_LOWER); |
| 125 | 125 | $sectionsize = count($this->Config[$section]); |
| 126 | 126 | $itemsize = count($item); |
| 127 | - if($sectionsize) |
|
| 127 | + if ($sectionsize) |
|
| 128 | 128 | { |
| 129 | - if($itemsize=='1') |
|
| 129 | + if ($itemsize == '1') |
|
| 130 | 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)]; |
|
| 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 | 134 | } |
| 135 | - else $this->Config[$section] = array_merge($this->Config[$section],$item); |
|
| 135 | + else $this->Config[$section] = array_merge($this->Config[$section], $item); |
|
| 136 | 136 | } |
| 137 | 137 | else $this->Config[$section] = $item; |
| 138 | 138 | } |
@@ -145,30 +145,30 @@ discard block |
||
| 145 | 145 | * @param $item |
| 146 | 146 | * @return bool |
| 147 | 147 | */ |
| 148 | - public function del($section, $item=null) |
|
| 148 | + public function del($section, $item = null) |
|
| 149 | 149 | { |
| 150 | 150 | $section = trim(strtolower($section)); |
| 151 | - if($item and strlen($item)) |
|
| 151 | + if ($item and strlen($item)) |
|
| 152 | 152 | { |
| 153 | 153 | $item = trim(strtolower($item)); |
| 154 | - if(!isset($this->Config[$section])) |
|
| 154 | + if (!isset($this->Config[$section])) |
|
| 155 | 155 | { |
| 156 | 156 | $key = $item; |
| 157 | 157 | $item = $section; |
| 158 | 158 | $section = $this->defaultSection; |
| 159 | - if(isset($this->Config[$section][$item][$key])) |
|
| 159 | + if (isset($this->Config[$section][$item][$key])) |
|
| 160 | 160 | { |
| 161 | - $itemSize=count($this->Config[$section][$item]); |
|
| 162 | - if($itemSize>1) unset($this->Config[$section][$item][$key]); |
|
| 161 | + $itemSize = count($this->Config[$section][$item]); |
|
| 162 | + if ($itemSize > 1) unset($this->Config[$section][$item][$key]); |
|
| 163 | 163 | else unset($this->Config[$section]); |
| 164 | 164 | } |
| 165 | 165 | } |
| 166 | 166 | else |
| 167 | 167 | { |
| 168 | - $sectionSize=count($this->Config[$section]); |
|
| 169 | - if(isset($this->Config[$section][$item])) |
|
| 168 | + $sectionSize = count($this->Config[$section]); |
|
| 169 | + if (isset($this->Config[$section][$item])) |
|
| 170 | 170 | { |
| 171 | - if($sectionSize>1) unset($this->Config[$section][$item]); |
|
| 171 | + if ($sectionSize > 1) unset($this->Config[$section][$item]); |
|
| 172 | 172 | else unset($this->Config[$section]); |
| 173 | 173 | } |
| 174 | 174 | } |
@@ -176,13 +176,13 @@ discard block |
||
| 176 | 176 | else |
| 177 | 177 | { |
| 178 | 178 | $item = $section; |
| 179 | - if(!isset($this->Config[$item])) |
|
| 179 | + if (!isset($this->Config[$item])) |
|
| 180 | 180 | { |
| 181 | 181 | $section = $this->defaultSection; |
| 182 | 182 | $defaultSectionSize = count($this->Config[$section]); |
| 183 | - if(isset($this->Config[$section][$item])) |
|
| 183 | + if (isset($this->Config[$section][$item])) |
|
| 184 | 184 | { |
| 185 | - if($defaultSectionSize>1) unset($this->Config[$section][$item]); |
|
| 185 | + if ($defaultSectionSize > 1) unset($this->Config[$section][$item]); |
|
| 186 | 186 | else unset($this->Config[$section]); |
| 187 | 187 | } |
| 188 | 188 | } |
@@ -195,29 +195,29 @@ discard block |
||
| 195 | 195 | * @param $location |
| 196 | 196 | * @return array|bool |
| 197 | 197 | */ |
| 198 | - private function configureOptions($file,$location=null){ |
|
| 199 | - if(!is_string($file) or ($location and !is_string($location))) |
|
| 198 | + private function configureOptions($file, $location = null) { |
|
| 199 | + if (!is_string($file) or ($location and !is_string($location))) |
|
| 200 | 200 | throw new Exception('SETUP ERROR: configuration manager can accept string only parameters'); |
| 201 | - $default=[ |
|
| 201 | + $default = [ |
|
| 202 | 202 | 'driver' => 'PHP', |
| 203 | 203 | 'filename' => null, |
| 204 | 204 | 'directory' => __DIR__, |
| 205 | 205 | ]; |
| 206 | 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); |
|
| 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 | 212 | } |
| 213 | 213 | $Options['filename'] = basename($file); |
| 214 | - if(strpos($Options['filename'],'.')!==false) |
|
| 214 | + if (strpos($Options['filename'], '.') !== false) |
|
| 215 | 215 | $Options['driver'] = strtoupper(pathinfo($Options['filename'], PATHINFO_EXTENSION)); |
| 216 | 216 | else |
| 217 | - $Options['filename']= $Options['filename'].'.'.strtolower($default['driver']); |
|
| 218 | - if(!in_array($Options['driver'],$this->availableDrivers)) |
|
| 217 | + $Options['filename'] = $Options['filename'].'.'.strtolower($default['driver']); |
|
| 218 | + if (!in_array($Options['driver'], $this->availableDrivers)) |
|
| 219 | 219 | throw new Exception('ERROR: driver "'.$Options['driver'].'" not supported'); |
| 220 | - $this->Options = array_merge($default,$Options); |
|
| 220 | + $this->Options = array_merge($default, $Options); |
|
| 221 | 221 | return $this->Options; |
| 222 | 222 | } |
| 223 | 223 | /** |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | */ |
| 228 | 228 | private function normalize($path, $relativeTo = null) { |
| 229 | 229 | $path = rtrim(preg_replace('#[/\\\\]+#', DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR); |
| 230 | - $isAbsolute = stripos(PHP_OS, 'win')===0 ? preg_match('/^[A-Za-z]+:/', $path): !strncmp($path, DIRECTORY_SEPARATOR, 1); |
|
| 230 | + $isAbsolute = stripos(PHP_OS, 'win') === 0 ? preg_match('/^[A-Za-z]+:/', $path) : !strncmp($path, DIRECTORY_SEPARATOR, 1); |
|
| 231 | 231 | if (!$isAbsolute) |
| 232 | 232 | { |
| 233 | 233 | if (!$relativeTo) $relativeTo = getcwd(); |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | while (end($parts) !== false) |
| 241 | 241 | { |
| 242 | 242 | array_pop($parts); |
| 243 | - $attempt = stripos(PHP_OS, 'win')===0 ? implode(DIRECTORY_SEPARATOR, $parts): DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts); |
|
| 243 | + $attempt = stripos(PHP_OS, 'win') === 0 ? implode(DIRECTORY_SEPARATOR, $parts) : DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $parts); |
|
| 244 | 244 | if ($realpaths = realpath($attempt)) |
| 245 | 245 | { |
| 246 | 246 | $path = $realpaths.substr($path, strlen($attempt)); |
@@ -253,14 +253,14 @@ discard block |
||
| 253 | 253 | * @param array $options |
| 254 | 254 | * @return mixed |
| 255 | 255 | */ |
| 256 | - private function parseConfiguration($options=[]) |
|
| 256 | + private function parseConfiguration($options = []) |
|
| 257 | 257 | { |
| 258 | 258 | |
| 259 | 259 | try |
| 260 | 260 | { $this->targetFile = $this->normalize($options['directory'].DIRECTORY_SEPARATOR.$options['filename']); |
| 261 | - if(!file_exists($this->targetFile)) |
|
| 262 | - file_put_contents($this->targetFile,'',LOCK_EX); |
|
| 263 | - switch($this->Options['driver']) |
|
| 261 | + if (!file_exists($this->targetFile)) |
|
| 262 | + file_put_contents($this->targetFile, '', LOCK_EX); |
|
| 263 | + switch ($this->Options['driver']) |
|
| 264 | 264 | { |
| 265 | 265 | case 'JSON': |
| 266 | 266 | $this->Config = unserialize(json_decode(file_get_contents($this->targetFile), true)); |
@@ -269,17 +269,17 @@ discard block |
||
| 269 | 269 | $this->Config = parse_ini_file($this->targetFile, true); |
| 270 | 270 | break; |
| 271 | 271 | case 'YML': |
| 272 | - $ndocs=0; |
|
| 273 | - $this->Config = yaml_parse_file($this->targetFile,0,$ndocs); |
|
| 272 | + $ndocs = 0; |
|
| 273 | + $this->Config = yaml_parse_file($this->targetFile, 0, $ndocs); |
|
| 274 | 274 | break; |
| 275 | 275 | default: |
| 276 | - if(!$this->Config = include $this->targetFile) $this->Config = []; |
|
| 276 | + if (!$this->Config = include $this->targetFile) $this->Config = []; |
|
| 277 | 277 | break; |
| 278 | 278 | } |
| 279 | 279 | } |
| 280 | - catch(Exception $b) |
|
| 280 | + catch (Exception $b) |
|
| 281 | 281 | { |
| 282 | - trigger_error($b->getMessage(),E_USER_ERROR); |
|
| 282 | + trigger_error($b->getMessage(), E_USER_ERROR); |
|
| 283 | 283 | } |
| 284 | 284 | return $this->Config; |
| 285 | 285 | } |
@@ -289,13 +289,13 @@ discard block |
||
| 289 | 289 | private function Save() |
| 290 | 290 | { |
| 291 | 291 | try { |
| 292 | - if( !is_writeable( $this->targetFile ) ) |
|
| 292 | + if (!is_writeable($this->targetFile)) |
|
| 293 | 293 | { |
| 294 | - if(@chmod($this->targetFile,0775)) |
|
| 294 | + if (@chmod($this->targetFile, 0775)) |
|
| 295 | 295 | throw new Exception('Cant write to file: '.$this->targetFile); |
| 296 | 296 | } |
| 297 | 297 | $content = null; |
| 298 | - switch($this->Options['driver']) |
|
| 298 | + switch ($this->Options['driver']) |
|
| 299 | 299 | { |
| 300 | 300 | case 'JSON': |
| 301 | 301 | $content .= json_encode(serialize($this->Config)); |
@@ -304,32 +304,32 @@ discard block |
||
| 304 | 304 | $content .= '; @file generator: Iriven France Php "'.get_class($this).'" Class'.PHP_EOL; |
| 305 | 305 | $content .= '; @Last Update: '.date('Y-m-d H:i:s').PHP_EOL; |
| 306 | 306 | $content .= PHP_EOL; |
| 307 | - foreach($this->Config as $section => $array) |
|
| 307 | + foreach ($this->Config as $section => $array) |
|
| 308 | 308 | { |
| 309 | 309 | is_array($array) or $array = array($array); |
| 310 | - $content .= '[' . $section . ']'.PHP_EOL; |
|
| 311 | - foreach( $array as $key => $value ) |
|
| 310 | + $content .= '['.$section.']'.PHP_EOL; |
|
| 311 | + foreach ($array as $key => $value) |
|
| 312 | 312 | $content .= $key.' = '.$value.PHP_EOL; |
| 313 | 313 | $content .= PHP_EOL; |
| 314 | 314 | } |
| 315 | 315 | break; |
| 316 | 316 | case 'YML': |
| 317 | - $content .= yaml_emit ($this->Config, YAML_UTF8_ENCODING , YAML_LN_BREAK ); |
|
| 317 | + $content .= yaml_emit($this->Config, YAML_UTF8_ENCODING, YAML_LN_BREAK); |
|
| 318 | 318 | break; |
| 319 | 319 | default: |
| 320 | 320 | $content .= '<?php'.PHP_EOL; |
| 321 | 321 | $content .= 'return '; |
| 322 | - $content .= var_export($this->Config, true) . ';'; |
|
| 322 | + $content .= var_export($this->Config, true).';'; |
|
| 323 | 323 | $content = preg_replace('/array\s+\(/', '[', $content); |
| 324 | 324 | $content = preg_replace('/,(\s+)\)/', '$1]', $content); |
| 325 | 325 | break; |
| 326 | 326 | } |
| 327 | 327 | file_put_contents($this->targetFile, $content, LOCK_EX); |
| 328 | - if(@chmod($this->targetFile,0644)) |
|
| 328 | + if (@chmod($this->targetFile, 0644)) |
|
| 329 | 329 | throw new Exception('Cant write to file: '.$this->targetFile); |
| 330 | 330 | } |
| 331 | 331 | catch (Exception $e) |
| 332 | - { trigger_error($e->getMessage(),E_USER_ERROR);} |
|
| 332 | + { trigger_error($e->getMessage(), E_USER_ERROR); } |
|
| 333 | 333 | return true; |
| 334 | 334 | } |
| 335 | 335 | /** |
@@ -36,13 +36,13 @@ discard block |
||
| 36 | 36 | $numargs = func_num_args(); |
| 37 | 37 | try |
| 38 | 38 | { |
| 39 | - if($numargs > 2) |
|
| 40 | - throw new Exception('SETUP ERROR: configuration manager can accept only up to 2 parameters,'.$numargs.' given!'); |
|
| 39 | + if($numargs > 2) { |
|
| 40 | + throw new Exception('SETUP ERROR: configuration manager can accept only up to 2 parameters,'.$numargs.' given!'); |
|
| 41 | + } |
|
| 41 | 42 | $this->configureOptions($filename,$location); |
| 42 | 43 | $this->parseConfiguration($this->Options); |
| 43 | 44 | return $this; |
| 44 | - } |
|
| 45 | - catch(Exception $a) |
|
| 45 | + } catch(Exception $a) |
|
| 46 | 46 | { |
| 47 | 47 | trigger_error($a->getMessage(),E_USER_ERROR); |
| 48 | 48 | } |
@@ -54,10 +54,18 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function get($section=null, $item=null) |
| 56 | 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; |
|
| 57 | + if($item) { |
|
| 58 | + $item = trim(strtolower($item)); |
|
| 59 | + } |
|
| 60 | + if($section) { |
|
| 61 | + $section = trim(strtolower($section)); |
|
| 62 | + } |
|
| 63 | + if(!count($this->Config)) { |
|
| 64 | + return false; |
|
| 65 | + } |
|
| 66 | + if(!$section or !strlen($section)) { |
|
| 67 | + return $this->Config; |
|
| 68 | + } |
|
| 61 | 69 | if($section AND $item) |
| 62 | 70 | { |
| 63 | 71 | if(!isset($this->Config[$section])) |
@@ -65,18 +73,22 @@ discard block |
||
| 65 | 73 | $key = $item; |
| 66 | 74 | $item = $section; |
| 67 | 75 | $section = $this->defaultSection; |
| 68 | - if(!isset($this->Config[$section][$item][$key])) |
|
| 69 | - return false; |
|
| 76 | + if(!isset($this->Config[$section][$item][$key])) { |
|
| 77 | + return false; |
|
| 78 | + } |
|
| 70 | 79 | return $this->Config[$section][$item][$key]; |
| 71 | 80 | } |
| 72 | - } |
|
| 73 | - elseif(!$item or !strlen($item)) |
|
| 81 | + } elseif(!$item or !strlen($item)) |
|
| 74 | 82 | { |
| 75 | 83 | $item = $section; |
| 76 | - if(isset($this->Config[$item])) return $this->Config[$item]; |
|
| 84 | + if(isset($this->Config[$item])) { |
|
| 85 | + return $this->Config[$item]; |
|
| 86 | + } |
|
| 77 | 87 | $section = $this->defaultSection; |
| 78 | 88 | } |
| 79 | - if(!isset($this->Config[$section][$item])) return false; |
|
| 89 | + if(!isset($this->Config[$section][$item])) { |
|
| 90 | + return false; |
|
| 91 | + } |
|
| 80 | 92 | return $this->Config[$section][$item]; |
| 81 | 93 | } |
| 82 | 94 | |
@@ -94,14 +106,17 @@ discard block |
||
| 94 | 106 | switch($numarg) |
| 95 | 107 | { |
| 96 | 108 | case 1: |
| 97 | - if(!is_array($arguments[0])) return false; |
|
| 109 | + if(!is_array($arguments[0])) { |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 98 | 112 | $item=array_change_key_case($arguments[0], CASE_LOWER); $section=null; $value=null; |
| 99 | 113 | break; |
| 100 | 114 | case 2: |
| 101 | - if(is_array($arguments[0])) return false; |
|
| 115 | + if(is_array($arguments[0])) { |
|
| 116 | + return false; |
|
| 117 | + } |
|
| 102 | 118 | $_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;} |
|
| 119 | + if(is_array($arguments[1])){ $section=$_arg; $item =array_change_key_case($arguments[1], CASE_LOWER);$value=null;} else {$item = $_arg;$value=$arguments[1];$section=null;} |
|
| 105 | 120 | break; |
| 106 | 121 | default: |
| 107 | 122 | break; |
@@ -109,18 +124,23 @@ discard block |
||
| 109 | 124 | $section = $section? trim(strtolower($section)) : $this->defaultSection; |
| 110 | 125 | if(!is_array($item)) |
| 111 | 126 | { |
| 112 | - if(!$value) return false; |
|
| 127 | + if(!$value) { |
|
| 128 | + return false; |
|
| 129 | + } |
|
| 113 | 130 | $item=trim(strtolower($item)); |
| 114 | 131 | if(!isset($this->Config[$section][$item]) or !is_array($this->Config[$section][$item])): |
| 115 | 132 | $this->Config[$section][$item]=$value; |
| 116 | - else: |
|
| 133 | + else { |
|
| 134 | + : |
|
| 117 | 135 | if(!is_array($value)) $value = array($value); |
| 136 | + } |
|
| 118 | 137 | $this->Config[$section][$item] = array_merge($this->Config[$section][$item],$value); |
| 119 | 138 | endif; |
| 120 | - } |
|
| 121 | - else |
|
| 139 | + } else |
|
| 122 | 140 | { |
| 123 | - if($value) return false; |
|
| 141 | + if($value) { |
|
| 142 | + return false; |
|
| 143 | + } |
|
| 124 | 144 | $item = array_change_key_case($item, CASE_LOWER); |
| 125 | 145 | $sectionsize = count($this->Config[$section]); |
| 126 | 146 | $itemsize = count($item); |
@@ -128,13 +148,17 @@ discard block |
||
| 128 | 148 | { |
| 129 | 149 | if($itemsize=='1') |
| 130 | 150 | { |
| 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)]; |
|
| 151 | + if(isset($this->Config[$section][key($item)])) { |
|
| 152 | + $this->Config[$section][key($item)] = array_merge($this->Config[$section][key($item)],$item[key($item)]); |
|
| 153 | + } else if(!is_numeric(key($item))) { |
|
| 154 | + $this->Config[$section][key($item)]=$item[key($item)]; |
|
| 155 | + } |
|
| 156 | + } else { |
|
| 157 | + $this->Config[$section] = array_merge($this->Config[$section],$item); |
|
| 134 | 158 | } |
| 135 | - else $this->Config[$section] = array_merge($this->Config[$section],$item); |
|
| 159 | + } else { |
|
| 160 | + $this->Config[$section] = $item; |
|
| 136 | 161 | } |
| 137 | - else $this->Config[$section] = $item; |
|
| 138 | 162 | } |
| 139 | 163 | $re = $this->Save(); |
| 140 | 164 | ob_end_clean(); |
@@ -159,21 +183,25 @@ discard block |
||
| 159 | 183 | if(isset($this->Config[$section][$item][$key])) |
| 160 | 184 | { |
| 161 | 185 | $itemSize=count($this->Config[$section][$item]); |
| 162 | - if($itemSize>1) unset($this->Config[$section][$item][$key]); |
|
| 163 | - else unset($this->Config[$section]); |
|
| 186 | + if($itemSize>1) { |
|
| 187 | + unset($this->Config[$section][$item][$key]); |
|
| 188 | + } else { |
|
| 189 | + unset($this->Config[$section]); |
|
| 190 | + } |
|
| 164 | 191 | } |
| 165 | - } |
|
| 166 | - else |
|
| 192 | + } else |
|
| 167 | 193 | { |
| 168 | 194 | $sectionSize=count($this->Config[$section]); |
| 169 | 195 | if(isset($this->Config[$section][$item])) |
| 170 | 196 | { |
| 171 | - if($sectionSize>1) unset($this->Config[$section][$item]); |
|
| 172 | - else unset($this->Config[$section]); |
|
| 197 | + if($sectionSize>1) { |
|
| 198 | + unset($this->Config[$section][$item]); |
|
| 199 | + } else { |
|
| 200 | + unset($this->Config[$section]); |
|
| 201 | + } |
|
| 173 | 202 | } |
| 174 | 203 | } |
| 175 | - } |
|
| 176 | - else |
|
| 204 | + } else |
|
| 177 | 205 | { |
| 178 | 206 | $item = $section; |
| 179 | 207 | if(!isset($this->Config[$item])) |
@@ -182,11 +210,15 @@ discard block |
||
| 182 | 210 | $defaultSectionSize = count($this->Config[$section]); |
| 183 | 211 | if(isset($this->Config[$section][$item])) |
| 184 | 212 | { |
| 185 | - if($defaultSectionSize>1) unset($this->Config[$section][$item]); |
|
| 186 | - else unset($this->Config[$section]); |
|
| 213 | + if($defaultSectionSize>1) { |
|
| 214 | + unset($this->Config[$section][$item]); |
|
| 215 | + } else { |
|
| 216 | + unset($this->Config[$section]); |
|
| 217 | + } |
|
| 187 | 218 | } |
| 219 | + } else { |
|
| 220 | + unset($this->Config[$item]); |
|
| 188 | 221 | } |
| 189 | - else unset($this->Config[$item]); |
|
| 190 | 222 | } |
| 191 | 223 | return $this->Save(); |
| 192 | 224 | } |
@@ -196,27 +228,31 @@ discard block |
||
| 196 | 228 | * @return array|bool |
| 197 | 229 | */ |
| 198 | 230 | 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'); |
|
| 231 | + if(!is_string($file) or ($location and !is_string($location))) { |
|
| 232 | + throw new Exception('SETUP ERROR: configuration manager can accept string only parameters'); |
|
| 233 | + } |
|
| 201 | 234 | $default=[ |
| 202 | 235 | 'driver' => 'PHP', |
| 203 | 236 | 'filename' => null, |
| 204 | 237 | 'directory' => __DIR__, |
| 205 | 238 | ]; |
| 206 | 239 | $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); |
|
| 240 | + if($location) { |
|
| 241 | + $Options['directory']=rtrim($this->normalize($location),DIRECTORY_SEPARATOR); |
|
| 242 | + } else{ |
|
| 243 | + if(basename($file)!==$file) { |
|
| 244 | + $Options['directory']= rtrim($this->normalize(pathinfo($file,PATHINFO_DIRNAME)),DIRECTORY_SEPARATOR); |
|
| 245 | + } |
|
| 212 | 246 | } |
| 213 | 247 | $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'); |
|
| 248 | + if(strpos($Options['filename'],'.')!==false) { |
|
| 249 | + $Options['driver'] = strtoupper(pathinfo($Options['filename'], PATHINFO_EXTENSION)); |
|
| 250 | + } else { |
|
| 251 | + $Options['filename']= $Options['filename'].'.'.strtolower($default['driver']); |
|
| 252 | + } |
|
| 253 | + if(!in_array($Options['driver'],$this->availableDrivers)) { |
|
| 254 | + throw new Exception('ERROR: driver "'.$Options['driver'].'" not supported'); |
|
| 255 | + } |
|
| 220 | 256 | $this->Options = array_merge($default,$Options); |
| 221 | 257 | return $this->Options; |
| 222 | 258 | } |
@@ -230,12 +266,17 @@ discard block |
||
| 230 | 266 | $isAbsolute = stripos(PHP_OS, 'win')===0 ? preg_match('/^[A-Za-z]+:/', $path): !strncmp($path, DIRECTORY_SEPARATOR, 1); |
| 231 | 267 | if (!$isAbsolute) |
| 232 | 268 | { |
| 233 | - if (!$relativeTo) $relativeTo = getcwd(); |
|
| 269 | + if (!$relativeTo) { |
|
| 270 | + $relativeTo = getcwd(); |
|
| 271 | + } |
|
| 234 | 272 | $path = $relativeTo.DIRECTORY_SEPARATOR.$path; |
| 235 | 273 | } |
| 236 | - if (is_link($path) and ($parentPath = realpath(dirname($path)))) |
|
| 237 | - return $parentPath.DIRECTORY_SEPARATOR.$path; |
|
| 238 | - if ($realpath = realpath($path)) return $realpath; |
|
| 274 | + if (is_link($path) and ($parentPath = realpath(dirname($path)))) { |
|
| 275 | + return $parentPath.DIRECTORY_SEPARATOR.$path; |
|
| 276 | + } |
|
| 277 | + if ($realpath = realpath($path)) { |
|
| 278 | + return $realpath; |
|
| 279 | + } |
|
| 239 | 280 | $parts = explode(DIRECTORY_SEPARATOR, trim($path, DIRECTORY_SEPARATOR)); |
| 240 | 281 | while (end($parts) !== false) |
| 241 | 282 | { |
@@ -258,8 +299,9 @@ discard block |
||
| 258 | 299 | |
| 259 | 300 | try |
| 260 | 301 | { $this->targetFile = $this->normalize($options['directory'].DIRECTORY_SEPARATOR.$options['filename']); |
| 261 | - if(!file_exists($this->targetFile)) |
|
| 262 | - file_put_contents($this->targetFile,'',LOCK_EX); |
|
| 302 | + if(!file_exists($this->targetFile)) { |
|
| 303 | + file_put_contents($this->targetFile,'',LOCK_EX); |
|
| 304 | + } |
|
| 263 | 305 | switch($this->Options['driver']) |
| 264 | 306 | { |
| 265 | 307 | case 'JSON': |
@@ -273,11 +315,12 @@ discard block |
||
| 273 | 315 | $this->Config = yaml_parse_file($this->targetFile,0,$ndocs); |
| 274 | 316 | break; |
| 275 | 317 | default: |
| 276 | - if(!$this->Config = include $this->targetFile) $this->Config = []; |
|
| 318 | + if(!$this->Config = include $this->targetFile) { |
|
| 319 | + $this->Config = []; |
|
| 320 | + } |
|
| 277 | 321 | break; |
| 278 | 322 | } |
| 279 | - } |
|
| 280 | - catch(Exception $b) |
|
| 323 | + } catch(Exception $b) |
|
| 281 | 324 | { |
| 282 | 325 | trigger_error($b->getMessage(),E_USER_ERROR); |
| 283 | 326 | } |
@@ -291,8 +334,9 @@ discard block |
||
| 291 | 334 | try { |
| 292 | 335 | if( !is_writeable( $this->targetFile ) ) |
| 293 | 336 | { |
| 294 | - if(@chmod($this->targetFile,0775)) |
|
| 295 | - throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 337 | + if(@chmod($this->targetFile,0775)) { |
|
| 338 | + throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 339 | + } |
|
| 296 | 340 | } |
| 297 | 341 | $content = null; |
| 298 | 342 | switch($this->Options['driver']) |
@@ -308,8 +352,9 @@ discard block |
||
| 308 | 352 | { |
| 309 | 353 | is_array($array) or $array = array($array); |
| 310 | 354 | $content .= '[' . $section . ']'.PHP_EOL; |
| 311 | - foreach( $array as $key => $value ) |
|
| 312 | - $content .= $key.' = '.$value.PHP_EOL; |
|
| 355 | + foreach( $array as $key => $value ) { |
|
| 356 | + $content .= $key.' = '.$value.PHP_EOL; |
|
| 357 | + } |
|
| 313 | 358 | $content .= PHP_EOL; |
| 314 | 359 | } |
| 315 | 360 | break; |
@@ -325,10 +370,10 @@ discard block |
||
| 325 | 370 | break; |
| 326 | 371 | } |
| 327 | 372 | file_put_contents($this->targetFile, $content, LOCK_EX); |
| 328 | - if(@chmod($this->targetFile,0644)) |
|
| 329 | - throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 330 | - } |
|
| 331 | - catch (Exception $e) |
|
| 373 | + if(@chmod($this->targetFile,0644)) { |
|
| 374 | + throw new Exception('Cant write to file: '.$this->targetFile); |
|
| 375 | + } |
|
| 376 | + } catch (Exception $e) |
|
| 332 | 377 | { trigger_error($e->getMessage(),E_USER_ERROR);} |
| 333 | 378 | return true; |
| 334 | 379 | } |