diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..eac82c5 --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,21 @@ +build: + nodes: + analysis: + project_setup: + override: + - 'true' + tests: + override: + - php-scrutinizer-run + - + command: phpcs-run + use_website_config: true + tests: true +checks: + php: true +coding_style: + php: + indentation: + general: + size: 2 +filter: { } diff --git a/ConfigManager.php b/ConfigManager.php index c87a008..0a965fa 100644 --- a/ConfigManager.php +++ b/ConfigManager.php @@ -1,35 +1,36 @@ configureOptions($filename,$location); $this->parseConfiguration($this->Options); - return $this; } catch(Exception $a) { - die($a->getMessage()); + trigger_error($a->getMessage(),E_USER_ERROR); } + return $this; } - /** - * @param null $section - * @param null $item + + /** + * @param $section + * @param $item * @return array|bool|mixed */ public function get($section=null, $item=null) @@ -79,15 +81,14 @@ public function get($section=null, $item=null) if(!isset($this->Config[$section][$item])) return false; return $this->Config[$section][$item]; } - - /** - * @param null $section + /** + * @param $section * @param $item - * @param null $value + * @param $value * @return bool */ public function set($section,$item=null,$value=null) - { + { ob_start(); $numarg = func_num_args(); $arguments=func_get_args(); @@ -95,8 +96,8 @@ public function set($section,$item=null,$value=null) { case 1: if(!is_array($arguments[0])) return false; - $item=array_change_key_case($arguments[0], CASE_LOWER); $section=null; $value=null; - break; + $item=array_change_key_case($arguments[0], CASE_LOWER); $section=null; $value=null; + break; case 2: if(is_array($arguments[0])) return false; $_arg = strtolower(trim($arguments[0])); @@ -140,9 +141,9 @@ public function set($section,$item=null,$value=null) ob_end_clean(); return $re; } -/** + /** * @param $section - * @param null $item + * @param $item * @return bool */ public function del($section, $item=null) @@ -171,7 +172,7 @@ public function del($section, $item=null) if($sectionSize>1) unset($this->Config[$section][$item]); else unset($this->Config[$section]); } - } + } } else { @@ -190,10 +191,12 @@ public function del($section, $item=null) } return $this->Save(); } + /** * @param $file - * @param null $location - * @return array|bool + * @param $location + * @return array + * @throws Exception */ private function configureOptions($file,$location=null){ if(!is_string($file) or ($location and !is_string($location))) @@ -215,15 +218,16 @@ private function configureOptions($file,$location=null){ $Options['driver'] = strtoupper(pathinfo($Options['filename'], PATHINFO_EXTENSION)); else $Options['filename']= $Options['filename'].'.'.strtolower($default['driver']); - if(!in_array($Options['driver'],$this->$availableDrivers)) + if(!in_array($Options['driver'],$this->availableDrivers)) throw new Exception('ERROR: driver "'.$Options['driver'].'" not supported'); - $this->Options = array_merge($default,$Options); + $this->Options = array_merge($default,$Options); return $this->Options; } + /** * @param $path - * @param null $relativeTo - * @return string + * @param $relativeTo + * @return bool|string */ private function normalize($path, $relativeTo = null) { $path = rtrim(preg_replace('#[/\\\\]+#', DIRECTORY_SEPARATOR, $path), DIRECTORY_SEPARATOR); @@ -249,13 +253,14 @@ private function normalize($path, $relativeTo = null) { } return $path; } - /** + + /** * @param array $options - * @return mixed + * @return array|bool|mixed */ + private function parseConfiguration($options=[]) { - try { $this->targetFile = $this->normalize($options['directory'].DIRECTORY_SEPARATOR.$options['filename']); if(!file_exists($this->targetFile)) @@ -279,52 +284,54 @@ private function parseConfiguration($options=[]) } catch(Exception $b) { - die( $b->getMessage()); + trigger_error($b->getMessage(),E_USER_ERROR); } return $this->Config; } -/** + + /** * @return bool */ - private function Save() + private function Save() { - if( !is_writeable( $this->targetFile ) ) @chmod($this->targetFile,0775); - $content = null; - switch($this->Options['driver']) - { - case 'JSON': - $content .= json_encode(serialize($this->Config)); - break; - case 'INI': - $content .= '; @file generator: Iriven France Php "'.get_class($this).'" Class'.PHP_EOL; - $content .= '; @Last Update: '.date('Y-m-d H:i:s').PHP_EOL; - $content .= PHP_EOL; - foreach($this->Config as $section => $array) - { - is_array($array) or $array = array($array); - $content .= '[' . $section . ']'.PHP_EOL; - foreach( $array as $key => $value ) - $content .= PHP_TAB.$key.' = '.$value.PHP_EOL; + try { + $content = null; + switch($this->Options['driver']) + { + case 'JSON': + $content .= json_encode(serialize($this->Config)); + break; + case 'INI': + $content .= '; @file generator: Iriven France Php "'.get_class($this).'" Class'.PHP_EOL; + $content .= '; @Last Update: '.date('Y-m-d H:i:s').PHP_EOL; $content .= PHP_EOL; - } - break; - case 'YML': - $content .= yaml_emit ($this->Config, YAML_UTF8_ENCODING , YAML_LN_BREAK ); - break; - default: - $content .= 'Config, true) . ';'; - $content = preg_replace('/array\s+\(/', '[', $content); - $content = preg_replace('/,(\s+)\)/', '$1]', $content); - break; + foreach($this->Config as $section => $array) + { + is_array($array) or $array = array($array); + $content .= '[' . $section . ']'.PHP_EOL; + foreach( $array as $key => $value ) + $content .= $key.' = '.$value.PHP_EOL; + $content .= PHP_EOL; + } + break; + case 'YML': + $content .= yaml_emit ($this->Config, YAML_UTF8_ENCODING , YAML_LN_BREAK ); + break; + default: + $content .= 'Config, true) . ';'; + $content = preg_replace('/array\s+\(/', '[', $content); + $content = preg_replace('/,(\s+)\)/', '$1]', $content); + break; + } + file_put_contents($this->targetFile, $content, LOCK_EX); } - file_put_contents($this->targetFile, $content, LOCK_EX); - @chmod($this->targetFile,0644); + catch (Exception $e) + { trigger_error($e->getMessage(),E_USER_ERROR);} return true; } /** * fin de la classe */ - } diff --git a/README.md b/README.md index cce24f0..76f189c 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Iriven PHP ConfigManager ======= [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XDCFPNTKUC4TU) [![Build Status](https://travis-ci.org/iriven/ConfigManager.svg?branch=master)](https://travis-ci.org/iriven/ConfigManager) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/iriven/ConfigManager/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/iriven/ConfigManager/?branch=master) +[![Code Intelligence Status](https://scrutinizer-ci.com/g/iriven/ConfigManager/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence) >Iriven Php ConfigManager est un composant / package stand alone, developpé pour manipuler facilement les fichiers de configurations de toute application PHP. diff --git a/exemple.php b/exemple.php index 6deb2e6..b807cb8 100644 --- a/exemple.php +++ b/exemple.php @@ -14,7 +14,7 @@ 'salt'=>pack('H*', md5(time())), 'lastvalidation' => 98745612 // default: time(), execution du script pour la 1ere fois ); -$ini = new IrivenConfigManager('./licence.ini'); +$ini = new Iriven\ConfigManager('./licence.ini'); $ini->set('licence',$licence); //with section $test = array('test1'=>'my tester1','test2'=>'my tester2','test3'=>'my tester3'); $ini->set($test); //no section