Yaf_Config_Abstract被设计在应用程序中简化访问和使用配置数据。它为在应用程序代码中访问这样的配置数据提 供了一个基于用户接口的嵌入式对象属性。配置数据可能来自于各种支持等级结构数据存储的媒体。 Yaf_Config_Abstract实现了Countable, ArrayAccess 和 Iterator 接口。 这样,可以基于Yaf_Config_Abstract对象使用count()函数和PHP语句如foreach, 也可以通过数组方式访问Yaf_Config_Abstract的元素.
Yaf_Config_Abstract类摘要[php] view plaincopyAbstract Yaf_Config_Abstract implements Iterator , ArrayAccess , Countable { protected array _config ; protected array _readonly ; public mixed get ( string $name = NULL ); public mixed __get ( string $name ); public mixed __isset ( string $name ); public mixed __set ( string|int $name , mixed $value ); public mixed set ( string|int $name , mixed $value ); public mixed count ( void ); public mixed offsetGet ( string|int $name ); public mixed offsetSet ( string|int $name , mixed $value ); public mixed offsetExists ( string|int $name ); public mixed offsetUnset ( string|int $name ); public void rewind ( void ); public mixed key ( void ); public mixed next ( void ); public mixed current ( void ); public boolean valid ( void ); public array toArray ( void ); public boolean readOnly ( void ); } 此外Yaf已经内建了两种配置适配器,一个是Yaf_Config_Ini,这个是为存储在ini文件提供适配,另一个是Yaf_Config_Simple,为数组形式的配置提供适配。 Yaf_Config_Ini Yaf_Config_Ini是基于PHP的parse_ini_file()来解析配置文件的,对于没有接触过得可以去看一下parse_ini_file()函数的使用方法和一些数据的处理方式。 Yaf_Config_Ini允许开发者通过嵌套的对象属性语法在应用程序中用熟悉的 INI 格式存储和读取配置数据。INI格式在提供拥有配置数据键的等级结构和配置数据节之间的继承能力方面具有专长。 配置数据等级结构通过用点或者句号(.)分离键值。一个节可以扩展或者通过在节的名称之后带一个冒号(:)和被继承的配置数据的节的名称来从另一个节继承。 文件示例: [php] view plaincopy[base] database.master.host = localhost [production : base] ;Yaf的配置 application.directory = /usr/local/www/production ;应用的配置 webhost = www.example.com database.adapter = pdo_mysql database.params.host = db.example.com database.params.username = dbuser database.params.password = secret database.params.dbname = dbname ; 开发站点配置数据从生产站点配置数据集成并如果需要可以重写 [dev : production] application.directory = /usr/dev/htdocs database.params.host = dev.example.com database.params.username = devuser database.params.password = devsecret dev节, 将得到production节的所有配置, 并间接获得base节的配置 并且覆盖application.directory的配置为"/usr/dev/htdocs"。Yaf_Config_Abstract实现了__get方法, 所以获取配置将会变得很容易 [php] view plaincopy$config = new Yaf_Config_Ini('/path/to/config.ini', 'staging'); echo $config->database->get("params")->host; // 输出 "dev.example.com" echo $config->get("database")->params->dbname; // 输出 "dbname" Yaf_Config_Simple Yaf_Config_Simple 是 Yad_Config_ini 的简洁版本,只允许传入数组进行初始化,并提供了设置readonly的参数。 Yaf_Config_Simple类结构 [php] view plaincopyYaf_Config_Simple extends Yaf_Config_Abstract implements Iterator , Traversable , ArrayAccess , Countable { /* 属性 */ protected $_readonly ; /* 方法 */ public __construct ( string $config_file [, string $section ] ) public void count ( void ) public void current ( void ) public void __get ([ string $name ] ) public void __isset ( string $name ) public void key ( void ) public void next ( void ) public void offsetExists ( string $name ) public void offsetGet ( string $name ) public void offsetSet ( string $name , string $value ) public void offsetUnset ( string $name ) public void readonly ( void ) public void rewind ( void ) public void __set ( string $name , string $value ) public void toArray ( void ) public void valid ( void ) /* 继承的方法 */ abstract public mixed Yaf_Config_Abstract::get ( string $name , mixed $value ) abstract public bool Yaf_Config_Abstract::readonly ( void ) abstract public Yaf_Config_Abstract Yaf_Config_Abstract::set ( void ) abstract public array Yaf_Config_Abstract::toArray ( void ) } 成员函数列表 Yaf_Config_Simple::__construct — 构造函数 Yaf_Config_Simple::count — 返回配置的节数量 Yaf_Config_Simple::current — 返回当前节点 Yaf_Config_Simple::__get — 读取节点配置 Yaf_Config_Simple::__isset — 检查节点是否存在 Yaf_Config_Simple::key — 返回当前元素的键 Yaf_Config_Simple::next — 向前移动到下一个元素 Yaf_Config_Simple::offsetExists — 检查一个偏移位置是否存在 Yaf_Config_Simple::offsetGet — 获取一个偏移位置的值 Yaf_Config_Simple::offsetSet — 设置一个偏移位置的值 Yaf_Config_Simple::offsetUnset — 复位一个偏移位置的值 Yaf_Config_Simple::readonly — 检查配置是否只读 Yaf_Config_Simple::rewind — 检查当前位置是否有效 Yaf_Config_Simple::__set — 设置节点配置 Yaf_Config_Simple::toArray — 转换为数组的格式 Yaf_Config_Simple::valid — 检查迭代器是否有效