| // +---------------------------------------------------------------------------+ // $Id$ /** * Handles reading and writing of config files. * * @package SGL * @author Demian Turner * @version $Revision: 1.49 $ */ class SGL_ParamHandler { var $source; function SGL_ParamHandler($source) { $this->source = $source; } function &singleton($source) { static $instances; if (!isset($instances)) { $instances = array(); } $signature = md5($source); if (!isset($instances[$signature])) { $ext = substr($source, -3); switch ($ext) { case 'xml': $ret = new SGL_ParamHandler_Xml($source); break; case 'php': $ret = new SGL_ParamHandler_Array($source); break; // at the moment .php file are ini files // but they should be arrays case 'ini': $ret = new SGL_ParamHandler_Ini($source); break; } $instances[$signature] = $ret; } return $instances[$signature]; } function read() {} function write() {} // function getAll() // { // if (empty($this->aParams)) { // $this->read(); // } // return $this->aParams; // } } /** * Concrete implementation for ini files. * * @package SGL * @author Demian Turner */ class SGL_ParamHandler_Ini extends SGL_ParamHandler { function read() { $ret = @parse_ini_file($this->source, true); return (count($ret)) ? $ret : false; } function write($data) { // load PEAR::Config require_once 'Config.php'; $c = new Config(); $c->parseConfig($data, 'phparray'); $ok = $c->writeConfig($this->source, 'inifile'); return $ok; } } /** * Concrete implementation for arrays. * * @package SGL * @author Demian Turner */ class SGL_ParamHandler_Array extends SGL_ParamHandler { function read() { if (is_file($this->source)) { $ok = require $this->source; if ($ok) { $ret = $conf; } else { $ret = $ok; } } else { $ret = false; } return $ret; } function write($data) { // load PEAR::Config require_once 'Config.php'; $c = new Config(); $c->parseConfig($data, 'phparray'); $ok = $c->writeConfig($this->source, 'phparray'); return $ok; } } /** * Concrete implementation for XML files. * * @package SGL * @author Demian Turner */ class SGL_ParamHandler_Xml extends SGL_ParamHandler { function read() { return simplexml_load_file($this->source); } function write($data) { // load PEAR::Config require_once 'Config.php'; $c = new Config(); $c->parseConfig($data, 'phparray'); $ok = $c->writeConfig($this->source, 'xml'); return $ok; } } ?>