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