DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> PHP PEAR::Config管理配置信息
PHP PEAR::Config管理配置信息
編輯:AJAX詳解     

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved