Accueil > > > [PHP5.3]PACKAGE CONFIG
[PHP5.3]PACKAGE CONFIG
Information sur la source
Description
[ATTENTION SOURCE EN PHP 5.3] Package de gestion des fichiers de config. Plusieurs méthodes pour charger les fichiers : Méthode load : charge un seule fichier de config et retourne un objet ConfigManager Méthode loadAllFile : charge tous les fichiers du répertoire et retourne un objet ConfigManager Grace a la classe MapIterator vous pouvez acceder aux variables de config de la facon suivant : $oConfig->bdd->hostname
Source
- <?php
- namespace System\Iterator;
-
- /**
- * @abstract
- * @name MapIterator
- * @author aberthelot
- * @since 05/12/2008
- * @package System::Iterator
- * @version 4.0.0 - AXB - 05/12/2008
- */
- abstract class MapIterator implements Iterator, Countable, ArrayAccess
- {
- /**
- * Tableau MAP
- *
- * @access protected
- * @var array
- */
- protected $_aMap;
-
- /**
- * Nombre d'élément du tableau
- *
- * @access private
- * @var integer
- */
- private $_iCount;
-
- /**
- * Index de l'itérateur
- *
- * @access private
- * @var integer
- */
- private $_iIndex;
-
- /**
- * Défini si on peut modifier les éléments du tableau
- *
- * @access private
- * @var boolean
- */
- private $_bWritable;
-
- /**
- * Constructeur de la classe
- * Initialise le tableau et le compteur
- *
- * @access public
- * @param array $aMap
- * @param boolean $bWritable
- * @return void
- */
- public function __construct(array $aMap = array(), $bWritable = true)
- {
- $this->_iIndex = 0;
- $this->_aMap = array();
- $this->_bWritable = $bWritable;
-
- if( count($aMap) > 0 )
- {
- $this->add($aMap);
- }
-
- $this->_iCount = count($this->_aMap);
- }
-
- /**
- * Retourne le nombre d'élément du tableau
- *
- * @access public
- * @return integer
- */
- public function count()
- {
- return $this->_iCount;
- }
-
- /**
- * Retourne l'élément suivant
- *
- * @access public
- * @return mixed
- */
- public function current()
- {
- return current($this->_aMap);
- }
-
- /**
- * Retourne la clé de l'élément courant
- *
- * @access public
- * @return mixed
- */
- public function key()
- {
- return key($this->_aMap);
- }
-
- /**
- * Passe le pointeur sur l'élément suivant
- *
- * @access public
- * @return void
- */
- public function next()
- {
- next($this->_aMap);
- $this->_iIndex++;
- }
-
- /**
- * Met le pointeur au début du tableau
- *
- * @access public
- * @return void
- */
- public function rewind()
- {
- reset($this->_aMap);
- $this->_iIndex = 0;
- }
-
- /**
- * Test si l'iterateur est valide
- *
- * @access public
- * @return boolean
- */
- public function valid()
- {
- return $this->_iIndex < $this->_iCount;
- }
-
- /**
- * Test l'existance de l'élément dans le tableau
- *
- * @access public
- * @param mixed $mOffset
- * @return boolean
- */
- public function offsetExists($mOffset)
- {
- return isset($this->_aMap[$mOffset]);
- }
-
- /**
- * Retourne un élément du tableau
- *
- * @param mixed $mOffset
- * @return mixed
- */
- public function offsetGet($mOffset)
- {
- return $this->get($mOffset);
- }
-
- /**
- * Modifie un élément du tableau
- *
- * @access public
- * @param mixed $mOffset
- * @param mixed $mValue
- * @return void
- */
- public function offsetSet($mOffset, $mValue)
- {
- $this->set($mOffset, $mValue);
- }
-
- /**
- * Efface un élément du tableau
- *
- * @access public
- * @param mixed $mKey
- * @return void
- */
- public function offsetUnset($mOffset)
- {
- return $this->remove($mOffset);
- }
-
- /**
- * Ajoute un tableau
- *
- * @access public
- * @param array $aMap
- * @return void
- */
- public function add(array $aMap)
- {
- $sClass = get_called_class();
-
- foreach( $aMap as $mKey => $mValue )
- {
- if( is_array($mValue) )
- {
- $this->_aMap[$mKey] = new $sClass($mValue);
- }
- else
- {
- $this->_aMap[$mKey] = $mValue;
- }
- }
-
- $this->_iCount = count($this->_aMap);
- }
-
- /**
- * Retourne un élément du tableau
- *
- * @access public
- * @param mixed $mKey
- * @return mixed
- */
- public function get($mKey)
- {
- if( isset($this->_aMap[$mKey]) )
- {
- return $this->_aMap[$mKey];
- }
-
- return null;
- }
-
- /**
- * Ajoute un élément au tableau
- *
- * @access public
- * @param mixed $mKey
- * @param mixed $mValue
- * @return void
- */
- public function set($mKey, $mValue)
- {
- if( false === $this->_bWritable )
- {
- throw new Exception('Vous n\'avez pas les droits d\'écriture.', E_ERROR);
- }
-
- if( is_array($mValue) )
- {
- $sClass = get_called_class();
- $this->_aMap[$mKey] = new $sClass($mValue);
- }
- else
- {
- $this->_aMap[$mKey] = $mValue;
- }
-
- $this->_iCount = count($this->_aMap);
- }
-
- /**
- * Retourne un tableau contenant les clé
- *
- * @access public
- * @return array
- */
- public function getKeys()
- {
- return array_keys($this->_aMap);
- }
-
- /**
- * Combine une autre instance de la class
- *
- * @access public
- * @param object $merge
- * @return object
- */
- public function merge($oMerge)
- {
- $sClass = get_called_class();
- $oReflexion = new ReflectionClass($sClass);
-
- if(! $oReflexion->isSubclassOf($this) )
- {
- throw new Exception('Vous devez passer en paramètre une classe enfant de MapIterator.', E_ERROR);
- }
-
- foreach($oMerge as $mKey => $mItem)
- {
- if(isset($this->_aMap[$mKey]))
- {
- if($mItem instanceof $sClass && $this->$mKey instanceof $sClass)
- {
- $this->$mKey = $this->$mKey->merge($mItem);
- }
- else
- {
- $this->$mKey = $mItem;
- }
- }
- else
- {
- $this->$mKey = $mItem;
- }
- }
-
- return $this;
- }
-
- /**
- * Efface un élément du tableau
- *
- * @access public
- * @param mixed $mKey
- * @return object|boolean
- */
- public function remove($mKey)
- {
- if( false === $this->_bWritable )
- {
- throw new Exception('Vous n\'avez pas les droits d\'écriture.', E_ERROR);
- }
-
- if( isset($this->_aMap[$mKey]) )
- {
- unset($this->_aMap[$mKey]);
- $this->_iCount = count($this->_aMap);
-
- return $this;
- }
-
- return false;
- }
-
- /**
- * Transforme l'object en tableau
- *
- * @access public
- * @return array
- */
- public function __toArray()
- {
- $aArray = array();
-
- foreach( $this->_aMap as $mKey => $mValue )
- {
- if( $mValue instanceof MapIterator )
- {
- $aArray[$mKey] = $mValue->__toArray();
- }
- else
- {
- $aArray[$mKey] = $mValue;
- }
- }
-
- return $aArray;
- }
-
- /**
- * Test si l'élément existe
- *
- * @access public
- * @param mixed $mKey
- * @return boolean
- */
- public function __isset($mKey)
- {
- return isset($this->_aMap[$mKey]);
- }
-
- /**
- * Efface un élément du tableau
- *
- * @access public
- * @param mixed $mKey
- * @return object
- */
- public function __unset($mKey)
- {
- if( false === $this->_bWritable )
- {
- throw new Exception('Vous n\'avez pas les droits d\'écriture.', E_ERROR);
- }
-
- return $this->remove($mKey);
- }
-
- /**
- * Retourne un élément du tableau
- *
- * @access public
- * @param mixed $mKey
- * @return mixed
- */
- abstract public function __get($mKey);
-
- /**
- * Ajoute un élément au tableau
- *
- * @access public
- * @param mixed $mKey
- * @param mixed $mValue
- * @return void
- */
- abstract public function __set($mKey, $mValue);
- }
- ?>
-
- <?php
- namespace System\Config;
- use System\Iterator\MapIterator;
- use System\Config\Reader\IConfig;
-
- /**
- * @name ConfigManager
- * @author aberthelot
- * @since 07/12/2008
- * @package System::Config
- * @version 4.0.0 - AXB - 07/12/2008
- */
- class ConfigManager extends MapIterator
- {
- /**
- * Constructeur de la classe
- *
- * @access public
- * @param array $aConfig
- * @param boolean $bWritable
- * @return void
- */
- public function __construct(array $aConfig = array(), $bWritable = false)
- {
- parent::__construct($aConfig, $bWritable);
- }
-
- /**
- * Modifie un élément de la config
- *
- * @access public
- * @param mixed $mKey
- * @param mixed $mValue
- * @return object
- */
- public function __set($mKey, $mValue)
- {
- $this->set($mKey, $mValue);
- }
-
- /**
- * Retourne un élément de la configuration
- *
- * @access public
- * @param mixed $mKey
- * @return mixed var
- */
- public function __get($mKey)
- {
- return $this->get($mKey);
- }
-
- /**
- * Charge tous les fichiers d'un répertoire
- *
- * @static
- * @access public
- * @param string $sDirectory
- * @param boolean $bWritable
- * @param string $sType
- * @param string $sSection
- * @return object
- */
- public static function loadAllFile($sDirectory, $bWritable = false, $sType = 'yaml', array $aException = array())
- {
- $sClass = 'System\\Config\\Reader\\Config'.ucfirst($sType);
- $oReflection = new ReflectionClass($sClass);
-
- if(! $oReflection->implementsInterface('System\\Config\\Reader\\IConfig') )
- {
- throw new InvalidArgumentException('The "'.$sClass.'" class must implement the "IConfig" interface.');
- }
-
- $oReader = new $sClass();
- $aConfig = self::recursiveDir($sDirectory, $oReader, $aException);
- $oConfig = new ConfigManager($aConfig, $bWritable);
-
- return $oConfig;
- }
-
- /**
- * Charge un fichier de config
- *
- * @static
- * @access public
- * @param string $sDirectory
- * @param boolean $bWritable
- * @param string $sType
- * @param string $sSection
- * @return object
- */
- public static function load($sFile, $bWritable = false, $sType = 'yaml', $sSection = null)
- {
- $sClass = 'System\\Config\\Reader\\Config'.ucfirst($sType);
- $oReflection = new ReflectionClass($sClass);
-
- if(! $oReflection->implementsInterface('System\\Config\\Reader\\IConfig') )
- {
- throw new InvalidArgumentException('The "'.$sClass.'" class must implement the "IConfig" interface.');
- }
-
- $oReader = new $sClass();
- $oConfig = new ConfigManager($oReader->load($sFile, $sSection), $bWritable);
- return $oConfig;
- }
-
- /**
- * Parcours le répertoire de façon récursive
- *
- * @static
- * @access private
- * @param string $sDirectory
- * @param object $oReader
- * @param array $aException
- * @return array
- */
- private static function recursiveDir($sDirectory, $oReader, $aException)
- {
- if( false === ($sFolder = @opendir($sDirectory)) )
- {
- throw new Exception('Erreur lors de l\'ouverture du dossier "'.$sDirectory.'".', E_ERROR);
- }
-
- $aConfig = array();
- $aFilter = array_merge(array('.', '..', '...'), $aException);
-
- while( false !== ($sFile = readdir($sFolder)) )
- {
- if( is_dir($sDirectory.$sFile) )
- {
- if( !in_array($sFile, $aFilter) )
- {
- $aConfig = array_merge($aConfig, self::recursiveDir($sDirectory.$sFile.'/', $oReader, $aException));
- }
- }
- else
- {
- if( !in_array($sFile, $aFilter) && !is_dir($sFile) )
- {
- $aConfig = array_merge($aConfig, $oReader->load($sDirectory.$sFile));
- }
- }
- }
-
- closedir($sFolder);
- return $aConfig;
- }
- }
- ?>
-
-
- <?php
- namespace System\Config\Reader;
- use System\Config\Reader\IConfig;
- use Plugin\Spy\Spyc;
-
- /**
- * @name ConfigYaml
- * @author aberthelot
- * @since 07/12/2008
- * @package System::Config
- * @version 4.0.0 - AXB - 07/12/2008
- */
- class ConfigYaml implements IConfig
- {
- /**
- * Retourne le contenu du fichier de configuration sous forme de tableau
- * Possibilité de récupérer qu'une partie du fichier avec l'option $sSection
- *
- * @access public
- * @param string $sPathFile
- * @param string $sSection
- * @return array
- */
- public function load($sPathFile, $sSection = null)
- {
- if(! file_exists($sPathFile))
- {
- throw new Exception('Le fichier "'.basename($sPathFile).'" est inexistant.', E_ERROR);
- }
-
- if (! is_readable($sPathFile))
- {
- throw new Exception('Vous n\'avez pas les droits de lecture sur le fichier "'.basename($sPathFile).'".', E_ERROR);
- }
-
- $aConfig = Spyc::YAMLLoad($sPathFile);
-
- if(! is_null($sSection) )
- {
- if( isset($aConfig[$sSection]) )
- {
- return $aConfig[$sSection];
- }
- }
-
- return $aConfig;
- }
- }
- ?>
-
-
- <?php
- namespace System\Config\Reader;
- use System\Config\Reader\IConfig;
-
- /**
- * @name ConfigXml
- * @author aberthelot
- * @since 07/12/2008
- * @package System::Config
- * @version 4.0.0 - AXB - 07/12/2008
- */
- class ConfigXml implements IConfig
- {
- /**
- * Retourne le contenu du fichier de configuration sous forme de tableau
- * Possibilité de récupérer qu'une partie du fichier avec l'option $sSection
- *
- * @access public
- * @param string $sPathFile
- * @param string $sSection
- * @return array
- */
- public function load($sPathFile, $sSection = null)
- {
- if(! file_exists($sPathFile))
- {
- throw new Exception('Le fichier "'.basename($sPathFile).'" est inexistant.', E_ERROR);
- }
-
- if (! is_readable($sPathFile))
- {
- throw new Exception('Vous n\'avez pas les droits de lecture sur le fichier "'.basename($sPathFile).'".', E_ERROR);
- }
-
- $oXml = simplexml_load_file($sPathFile);
-
- if(! is_null($sSection) )
- {
- $aConfig = $this->recursive($oXml->$sSection);
- }
- else
- {
- $aConfig = $this->recursive($oXml);
- }
-
- return $aConfig;
- }
-
- /**
- * Affectation des valeurs xml dans un tableau
- *
- * @access private
- * @param object $aData
- * @return array $aDataTmp
- */
- private function recursive(SimpleXMLElement $oConfig)
- {
- foreach( $oConfig as $mKey => $mValue )
- {
- if( is_array($value) )
- {
- $aConfigTmp[$mKey] = $this->recursive($mValue);
- }
- else
- {
- $aConfigTmp[$mKey] = $mValue;
- }
- }
-
- return $aConfigTmp;
- }
- }
- ?>
-
-
- <?php
- namespace System\Config\Reader;
- use System\Config\Reader\IConfig;
-
- /**
- * @name ConfigIni
- * @author aberthelot
- * @since 07/12/2008
- * @package System::Config
- * @version 4.0.0 - AXB - 07/12/2008
- */
- class ConfigIni implements IConfig
- {
- /**
- * Retourne le contenu du fichier de configuration sous forme de tableau
- * Possibilité de récupérer qu'une partie du fichier avec l'option $sSection
- *
- * @access public
- * @param string $sPathFile
- * @param string $sSection
- * @return array
- */
- public static function load($sPathFile, $sSection = null)
- {
- if(! file_exists($sPathFile))
- {
- throw new Exception('Le fichier "'.basename($sPathFile).'" est inexistant.', E_ERROR);
- }
-
- if (! is_readable($sPathFile))
- {
- throw new Exception('Vous n\'avez pas les droits de lecture sur le fichier "'.basename($sPathFile).'".', E_ERROR);
- }
-
- $aConfig = parse_ini_file($sPathFile, true);
-
- if(! is_null($sSection) )
- {
- if( isset($aConfig[$sSection]) )
- {
- return $aConfig[$sSection];
- }
- }
-
- return $aConfig;
- }
- }
- ?>
-
-
- ##### EXEMPLE D UTILISATION #####
- $oConfig = System\Config\ConfigManager::load('./config/config.yml');
- print $oConfig->config1->test1;
<?php
namespace System\Iterator;
/**
* @abstract
* @name MapIterator
* @author aberthelot
* @since 05/12/2008
* @package System::Iterator
* @version 4.0.0 - AXB - 05/12/2008
*/
abstract class MapIterator implements Iterator, Countable, ArrayAccess
{
/**
* Tableau MAP
*
* @access protected
* @var array
*/
protected $_aMap;
/**
* Nombre d'élément du tableau
*
* @access private
* @var integer
*/
private $_iCount;
/**
* Index de l'itérateur
*
* @access private
* @var integer
*/
private $_iIndex;
/**
* Défini si on peut modifier les éléments du tableau
*
* @access private
* @var boolean
*/
private $_bWritable;
/**
* Constructeur de la classe
* Initialise le tableau et le compteur
*
* @access public
* @param array $aMap
* @param boolean $bWritable
* @return void
*/
public function __construct(array $aMap = array(), $bWritable = true)
{
$this->_iIndex = 0;
$this->_aMap = array();
$this->_bWritable = $bWritable;
if( count($aMap) > 0 )
{
$this->add($aMap);
}
$this->_iCount = count($this->_aMap);
}
/**
* Retourne le nombre d'élément du tableau
*
* @access public
* @return integer
*/
public function count()
{
return $this->_iCount;
}
/**
* Retourne l'élément suivant
*
* @access public
* @return mixed
*/
public function current()
{
return current($this->_aMap);
}
/**
* Retourne la clé de l'élément courant
*
* @access public
* @return mixed
*/
public function key()
{
return key($this->_aMap);
}
/**
* Passe le pointeur sur l'élément suivant
*
* @access public
* @return void
*/
public function next()
{
next($this->_aMap);
$this->_iIndex++;
}
/**
* Met le pointeur au début du tableau
*
* @access public
* @return void
*/
public function rewind()
{
reset($this->_aMap);
$this->_iIndex = 0;
}
/**
* Test si l'iterateur est valide
*
* @access public
* @return boolean
*/
public function valid()
{
return $this->_iIndex < $this->_iCount;
}
/**
* Test l'existance de l'élément dans le tableau
*
* @access public
* @param mixed $mOffset
* @return boolean
*/
public function offsetExists($mOffset)
{
return isset($this->_aMap[$mOffset]);
}
/**
* Retourne un élément du tableau
*
* @param mixed $mOffset
* @return mixed
*/
public function offsetGet($mOffset)
{
return $this->get($mOffset);
}
/**
* Modifie un élément du tableau
*
* @access public
* @param mixed $mOffset
* @param mixed $mValue
* @return void
*/
public function offsetSet($mOffset, $mValue)
{
$this->set($mOffset, $mValue);
}
/**
* Efface un élément du tableau
*
* @access public
* @param mixed $mKey
* @return void
*/
public function offsetUnset($mOffset)
{
return $this->remove($mOffset);
}
/**
* Ajoute un tableau
*
* @access public
* @param array $aMap
* @return void
*/
public function add(array $aMap)
{
$sClass = get_called_class();
foreach( $aMap as $mKey => $mValue )
{
if( is_array($mValue) )
{
$this->_aMap[$mKey] = new $sClass($mValue);
}
else
{
$this->_aMap[$mKey] = $mValue;
}
}
$this->_iCount = count($this->_aMap);
}
/**
* Retourne un élément du tableau
*
* @access public
* @param mixed $mKey
* @return mixed
*/
public function get($mKey)
{
if( isset($this->_aMap[$mKey]) )
{
return $this->_aMap[$mKey];
}
return null;
}
/**
* Ajoute un élément au tableau
*
* @access public
* @param mixed $mKey
* @param mixed $mValue
* @return void
*/
public function set($mKey, $mValue)
{
if( false === $this->_bWritable )
{
throw new Exception('Vous n\'avez pas les droits d\'écriture.', E_ERROR);
}
if( is_array($mValue) )
{
$sClass = get_called_class();
$this->_aMap[$mKey] = new $sClass($mValue);
}
else
{
$this->_aMap[$mKey] = $mValue;
}
$this->_iCount = count($this->_aMap);
}
/**
* Retourne un tableau contenant les clé
*
* @access public
* @return array
*/
public function getKeys()
{
return array_keys($this->_aMap);
}
/**
* Combine une autre instance de la class
*
* @access public
* @param object $merge
* @return object
*/
public function merge($oMerge)
{
$sClass = get_called_class();
$oReflexion = new ReflectionClass($sClass);
if(! $oReflexion->isSubclassOf($this) )
{
throw new Exception('Vous devez passer en paramètre une classe enfant de MapIterator.', E_ERROR);
}
foreach($oMerge as $mKey => $mItem)
{
if(isset($this->_aMap[$mKey]))
{
if($mItem instanceof $sClass && $this->$mKey instanceof $sClass)
{
$this->$mKey = $this->$mKey->merge($mItem);
}
else
{
$this->$mKey = $mItem;
}
}
else
{
$this->$mKey = $mItem;
}
}
return $this;
}
/**
* Efface un élément du tableau
*
* @access public
* @param mixed $mKey
* @return object|boolean
*/
public function remove($mKey)
{
if( false === $this->_bWritable )
{
throw new Exception('Vous n\'avez pas les droits d\'écriture.', E_ERROR);
}
if( isset($this->_aMap[$mKey]) )
{
unset($this->_aMap[$mKey]);
$this->_iCount = count($this->_aMap);
return $this;
}
return false;
}
/**
* Transforme l'object en tableau
*
* @access public
* @return array
*/
public function __toArray()
{
$aArray = array();
foreach( $this->_aMap as $mKey => $mValue )
{
if( $mValue instanceof MapIterator )
{
$aArray[$mKey] = $mValue->__toArray();
}
else
{
$aArray[$mKey] = $mValue;
}
}
return $aArray;
}
/**
* Test si l'élément existe
*
* @access public
* @param mixed $mKey
* @return boolean
*/
public function __isset($mKey)
{
return isset($this->_aMap[$mKey]);
}
/**
* Efface un élément du tableau
*
* @access public
* @param mixed $mKey
* @return object
*/
public function __unset($mKey)
{
if( false === $this->_bWritable )
{
throw new Exception('Vous n\'avez pas les droits d\'écriture.', E_ERROR);
}
return $this->remove($mKey);
}
/**
* Retourne un élément du tableau
*
* @access public
* @param mixed $mKey
* @return mixed
*/
abstract public function __get($mKey);
/**
* Ajoute un élément au tableau
*
* @access public
* @param mixed $mKey
* @param mixed $mValue
* @return void
*/
abstract public function __set($mKey, $mValue);
}
?>
<?php
namespace System\Config;
use System\Iterator\MapIterator;
use System\Config\Reader\IConfig;
/**
* @name ConfigManager
* @author aberthelot
* @since 07/12/2008
* @package System::Config
* @version 4.0.0 - AXB - 07/12/2008
*/
class ConfigManager extends MapIterator
{
/**
* Constructeur de la classe
*
* @access public
* @param array $aConfig
* @param boolean $bWritable
* @return void
*/
public function __construct(array $aConfig = array(), $bWritable = false)
{
parent::__construct($aConfig, $bWritable);
}
/**
* Modifie un élément de la config
*
* @access public
* @param mixed $mKey
* @param mixed $mValue
* @return object
*/
public function __set($mKey, $mValue)
{
$this->set($mKey, $mValue);
}
/**
* Retourne un élément de la configuration
*
* @access public
* @param mixed $mKey
* @return mixed var
*/
public function __get($mKey)
{
return $this->get($mKey);
}
/**
* Charge tous les fichiers d'un répertoire
*
* @static
* @access public
* @param string $sDirectory
* @param boolean $bWritable
* @param string $sType
* @param string $sSection
* @return object
*/
public static function loadAllFile($sDirectory, $bWritable = false, $sType = 'yaml', array $aException = array())
{
$sClass = 'System\\Config\\Reader\\Config'.ucfirst($sType);
$oReflection = new ReflectionClass($sClass);
if(! $oReflection->implementsInterface('System\\Config\\Reader\\IConfig') )
{
throw new InvalidArgumentException('The "'.$sClass.'" class must implement the "IConfig" interface.');
}
$oReader = new $sClass();
$aConfig = self::recursiveDir($sDirectory, $oReader, $aException);
$oConfig = new ConfigManager($aConfig, $bWritable);
return $oConfig;
}
/**
* Charge un fichier de config
*
* @static
* @access public
* @param string $sDirectory
* @param boolean $bWritable
* @param string $sType
* @param string $sSection
* @return object
*/
public static function load($sFile, $bWritable = false, $sType = 'yaml', $sSection = null)
{
$sClass = 'System\\Config\\Reader\\Config'.ucfirst($sType);
$oReflection = new ReflectionClass($sClass);
if(! $oReflection->implementsInterface('System\\Config\\Reader\\IConfig') )
{
throw new InvalidArgumentException('The "'.$sClass.'" class must implement the "IConfig" interface.');
}
$oReader = new $sClass();
$oConfig = new ConfigManager($oReader->load($sFile, $sSection), $bWritable);
return $oConfig;
}
/**
* Parcours le répertoire de façon récursive
*
* @static
* @access private
* @param string $sDirectory
* @param object $oReader
* @param array $aException
* @return array
*/
private static function recursiveDir($sDirectory, $oReader, $aException)
{
if( false === ($sFolder = @opendir($sDirectory)) )
{
throw new Exception('Erreur lors de l\'ouverture du dossier "'.$sDirectory.'".', E_ERROR);
}
$aConfig = array();
$aFilter = array_merge(array('.', '..', '...'), $aException);
while( false !== ($sFile = readdir($sFolder)) )
{
if( is_dir($sDirectory.$sFile) )
{
if( !in_array($sFile, $aFilter) )
{
$aConfig = array_merge($aConfig, self::recursiveDir($sDirectory.$sFile.'/', $oReader, $aException));
}
}
else
{
if( !in_array($sFile, $aFilter) && !is_dir($sFile) )
{
$aConfig = array_merge($aConfig, $oReader->load($sDirectory.$sFile));
}
}
}
closedir($sFolder);
return $aConfig;
}
}
?>
<?php
namespace System\Config\Reader;
use System\Config\Reader\IConfig;
use Plugin\Spy\Spyc;
/**
* @name ConfigYaml
* @author aberthelot
* @since 07/12/2008
* @package System::Config
* @version 4.0.0 - AXB - 07/12/2008
*/
class ConfigYaml implements IConfig
{
/**
* Retourne le contenu du fichier de configuration sous forme de tableau
* Possibilité de récupérer qu'une partie du fichier avec l'option $sSection
*
* @access public
* @param string $sPathFile
* @param string $sSection
* @return array
*/
public function load($sPathFile, $sSection = null)
{
if(! file_exists($sPathFile))
{
throw new Exception('Le fichier "'.basename($sPathFile).'" est inexistant.', E_ERROR);
}
if (! is_readable($sPathFile))
{
throw new Exception('Vous n\'avez pas les droits de lecture sur le fichier "'.basename($sPathFile).'".', E_ERROR);
}
$aConfig = Spyc::YAMLLoad($sPathFile);
if(! is_null($sSection) )
{
if( isset($aConfig[$sSection]) )
{
return $aConfig[$sSection];
}
}
return $aConfig;
}
}
?>
<?php
namespace System\Config\Reader;
use System\Config\Reader\IConfig;
/**
* @name ConfigXml
* @author aberthelot
* @since 07/12/2008
* @package System::Config
* @version 4.0.0 - AXB - 07/12/2008
*/
class ConfigXml implements IConfig
{
/**
* Retourne le contenu du fichier de configuration sous forme de tableau
* Possibilité de récupérer qu'une partie du fichier avec l'option $sSection
*
* @access public
* @param string $sPathFile
* @param string $sSection
* @return array
*/
public function load($sPathFile, $sSection = null)
{
if(! file_exists($sPathFile))
{
throw new Exception('Le fichier "'.basename($sPathFile).'" est inexistant.', E_ERROR);
}
if (! is_readable($sPathFile))
{
throw new Exception('Vous n\'avez pas les droits de lecture sur le fichier "'.basename($sPathFile).'".', E_ERROR);
}
$oXml = simplexml_load_file($sPathFile);
if(! is_null($sSection) )
{
$aConfig = $this->recursive($oXml->$sSection);
}
else
{
$aConfig = $this->recursive($oXml);
}
return $aConfig;
}
/**
* Affectation des valeurs xml dans un tableau
*
* @access private
* @param object $aData
* @return array $aDataTmp
*/
private function recursive(SimpleXMLElement $oConfig)
{
foreach( $oConfig as $mKey => $mValue )
{
if( is_array($value) )
{
$aConfigTmp[$mKey] = $this->recursive($mValue);
}
else
{
$aConfigTmp[$mKey] = $mValue;
}
}
return $aConfigTmp;
}
}
?>
<?php
namespace System\Config\Reader;
use System\Config\Reader\IConfig;
/**
* @name ConfigIni
* @author aberthelot
* @since 07/12/2008
* @package System::Config
* @version 4.0.0 - AXB - 07/12/2008
*/
class ConfigIni implements IConfig
{
/**
* Retourne le contenu du fichier de configuration sous forme de tableau
* Possibilité de récupérer qu'une partie du fichier avec l'option $sSection
*
* @access public
* @param string $sPathFile
* @param string $sSection
* @return array
*/
public static function load($sPathFile, $sSection = null)
{
if(! file_exists($sPathFile))
{
throw new Exception('Le fichier "'.basename($sPathFile).'" est inexistant.', E_ERROR);
}
if (! is_readable($sPathFile))
{
throw new Exception('Vous n\'avez pas les droits de lecture sur le fichier "'.basename($sPathFile).'".', E_ERROR);
}
$aConfig = parse_ini_file($sPathFile, true);
if(! is_null($sSection) )
{
if( isset($aConfig[$sSection]) )
{
return $aConfig[$sSection];
}
}
return $aConfig;
}
}
?>
##### EXEMPLE D UTILISATION #####
$oConfig = System\Config\ConfigManager::load('./config/config.yml');
print $oConfig->config1->test1;
Historique
- 08 février 2009 02:27:55 :
- .
- 08 février 2009 02:40:50 :
- ..
- 08 février 2009 19:25:56 :
- MAJ des namespaces
- 02 mars 2009 14:32:42 :
- .
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
MySql config ? [ par holger ]
J'auyrai souhaiter savoir si il était possible de fairetourner MySql sur un ordi tout pourrit :PC 200Mx 48Mo Ram Edo sous Windows 95Merci,Un lyonnais
config mi raisin [ par BigZoo ]
je vais être bref.Je débute en php et ai un petit boulot d'automatisation de mise en page à faire sur un site acceptant ce langage.Pour travailler tra
Probleme de mise a jour de variables [ par Neozix ]
Salut,Voila mon prob les amis :J'ai fait un page de configuration-administration en php pour un petit site.J'ai donc ecrit un script qui fait appele a
Pour un serveur web, quel Os, quel config ? [ par Tomcube ]
Salut à tous,Je vois sur certains sites qui proposent des hébergements "Système d'exploitation : Si HTML > Windows, Si PHP, Linux" Pourquoi changen
config easyphp comme free [ par mogmog ]
Bonjour!Je voudrais savoir comment configurer easyphp pour avoir le meme environnement que chez FREE,@pour ne plus avoir de mauvaises surprises lors
ldap config slapd.conf et ldif [ par fmazoue ]
voila je comprend rien a la config de openldap !faut en fait configurer le slapd.conf et crere un .ldif !mais moi j'y arrive qu'une fois sur mille a l
connection phpmyadmin [ par salley ]
Bonjour tout le monde,Voila, je dois faire une sauvegarde d'une base de donnees sur un serveur.Comme la base de donnee n'est pas trop grande je veux u
Connexion Oracle9i [ par willinfeo ]
Bonjour à tous,j'essaye de me connecter à Oracle9i, mais j'ai le message d'erreur suivant :"PHP has encountered an Access Violation at 612A2217"Ma con
Class POO retourné le nom de l'objet [ par MeTh ]
Bonjour,Comment retourné le nom de l'objet déclaré?exemple :$monobjet = new GridR();comment recuperé $monobjet dans ma class?Merci
URL Rewriting & .htaccess [ par dorian53 ]
Bonjour,j'aimerais pouvoir tester l'URL Rewriting en local sur mon PC. Je suis sous ephp 1.7. Pour le moment j'ai laissé toutes les config par défaut,
|
Derniers Blogs
GESTION D'EXCEPTION AVEC LES TASKSGESTION D'EXCEPTION AVEC LES TASKS par richardc
Nous avons vu dans un précédent article comment utiliser Task pour effectuer des opérations dans un autre thread.
Malheureusement, comme tout le monde n'est pas parfait, il se peut que cette exécution se passe mal et qu'une exception se produise.
La...
Cliquez pour lire la suite de l'article par richardc DéMARRONS AVEC LES TASKSDéMARRONS AVEC LES TASKS par richardc
Que vous le vouliez ou non, le développement multi-tâche est maintenant une obligation pour toute nouvelle application. Il est donc vital d'en comprendre les mécanismes et de s'y mettre le plus tôt possible.
En attendant le .NET Framework 4.5 avec le...
Cliquez pour lire la suite de l'article par richardc SLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPSSLIDE & DéMO TECHDAYS 2012 - FAST & FURIOUS XAML APPS par Vko
Retrouvez les slides et les démo de ma session Fast & Furious XAML Apps. A ceux qui se posent la question : "est-ce que le code de la DataGrid est disponible?", je vous répondrais "pas encore". Je vais mettre en place un projet codeplex pour part...
Cliquez pour lire la suite de l'article par Vko XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
CHAMPS TIMECHAMPS TIME par vargas
Cliquez pour lire la suite par vargas
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|