博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Zend\Config\Reader
阅读量:6847 次
发布时间:2019-06-26

本文共 8362 字,大约阅读时间需要 27 分钟。

hot3.png

Zend\Config\Reader gives you the ability to read a config file. It works with concrete implementations for different file format. TheZend\Config\Reader is only an interface, that define the two methods fromFile() and fromString(). The concrete implementations of this interface are:

  • Zend\Config\Reader\Ini
  • Zend\Config\Reader\Xml
  • Zend\Config\Reader\Json
  • Zend\Config\Reader\Yaml

The fromFile() and fromString() return a PHP array contains the data of the configuration file.

Note

Differences from ZF1

The Zend\Config\Reader component no longer supports the following features:

  • Inheritance of sections.
  • Reading of specific sections.

Zend\Config\Reader\Ini

Zend\Config\Reader\Ini enables developers to store configuration data in a familiar INI format and read them in the application by using an array syntax.

Zend\Config\Reader\Ini utilizes the  PHP function. Please review this documentation to be aware of its specific behaviors, which propagate to Zend\Config\Reader\Ini, such as how the special values of “TRUE”, “FALSE”, “yes”, “no”, and “NULL” are handled.

Note

Key Separator

By default, the key separator character is the period character (“.”). This can be changed, however, using the setNestSeparator()method. For example:

$reader = new Zend\Config\Reader\Ini(); $reader->setNestSeparator('-');

The following example illustrates a basic use of Zend\Config\Reader\Ini for loading configuration data from an INI file. In this example there are configuration data for both a production system and for a staging system. Suppose we have the following INI configuration file:

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 = 'dbproduction'

We can use the Zend\Config\Reader\Ini to read this INI file:

$reader = new Zend\Config\Reader\Ini(); $data = $reader->fromFile('/path/to/config.ini'); echo $data['webhost'] // prints "www.example.com" echo $data['database']['params']['dbname']; // prints "dbproduction"

The Zend\Config\Reader\Ini supports a feature to include the content of a INI file in a specific section of another INI file. For instance, suppose we have an INI file with the database configuration:

database.adapter = 'pdo_mysql' database.params.host = 'db.example.com' database.params.username = 'dbuser' database.params.password = 'secret' database.params.dbname = 'dbproduction'

We can include this configuration in another INI file, for instance:

webhost = 'www.example.com' @include = 'database.ini'

If we read this file using the component Zend\Config\Reader\Ini we will obtain the same configuration data structure of the previous example.

The   = 'file-to-include.ini' can be used also in a subelement of a value. For instance we can have an INI file like that:

adapter = 'pdo_mysql' params.host = 'db.example.com' params.username = 'dbuser' params.password = 'secret' params.dbname = 'dbproduction'

And assign the   as sublement of the database value:

webhost = 'www.example.com' database.@include = 'database.ini'

Zend\Config\Reader\Xml

Zend\Config\Reader\Xml enables developers to read configuration data in a familiar XML format and read them in the application by using an array syntax. The root element of the XML file or string is irrelevant and may be named arbitrarily.

The following example illustrates a basic use of Zend\Config\Reader\Xml for loading configuration data from an XML file. Suppose we have the following XML configuration file:

www.example.com

We can use the Zend\Config\Reader\Xml to read this XML file:

$reader = new Zend\Config\Reader\Xml(); $data = $reader->fromFile('/path/to/config.xml'); echo $data['webhost'] // prints "www.example.com" echo $data['database']['params']['dbname']; // prints "dbproduction"

Zend\Config\Reader\Xml utilizes the  PHP class. Please review this documentation to be aware of its specific behaviors, which propagate to Zend\Config\Reader\Xml.

Using Zend\Config\Reader\Xml we can include the content of XML files in a specific XML element. This is provided using the standard function  of XML. To use this function you have to add the namespace xmlns:xi="http://www.w3.org/2001/XInclude" to the XML file. Suppose we have an XML files that contains only the database configuration:

pdo_mysql
db.example.com
dbuser
secret
dbproduction

We can include this configuration in another XML file, for instance:

 
 
www.example.com
 
 

The syntax to include an XML file in a specific element is <xi:include href="file-to-include.xml"/>

Zend\Config\Reader\Json

Zend\Config\Reader\Json enables developers to read configuration data in a JSON format and read them in the application by using an array syntax.

The following example illustrates a basic use of Zend\Config\Reader\Json for loading configuration data from a JSON file. Suppose we have the following JSON configuration file:

{ "webhost" : "www.example.com",    "database" : {         "adapter" : "pdo_mysql",         "params" : {              "host" : "db.example.com",              "username" : "dbuser",               "password" : "secret",               "dbname" : "dbproduction"   } } }

We can use the Zend\Config\Reader\Json to read this JSON file:

$reader = new Zend\Config\Reader\Json(); 
$data = $reader->fromFile('/path/to/config.json'); 
echo $data['webhost'] // prints "www.example.com" 
echo $data['database']['params']['dbname']; // prints "dbproduction"

Zend\Config\Reader\Json utilizes the  class.

Using Zend\Config\Reader\Json we can include the content of a JSON file in a specific JSON section or element. This is provided using the special syntax  . Suppose we have a JSON file that contains only the database configuration:

{ "database" : {   "adapter" : "pdo_mysql",   "params" : {      "host" : "db.example.com",      "username" : "dbuser",      "password" : "secret",     "dbname" : "dbproduction"    }  } }

We can include this configuration in another JSON file, for instance:

{ "webhost" : "www.example.com", "@include" : "database.json" }

Zend\Config\Reader\Yaml

Zend\Config\Reader\Yaml enables developers to read configuration data in a YAML format and read them in the application by using an array syntax. In order to use the YAML reader we need to pass a callback to an external PHP library or use the .

The following example illustrates a basic use of Zend\Config\Reader\Yaml that use the Yaml PECL extension. Suppose we have the following YAML configuration file:

webhost: www.example.com database:     adapter: pdo_mysql     params:       host: db.example.com       username: dbuser       password:secret       dbname: dbproduction

We can use the Zend\Config\Reader\Yaml to read this YAML file:

$reader = new Zend\Config\Reader\Yaml(); $data = $reader->fromFile('/path/to/config.yaml'); echo $data['webhost'] // prints "www.example.com" echo $data['database']['params']['dbname']; // prints "dbproduction"

If you want to use an external YAML reader you have to pass the callback function in the constructor of the class. For instance, if you want to use the  library:

// include the Spyc library require_once ('path/to/spyc.php'); $reader = new Zend\Config\Reader\Yaml(array('Spyc','YAMLLoadString')); $data = $reader->fromFile('/path/to/config.yaml'); echo $data['webhost'] // prints "www.example.com" echo $data['database']['params']['dbname']; // prints "dbproduction"

You can also instantiate the Zend\Config\Reader\Yaml without any parameter and specify the YAML reader in a second moment using thesetYamlDecoder() method.

Using Zend\Config\ReaderYaml we can include the content of a YAML file in a specific YAML section or element. This is provided using the special syntax  . Suppose we have a YAML file that contains only the database configuration:

database: adapter: pdo_mysql params: host: db.example.com username: dbuser password: secret dbname: dbproduction

We can include this configuration in another YAML file, for instance:

webhost:  www.example.com@include: database.yaml

转载于:https://my.oschina.net/mickelfeng/blog/107717

你可能感兴趣的文章
阿里云基于NVM的持久化高性能Redis数据库
查看>>
阿里如何做到百万量级硬件故障自愈?
查看>>
阿里云重磅发布DMS数据库实验室 免费体验数据库引擎
查看>>
832. Flipping an Image - LeetCode
查看>>
791. Custom Sort String - LeetCode
查看>>
人力资源系统想法
查看>>
Android中的ListView控件
查看>>
PHP学习笔记【13】--查找
查看>>
腾讯QQ_2013年_最新加密解密_易语言也是母语_解开神秘的QQ
查看>>
shell脚本--把每天磁盘的使用情况生成一个文件
查看>>
VS2010程序开发环境可运行,不能独立运行
查看>>
PostgreSQL实用日常维护SQL
查看>>
hostname命令
查看>>
Drop与Truncate的区别。
查看>>
在 Arch Linux 上安装 XFCE
查看>>
Android Studio -- 设置字体样式
查看>>
编译安装详解
查看>>
我的友情链接
查看>>
LOG4J常用配置
查看>>
android HorizontalScrollView
查看>>