Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -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: { }
147 changes: 77 additions & 70 deletions ConfigManager.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
<?php
namespace Iriven;
use Exception;

/**
* Created by PhpStorm.
* User: IRIVEN FRANCE
* Date: 21/11/2015
* Time: 10:44
*/
use Exception;

class ConfigManager
{
/**
* Chemin complet du fichier de configuration
*/
* Chemin complet du fichier de configuration
*/
private $targetFile;
private $Options = [];
/**
* tableau multidimensionnel contenant les donnée de configuration, initialement
* chargées depuis le fichier
*/
* tableau multidimensionnel contenant les donnée de configuration, initialement
* chargées depuis le fichier
*/
private $Config = [];
/**
* processeurs de fichier pris en charge par l'application
*/
* processeurs de fichier pris en charge par l'application
*/
private $availableDrivers = array('JSON', 'PHP','INI','YML');

private $defaultSection = 'runtime';

/**
* ConfigManager constructor.
* @param $filename
* @param null $location
* @throws \Exception
* @param $location
*/
public function __construct( $filename, $location=null )
{
Expand All @@ -40,16 +41,17 @@ public function __construct( $filename, $location=null )
throw new Exception('SETUP ERROR: configuration manager can accept only up to 2 parameters,'.$numargs.' given!');
$this->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)
Expand Down Expand Up @@ -79,24 +81,23 @@ 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();
switch($numarg)
{
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]));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -171,7 +172,7 @@ public function del($section, $item=null)
if($sectionSize>1) unset($this->Config[$section][$item]);
else unset($this->Config[$section]);
}
}
}
}
else
{
Expand All @@ -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)))
Expand All @@ -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);
Expand All @@ -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))
Expand All @@ -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 .= '<?php'.PHP_EOL;
$content .= 'return ';
$content .= var_export($this->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 .= '<?php'.PHP_EOL;
$content .= 'return ';
$content .= var_export($this->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
*/

}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion exemple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down