|

- 帖子
- 88
- 精华
- 0
- 积分
- 88
- 威望
- 0
- 银币
- 248
- 金币
- 20
- 在线时间
- 6 小时
- 注册时间
- 2009-7-3
|
2#
发表于 2009-7-3 22:01
| 只看该作者
清单 9. text3.php
<?php
class Configuration
{
…
function save()
{
$nf = ”;
$fh = fopen( $this->configFile, ‘r’ );
while( $l = fgets( $fh ) )
{
if ( preg_match( ‘/^#/’, $l ) == false )
{
preg_match( ‘/^(.*?)=(.*?)$/’, $l, $found );
$nf .= $found[1].”=”.$this->items[$found[1]].”\n”;
}
else
{
$nf .= $l;
}
}
fclose( $fh );
copy( $this->configFile, $this->configFile.’.bak’ );
$fh = fopen( $this->configFile, ‘w’ );
fwrite( $fh, $nf );
fclose( $fh );
}
}
$c = new Configuration();
echo( $c->TemplateDirectory.”\n” );
$c->TemplateDirectory = ‘foobar’;
echo( $c->TemplateDirectory.”\n” );
$c->save();
?>
新的 save 函数巧妙地**作 config.txt。我并没有仅用更新过的配置项重写文件(这样会移除掉注释),而是读取了这个文件并灵活地重写了 $items 数组中的内容。这样的话,就保留了文件中的注释。
在命令行运行该脚本并输出文本配置文件中的内容,能够看到下列输出。
清单 10. 保存函数输出
% php text3.php
tempdir
foobar
% cat config.txt
# My application’s configuration file
Title=My App
TemplateDirectory=foobar
%
原始的 config.txt 文件现在被新值更新了。
XML 配置文件
尽管文本文件易于阅读及编辑,但却不如 XML 文件流行。另外,XML 有众多适用的编辑器,这些编辑器能够理解标记、特殊符号转义等等。所以配置文件的 XML 版本会是什么样的呢?清单 11 显示了 XML 格式的配置文件。
清单 11. config.xml
<?xml version=”1.0″?>
<config>
<Title>My App</Title>
<TemplateDirectory>tempdir</TemplateDirectory>
</config>
清单 12 显示了使用 XML 来装载配置设置的 Configuration 类的更新版。
清单 12. xml1.php
<?php
class Configuration
{
private $configFile = ‘config.xml’;
private $items = array();
function __construct() { $this->parse(); }
function __get($id) { return $this->items[ $id ]; }
function parse()
{
$doc = new DOMDocument();
$doc->load( $this->configFile );
$cn = $doc->getElementsByTagName( “config” );
$nodes = $cn->item(0)->getElementsByTagName( “*” );
foreach( $nodes as $node )
$this->items[ $node->nodeName ] = $node->nodeValue;
}
}
$c = new Configuration();
echo( $c->TemplateDirectory.”\n” );
?>
看起来 XML 还有另一个好处:代码比文本版的代码更为简洁、容易。为保存这个 XML,需要另一个版本的 save 函数,将结果保存为 XML 格式,而不是文本格式。
清单 13. xml2.php
…
function save()
{
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( “config” );
$doc->appendChild( $r );
foreach( $this->items as $k => $v )
{
$kn = $doc->createElement( $k );
$kn->appendChild( $doc->createTextNode( $v ) );
$r->appendChild( $kn );
}
copy( $this->configFile, $this->configFile.’.bak’ );
$doc->save( $this->configFile );
}
…
这段代码创建了一个新的 XML 文档对象模型(Document Object Model ,DOM),然后将 $items 数组中的所有数据都保存到这个模型中。完成这些以后,使用 save 方法将 XML 保存为一个文件。
使用数据库
最后的替代方式是使用一个数据库保存配置元素的值。那首先要用一个简单的模式来存储配置数据。下面是一个简单的模式。
清单 14. schema.sql
DROP TABLE IF EXISTS settings;
CREATE TABLE settings (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name TEXT,
value TEXT,
PRIMARY KEY ( id )
);
这要求进行一些基于应用程序需求的调整。例如,如果想让配置元素按照每个用户进行存储,就需要添加用户 ID 作为额外的一列。
为了读取及写入数据,我编写了如图 15 所示的更新过的 Configuration 类。
清单 15. db1.php
<?php
require_once( ‘DB.php’ );
$dsn = ‘mysql://root:password@localhost/config’;
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }
class Configuration
{
private $configFile = ‘config.xml’;
private $items = array();
function __construct() { $this->parse(); }
function __get($id) { return $this->items[ $id ]; }
function __set($id,$v)
{
global $db;
$this->items[ $id ] = $v;
$sth1 = $db->prepare( ‘DELETE FROM settings WHERE name=?’ );
$db->execute( $sth1, $id );
if (PEAR::isError($db)) { die($db->getMessage()); }
$sth2 = $db->prepare(
‘INSERT INTO settings ( id, name, value ) VALUES ( 0, ?, ? )’ );
$db->execute( $sth2, array( $id, $v ) );
if (PEAR::isError($db)) { die($db->getMessage()); }
}
function parse()
{
global $db;
$doc = new DOMDocument();
$doc->load( $this->configFile );
$cn = $doc->getElementsByTagName( “config” );
$nodes = $cn->item(0)->getElementsByTagName( “*” );
foreach( $nodes as $node )
$this->items[ $node->nodeName ] = $node->nodeValue;
$res = $db->query( ‘SELECT name,value FROM settings’ );
if (PEAR::isError($db)) { die($db->getMessage()); }
while( $res->fetchInto( $row ) ) {
$this->items[ $row[0] ] = $row[1];
}
}
}
$c = new Configuration();
echo( $c->TemplateDirectory.”\n” );
$c->TemplateDirectory = ‘new foo’;
echo( $c->TemplateDirectory.”\n” );
?> |
|