PHP PEAR::Config管理配置信息

王朝php·作者佚名  2005-05-26
窄屏简体版  字體: |||超大  

PHP PEAR::Config管理配置信息

Config类可以熟练地管理你的配置文件,不论它是存放在XML文件,INI文件,PHP数组或其它的数据资源中,它有以下的特性:

1.解析不同的数据格式;

2.可以熟练地处理你所使用的sections, directives, comments, blanks

3.将配置信息重新保存到你所喜欢的格式中(XML文件,INI文件,PHP数组或其它的数据资源).

最顶层对象Config_Container

它的结构:

Section:Section下面还可以包括Config_Container对象;

Directive:不包含其它的对象,但有一对类似于key-value形式的数据;

Comment:

Blank:

可以设置的数据格式为:

xml

phparray

inifile

Apache conf(.conf)

假设我们要将员工的信息保存为如下形式

<?xml version="1.0" encoding="UTF-8"?>

<Company>

<Member>

<name>jxyuhua</name>

<email>jxyuhua at yahoo.com.cn</email>

<www>http://www.b2c-battery.co.uk/</www>

</Member>

</Company>

下面开始建立它

<?php

require_once('Config.php');

$conf['storage']['driver'] = 'sql';

$conf['storage']['params']['phptype'] = 'mysql';

$conf['storage']['params']['hostspec'] = 'localhost';

$conf['storage']['params']['username'] = 'mamasam';

$conf['storage']['params']['password'] = 'foobar';

$conf['Menu']['Laptop'] = array('ibm'=>'IBM', 'sony'=>'SONY');

$conf['Website']['UK']['b2c-battery'] = 'http://www.b2c-battery.co.uk';

$conf['Website']['UK']['global-batteries'] = 'http://www.global-batteries.co.uk';

$c = new Config();

$root =& $c->parseConfig($conf, 'phparray');

$storage =& $root->getItem('section', 'storage');

//先删除,然后增加,改变它出现的顺序

$storage->removeItem();

$root->addItem($storage);

//echo '<pre>'. htmlspecialchars($root->toString('xml', array('name' => 'Company'))) .'</pre>';

if ($c->writeConfig('D:/tmp/config.xml', 'xml', array('name' => 'Company')) === true) {

echo 'Config written into D:/tmp/config.xml';

}

?>

读取配置文件

<?php

require_once("Config.php");

$c = new Config();

$root = & $c->parseConfig("D:/tmp/member.xml", "xml");

//你也可以从其它地方读取配置信息如:

/**

$conf = array('DB' => array('type' => 'mysql',

'host' => 'localhost',

'user' => 'user',

'pass' => 'pass')

);

$root =& $c->parseConfig($conf, 'phparray', array('name' => 'conf'));

*/

if (PEAR::isError($root)) {

die('Error while reading configuration: ' . $root->getMessage());

}

$settings = $root->toArray();

printf('User settings: <a href="%s">%s %s</a>',

$settings['root']['Company']['Member']['www'],

$settings['root']['Company']['Member']['name'],

$settings['root']['Company']['Member']['email']

);

?>

下面来个复杂,实用一些的:

有如下的一个配置文件

<?xml version="1.0" encoding="ISO-8859-1"?>

<Company>

<Menu>

<Laptop>

<ibm>IBM</ibm>

<sony>SONY</sony>

</Laptop>

</Menu>

<Website>

<UK>

<b2c-battery>http://www.b2c-battery.co.uk</b2c-battery>

<global-batteries>http://www.global-batteries.co.uk</global-batteries>

</UK>

</Website>

<storage>

<driver>sql</driver>

<params>

<phptype>mysql</phptype>

<hostspec>localhost</hostspec>

<username>mamasam</username>

<password>foobar</password>

</params>

</storage>

</Company>

首先,我们先将它保存进xml文件中:

//write.php

<?php

require_once('Config.php');

$conf['storage']['driver'] = 'sql';

$conf['storage']['params']['phptype'] = 'mysql';

$conf['storage']['params']['hostspec'] = 'localhost';

$conf['storage']['params']['username'] = 'mamasam';

$conf['storage']['params']['password'] = 'foobar';

$conf['Menu']['Laptop'] = array('ibm'=>'IBM', 'sony'=>'SONY');

$conf['Website']['UK']['b2c-battery'] = 'http://www.b2c-battery.co.uk';

$conf['Website']['UK']['global-batteries'] = 'http://www.global-batteries.co.uk';

$c = new Config();

$root =& $c->parseConfig($conf, 'phparray');

$storage =& $root->getItem('section', 'storage');

//先删除,然后增加,改变它出现的顺序

$storage->removeItem();

$root->addItem($storage);

//echo '<pre>'. htmlspecialchars($root->toString('xml', array('name' => 'Company'))) .'</pre>';

if ($c->writeConfig('D:/tmp/config.xml', 'xml', array('name' => 'Company')) === true) {

echo 'Config written into D:/tmp/config.xml';

}

?>好了,接下来就是读取这个配置文件了,假设我们只需要数据库配置这段内容<storage>

//read.php

<?php

require_once('Config.php');

$c = new Config();

$root = & $c->parseConfig('D:/tmp/config.xml', 'xml');

if(PEAR::isError($root)) {

die($root->getMessage());

}

//读取指定选区的信息

//我这样读不行,不知道是不是Config的问题,但对一个简单的XML结构的文件是可以的,稍后会附上例子

//$storage =& $root->getItem('section', 'storage');

//因为以上的代码不行,所以只好用一个笨办法,这样对大数据量的文件来说,效率不高。

//你是不是有好的解决办法呢?不妨告诉我一下。

$content = $root->toArray();

unset($root);

$storage = $content['root']['Company']['storage'];

unset($content);

echo('<pre>');

print_r($storage);

echo('</pre>');

//这样你就得到了一个PHP数组了,其它的操作就看你自己了。

?>

一个可以用getItem()读取的例子,它的数据结构如下:

<?xml version="1.0" encoding="UTF-8"?>

<mysql>

<host>localhost</host>

<user>joe</user>

<pass>secret</pass>

<db>db456</db>

</mysql>

<?php

require_once("Config.php");

$c = new Config();

$root =& $c->parseConfig("mysql.xml", "XML");

$mysqlSection =& $root->getItem("section", "mysql");

$hostDirective =& $mysqlSection->getItem("directive", "host");

$userDirective =& $mysqlSection->getItem("directive", "user");

$passDirective =& $mysqlSection->getItem("directive", "pass");

$dbDirective =& $mysqlSection->getItem("directive", "db");

$user = $userDirective->getContent();

echo($user);

?>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航