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
[MIX10] KEYNOTE DEUXIèME JOURNéE - INTERNET EXPLORER 9, HTML5, VISUAL STUDIO 2010, ODATA[MIX10] KEYNOTE DEUXIèME JOURNéE - INTERNET EXPLORER 9, HTML5, VISUAL STUDIO 2010, ODATA par cyril
Le deuxième keynote du mix fut très riche en contenu. Internet Explorer 9 Juste un après le lancement de Internet Explorer 8, Microsoft a dévoilé les nouveautés de Internet Explorer 9. Désormais, IE supportera HTML5, SVG et CSS3. L'élément ...
Cliquez pour lire la suite de l'article par cyril CERTIFICATIONS BETA .NET 4CERTIFICATIONS BETA .NET 4 par KooKiz
Les inscriptions pour les certifications beta .NET 4 ont commencé. L'inscription est offerte pour les examens suivants : - 71-511, TS: Windows Applications Development with Microsoft .NET Framework 4 - 71-515, TS: Web Applications Development with...
Cliquez pour lire la suite de l'article par KooKiz [MIX 2010] - MICROSOFT TRANSLATOR TECHNOLOGY PREVIEW V2[MIX 2010] - MICROSOFT TRANSLATOR TECHNOLOGY PREVIEW V2 par redo
J'imagine que la plupart d'entre vous connaissent bien et utilisent le service de traduction de Google, mais connaissez-vous celui de Microsoft . Microsoft Translator ? Effectivement, Microsoft nous annoncé le lancement version 2 de la Technologie Preview...
Cliquez pour lire la suite de l'article par redo LANCEMENT EN PREVIEW DE CYCLONE LORS DES TECHDAYS 2010!LANCEMENT EN PREVIEW DE CYCLONE LORS DES TECHDAYS 2010! par MPOWARE
Toutes les vidéos de ce lancement sont en ligne!
Partie I - Intro
http://www.youtube.com/watch?v=LkQzTQ8T6CA
Partie II - Démo 1
http://www.youtube.com/watch?v=drAhYQ7lqvo
Partie III - Démo 2
http://www.youtube.com/watch?v=c8KM_1Gqybc...
Cliquez pour lire la suite de l'article par MPOWARE [WP7] JE NE VEUX PAS D'UN NOUVEL IPHONE[WP7] JE NE VEUX PAS D'UN NOUVEL IPHONE par FREMYCOMPANY
Je pense qu'ils ont besoin d'une piqure de rappel chez Microsoft : c'est bien gentil d'avoir une interface jolie, mais si c'est pour avoir un truc qui ne convainct pas dedans, c'est peine perdue.
---->
Système ouvert ----> Fermé ?
P...
Cliquez pour lire la suite de l'article par FREMYCOMPANY
Logiciels
Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods Konvertor (4.00)KONVERTOR (4.00)Le logiciel est un gestionnaire multimedia affichant, jouant et convertissant plus de 2000 format... Cliquez pour télécharger Konvertor
|