在 PHP 專案裡,設定資料散落在各處是維護噩梦的起點。PEAR 的 Config 套件提供了一個統一的抽象層:同一份設定,可以用 XML、INI、PHP 陣列甚至 Apache conf 來存,讀寫之間自由轉換。
它主要提供三個能力:
1. 解析多種資料格式;
2. 完整保留 section、directive、註解與空行等結構資訊;
3. 把設定重新寫回你偏好的格式。
最外層是 Config_Container 物件,內部可包含四種節點:
· Section:可再包含其他 Config_Container,形成樹狀結構。
· Directive:不含子節點,只有一組 key-value。
· Comment:註解。
· Blank:空行。
支援的格式包含 XML、PHP array、inifile 與 Apache conf。
假設我們要保存員工資料,目標長這樣:
<?xml version="1.0" encoding="UTF-8"?>
<Company>
<Member>
<name>jxyuhua</name>
<email>jxyuhua at yahoo.com.cn</email>
<www>https://www.example.com/</www>
</Member>
</Company>
先用陣列描述設定,再交給 Config 處理:
<?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']['example'] = 'https://www.example.com';
$c = new Config();
$root =& $c->parseConfig($conf, 'PHParray');
// 調整節點順序:先移除再加入,它就會排到最後
$storage =& $root->getItem('section', 'storage');
$storage->removeItem();
$root->addItem($storage);
if ($c->writeConfig('/tmp/config.xml', 'XML',
array('name' => 'Company')) === true) {
echo 'Config written into /tmp/config.xml';
}
?>
如果想先預覽結果而不寫檔,可以用 $root->toString('XML', array('name' => 'Company')) 取得字串輸出。
<?php
require_once('Config.php');
$c = new Config();
$root =& $c->parseConfig('/tmp/member.xml', 'XML');
if (PEAR::isError($root)) {
die('Error while reading configuration: ' . $root->getMessage());
}
$settings = $root->toArray();
printf('<a href="%s">%s %s</a>',
$settings['root']['Company']['Member']['www'],
$settings['root']['Company']['Member']['name'],
$settings['root']['Company']['Member']['email']
);
?>
來源不一定要是檔案。直接傳陣列也可以,只要記得指定根節點名稱:
$conf = array('DB' => array('type' => 'mysql',
'host' => 'localhost',
'user' => 'user',
'pass' => 'pass'));
$root =& $c->parseConfig($conf, 'PHParray', array('name' => 'conf'));
假設 config.xml 裡同時有 Menu、Website 與 storage 三段,而你只需要 storage:
<?php
require_once('Config.php');
$c = new Config();
$root =& $c->parseConfig('/tmp/config.xml', 'XML');
if (PEAR::isError($root)) {
die($root->getMessage());
}
$content = $root->toArray();
unset($root);
$storage = $content['root']['Company']['storage'];
unset($content);
echo '<pre>';
print_r($storage);
echo '</pre>';
?>
這裡刻意先用 toArray() 轉成純 PHP 陣列再取用。原因是:結構較深的 XML 直接用 getItem('section', 'storage') 有時取不到,結構簡單的檔案則沒問題。改成陣列法的代價是效率——大型設定檔會把整棵樹都載入記憶體。若你知道更好的取用方式,值得替換掉這段。
對應這份結構簡單的 XML:
<?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');
echo $userDirective->getContent();
?>
Config 的價值在於「格式無關」:開發階段用 PHP 陣列方便快速迭代,交付時寫成 XML 或 INI 給使用者手動編輯,程式碼幾乎不用改。要注意的是它並非高效能元件,每次請求都重新解析大型 XML 的代價不低;實務上常見的做法是啟動時解析一次,把 toArray() 的結果放進快取或應用層級的全域變數,後續都讀這份快取。