PHP PEAR::Config管理配置信息

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);
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值