/** * Fusionne un ensemble de fichiers de configuration en un seul grand tableau * * @param PHPArray[] $aConfigSources Les fichiers de configuration * @param int $cMode Le mode de fusion * @throws \Exception */ public function __construct($aConfigSources, $cMode = self::MODE_ARRAY_MERGE_RECURSIVE_SIMPLE) { $sUid = "/"; foreach ($aConfigSources as $oConfigSource) { $sUid .= $oConfigSource->getUid() . "/"; } if (!isset(self::$_aConfs[$sUid])) { self::$_aConfs[$sUid] = []; foreach ($aConfigSources as $oConfigSource) { switch ($cMode) { case self::MODE_ARRAY_MERGE_RECURSIVE_SIMPLE: self::$_aConfs[$sUid] = $this->array_merge_recursive_simple(self::$_aConfs[$sUid], $oConfigSource->getValue()); break; case self::MODE_ARRAY_MERGE_RECURSIVE: self::$_aConfs[$sUid] = array_merge_recursive(self::$_aConfs[$sUid], $oConfigSource->getValue()); break; case self::MODE_ARRAY_MERGE: self::$_aConfs[$sUid] = array_merge(self::$_aConfs[$sUid], $oConfigSource->getValue()); break; default: throw new \Exception("Mode de fusion inconnu : {$cMode}"); } } } parent::__construct($sUid, self::$_aConfs[$sUid]); }
/** * Parse un fichier JSON * * @param string $sFilePath Le chemin long du fichier JSON * @throws Exception */ public function __construct($sFilePath) { if (!isset(self::$_aConf[$sFilePath])) { if (is_file($sFilePath) && file_exists($sFilePath)) { $sJson = file_get_contents($sFilePath); self::$_aConf[$sFilePath] = json_decode($sJson, true); } else { throw new Exception("Le fichier de configuration {$sFilePath} n'est pas présent."); } } parent::__construct($sFilePath, self::$_aConf[$sFilePath]); }
/** * Parse un fichier JSON * * @param string $sFilePath Le chemin long du fichier JSON * @throws Exception */ public function __construct($sFilePath) { if (!isset(self::$_aConf[$sFilePath])) { if (is_file($sFilePath) && file_exists($sFilePath)) { global $CONF; include_once $sFilePath; self::$_aConf[$sFilePath] = $CONF; } else { throw new Exception("Le fichier de configuration {$sFilePath} n'est pas présent."); } } parent::__construct($sFilePath, self::$_aConf[$sFilePath]); }
/** * Parse un fichier YAML * * @param string $sFilePath Le chemin long du fichier YAML * @param callable $onchange Une fonction à exécuter si le fichier est modifié (utile pour supprimer le cache) * @throws Exception */ public function __construct($sFilePath, $onchange = null) { if (!isset(self::$_aConf[$sFilePath])) { if (is_file($sFilePath) && file_exists($sFilePath)) { $oCache = new Cache(); $iLastModTimestamp = filemtime($sFilePath); // On doit recharger le fichier yaml s'il n'a jamais été chargé ou si son contenu a changé depuis son dernier chargement $bYamlMustBeLoaded = !$oCache->get("LastLoaded_{$sFilePath}") || !$oCache->get("Yaml_{$sFilePath}") || $oCache->get("LastLoaded_{$sFilePath}") != $iLastModTimestamp; if ($bYamlMustBeLoaded) { self::$_aConf[$sFilePath] = SymfonyYaml::parse(file_get_contents($sFilePath)); $oCache->set("Yaml_{$sFilePath}", self::$_aConf[$sFilePath]); $oCache->set("LastLoaded_{$sFilePath}", $iLastModTimestamp); if ($onchange !== null) { $onchange(); } } else { self::$_aConf[$sFilePath] = $oCache->get("Yaml_{$sFilePath}"); } } else { throw new Exception("Le fichier de configuration {$sFilePath} n'est pas présent."); } } parent::__construct($sFilePath, self::$_aConf[$sFilePath]); }