Completed
Branch master (2597c3)
by Alfred
03:11 queued 01:21
created
ConfigManager.php 1 patch
Braces   +114 added lines, -69 removed lines patch added patch discarded remove patch
@@ -36,13 +36,13 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
     }
Please login to merge, or discard this patch.