function getOrCreate(Config_Container $cont, $name, $value = null) { $itemtype = is_null($value) ? 'section' : 'directive'; if ($child = $cont->searchPath(array($name))) { return $child; } return $cont->createItem($itemtype, $name, null); }
/** * Method to create initial IniFile config elements. This method will create * blank ini params. * @example * $config_container ="MAIL"/"Settings" * $settings = array("name"=>"Bruce Banner","email"=> "*****@*****.**") * $iniPath = "/config/" * $iniName ="my.ini" * @param string $config_container. This describes the main header section of the iniFile * @param array $settings. The values that need initializing * @param string $iniPath. File path * @param string $iniName. File name */ public function createConfig($config_container = false, $settings, $iniPath = false, $iniName) { try { //define the main header setting if (isset($config_container)) { $Section = new Config_Container("section", "Settings"); } else { $Section = new Config_Container("section", "{$config_container}"); } // create variables/values if (is_array($settings)) { foreach ($settings as $key => $value) { $Section->createDirective("{$key}", "{$value}"); } } // reassign root $this->objConf->setRoot($Section); // write configuration to file $result = $this->objConf->writeConfig("{$iniPath}" . "{$iniName}", "INIFile"); if ($result == false) { throw new customException($this->objLanguage->languageText("mod_userparamsadmin_writeerror", "userparamsadmin")); log_debug($this->objLanguage->languageText("mod_userparamsadmin_writeerror", "userparamsadmin")); } } catch (customException $e) { customException::cleanUp(); exit; } }
<?php /** * Config.php example * * Lots of different manipulations to show Config features. * * @author Bertrand Mansion <*****@*****.**> * @package Config */ // $Id: IniFromScratch.php,v 1.2 2003/03/21 18:04:21 mansion Exp $ require_once 'Config.php'; // Creates a PHPArray config with attributes, from scratch $dsn = array('type' => 'mysql', 'host' => 'localhost', 'user' => 'mamasam', 'pass' => 'foobar'); $c = new Config_Container('section', 'root'); $c->createComment('DB Config'); $db =& $c->createSection('DB', $dsn); $fields =& $db->createSection('fields'); $fields->createDirective('username', 'USERNAME', array('type' => 'varchar', 'size' => 32)); $fields->createDirective('password', 'PASSWD', array('type' => 'varchar', 'size' => 32)); $c->createBlank(); $c->createComment('Support config'); $c->createDirective('support', 'See my wishlist...'); echo '<pre>' . $c->toString('phparray') . '</pre>'; unset($c); // Parses and writes an existing php array $conf $conf['storage']['driver'] = 'sql'; $conf['storage']['params']['phptype'] = 'mysql'; $conf['storage']['params']['hostspec'] = 'localhost'; $conf['storage']['params']['username'] = '******'; $conf['storage']['params']['password'] = '******';
/** * Create config for adm * */ public function createAdmConfig($servarray) { //check for the directory structure if (!file_exists($this->objConfig->getcontentBasePath() . 'adm/')) { mkdir($this->objConfig->getcontentBasePath() . 'adm/', 0777); } // write the server list file $cfile = $this->objConfig->getcontentBasePath() . 'adm/adm.xml'; if (!file_exists($cfile)) { $conf = new Config_Container('section', 'adm'); //$servarray['name']); $conf_serv =& $conf->createSection($servarray['name']); $conf_serv->createDirective('servername', $servarray['name']); $conf_serv->createDirective('serverapiurl', $servarray['url']); $conf_serv->createDirective('serveremail', $servarray['email']); $conf_serv->createDirective('regtime', date('r')); $config = new Config(); $config->setRoot($conf); // write the container to an XML document $config->writeConfig($cfile, 'XML'); } else { // update the xml with the new server $root =& $config->parseConfig($cfile, 'XML', array('name' => 'adm')); log_debug($root); $conf_serv =& $root->createSection($servarray['name']); $conf_serv->createDirective('servername', $servarray['name']); $conf_serv->createDirective('serverapiurl', $servarray['url']); $conf_serv->createDirective('serveremail', $servarray['email']); $conf_serv->createDirective('regtime', date('r')); $config = new Config(); $config->setRoot($root); // write the container to an XML document $config->writeConfig($cfile, 'XML'); } }