Passed
Branch master (c0d55c)
by Alfred
04:23
created
exemple.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -10,16 +10,16 @@
 block discarded – undo
10 10
   'domain'=> 'monclient.com', // you can limit the key to per domain
11 11
   'expiration'=> '1390389563', // [time()+(30*24*3600)]; (30 days) you can limit the key to per expiration time
12 12
   'algorithm'=>'md5',
13
-  'serial'=>'736CC-AFB84-9B6C0-65252-6C107',//by key generator
13
+  'serial'=>'736CC-AFB84-9B6C0-65252-6C107', //by key generator
14 14
   'salt'=>pack('H*', md5(time())),
15 15
   'lastvalidation' => 98745612 // default: time(), execution du script pour la 1ere fois
16 16
 );
17 17
 $ini = new IrivenConfigManager('./licence.ini');
18
-$ini->set('licence',$licence); //with section
19
-$test = array('test1'=>'my tester1','test2'=>'my tester2','test3'=>'my tester3');			
20
-$ini->set($test);	//no section	
21
-$ini->set('repo','centos');	//no section	
22
-$ini->set('linux','repo','fedora');	//with section
18
+$ini->set('licence', $licence); //with section
19
+$test = array('test1'=>'my tester1', 'test2'=>'my tester2', 'test3'=>'my tester3');			
20
+$ini->set($test); //no section	
21
+$ini->set('repo', 'centos'); //no section	
22
+$ini->set('linux', 'repo', 'fedora'); //with section
23 23
 
24 24
 echo $ini->get('test1').PHP_EOL;
25 25
 
Please login to merge, or discard this patch.
ConfigManager.php 1 patch
Spacing   +82 added lines, -82 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
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,18 +31,18 @@  discard block
 block discarded – undo
31 31
      * @param null $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 47
             die($a->getMessage());
48 48
         }
@@ -52,31 +52,31 @@  discard block
 block discarded – undo
52 52
      * @param null $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
 block discarded – undo
86 86
      * @param null $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
 block discarded – undo
145 145
      * @param null $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
 block discarded – undo
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
 block discarded – undo
195 195
      * @param null $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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
-            die( $b->getMessage());
282
+            die($b->getMessage());
283 283
         }
284 284
         return $this->Config;
285 285
     }
@@ -288,9 +288,9 @@  discard block
 block discarded – undo
288 288
      */
289 289
      private function Save()
290 290
     {
291
-        if( !is_writeable( $this->targetFile ) ) @chmod($this->targetFile,0775);
291
+        if (!is_writeable($this->targetFile)) @chmod($this->targetFile, 0775);
292 292
         $content = null;
293
-        switch($this->Options['driver'])
293
+        switch ($this->Options['driver'])
294 294
         {
295 295
             case 'JSON':
296 296
                 $content .= json_encode(serialize($this->Config));
@@ -299,28 +299,28 @@  discard block
 block discarded – undo
299 299
                 $content .= '; @file generator: Iriven France Php "'.get_class($this).'" Class'.PHP_EOL;
300 300
                 $content .= '; @Last Update: '.date('Y-m-d H:i:s').PHP_EOL;
301 301
                 $content .= PHP_EOL;
302
-                foreach($this->Config as $section => $array)
302
+                foreach ($this->Config as $section => $array)
303 303
                 {
304 304
                     is_array($array) or $array = array($array);
305
-                    $content .= '[' . $section . ']'.PHP_EOL;
306
-                    foreach( $array as $key => $value )
305
+                    $content .= '['.$section.']'.PHP_EOL;
306
+                    foreach ($array as $key => $value)
307 307
                         $content .= PHP_TAB.$key.' = '.$value.PHP_EOL;
308 308
                     $content .= PHP_EOL;
309 309
                 }
310 310
                 break;
311 311
             case 'YML':
312
-                $content .= yaml_emit ($this->Config, YAML_UTF8_ENCODING , YAML_LN_BREAK );
312
+                $content .= yaml_emit($this->Config, YAML_UTF8_ENCODING, YAML_LN_BREAK);
313 313
                 break;
314 314
             default:
315 315
                 $content .= '<?php'.PHP_EOL;
316 316
                 $content .= 'return ';
317
-                $content .= var_export($this->Config, true) . ';';
317
+                $content .= var_export($this->Config, true).';';
318 318
                 $content = preg_replace('/array\s+\(/', '[', $content);
319 319
                 $content = preg_replace('/,(\s+)\)/', '$1]', $content);
320 320
                 break;
321 321
         }
322 322
         file_put_contents($this->targetFile, $content, LOCK_EX);
323
-        @chmod($this->targetFile,0644);
323
+        @chmod($this->targetFile, 0644);
324 324
         return true;
325 325
     }
326 326
     /**
Please login to merge, or discard this patch.