Analysis of php INI configuration file parsing

Source: Internet
Author: User
Tags spl rewind

So when I saw this article, I just learned that there was a dba function available. Well, I carefully read the dba function installtion, it is found that the support for inifile is also implemented from PHP5. Well, you can refer to the following for the relevant dba: Role

OK. The original Article is from http://www.cardii.net/php-spl-parse-ini-file /.

I have introduced various SPL interfaces and iterators. Today, when browsing the PHP source code directory, I found an example of parsing the INI file. I thought it was good, so I sorted out an instance and shared it with me.

In PHP applications, configuration files are indispensable, especially for products such as malls and CMS. Different customers have different requirements. Of course, each customer will not develop a set of programs, A good solution is that each customer has a set of different configuration files. I once said that the configuration files are suitable for the following four types: PHP arrays (almost all other configuration methods are eventually parsed into PHP arrays), XML, YAML, and INI. Today, I will only talk about INI files. ZendFramework uses this configuration.

See the DbaReader class. The file name is DbaReader. php:
Copy codeThe Code is as follows:
<? Php
Class DbaReader implements Iterator
{

Protected $ db = NULL;
Private $ key = false;
Private $ val = false;

/**
* Open database $ file with $ handler in read only mode.
*
* @ Param file Database file to open.
* @ Param handler Handler to use for database access.
*/
Function _ construct ($ file, $ handler ){
If (! $ This-> db = dba_open ($ file, 'R', $ handler )){
Throw new exception ('could not open file'. $ file );
}
}

/**
* Close database.
*/
Function _ destruct (){
Dba_close ($ this-> db );
}

/**
* Rewind to first element.
*/
Function rewind (){
$ This-> key = dba_firstkey ($ this-> db );
$ This-> fetch_data ();
}

/**
* Move to next element.
*
* @ Return void
*/
Function next (){
$ This-> key = dba_nextkey ($ this-> db );
$ This-> fetch_data ();
}

/**
* Fetches the current data if $ key is valid
*/
Private function fetch_data (){
If ($ this-> key! = False ){
$ This-> val = dba_fetch ($ this-> key, $ this-> db );
}
}

/**
* @ Return Current data.
*/
Function current (){
Return $ this-> val;
}

/**
* @ Return Whether more elements are available.
*/
Function valid (){
If ($ this-> db & $ this-> key! = False ){
Return true;
} Else {
Return false;
}
}

/**
* @ Return Current key.
*/
Function key (){
Return $ this-> key;
}
}
?>

DbaReader uses the Iterator interface. Of course, five iteration methods must be implemented. The iteration method parses the handlerhandlerINI file and uses the dba extension.

What is Dba? Why Dba?
Dba is a database. Specifically, Dba is an indexed file storage system. Suitable for relatively static indexed data storage. All versions of Linux will contain this database.
Since files are used to store data, why Dba? There are two reasons:
1. The storage length of data records may not be fixed;
2. Use indexes to store and retrieve data.

DbaReader provides an iterative method to access INI file data. What if you need to store and delete data? Therefore, DbaArray implements this function based on DbaReader inheritance.
Copy codeThe Code is as follows:
<? Php
Class DbaArray extends DbaReader implements ArrayAccess
{

/**
* Open database $ file with $ handler in read only mode.
*
* @ Param file Database file to open.
* @ Param handler Handler to use for database access. Value http://www.php.net/manual/en/dba.requirements.php
*/
Function _ construct ($ file, $ handler)
{
$ This-> db = dba_popen ($ file, "c", $ handler );
If (! $ This-> db ){
Throw new exception ("Databse cocould not be opened ");
}
}

/**
* Close database.
*/
Function _ destruct ()
{
Parent: :__ destruct ();
}

/**
* Read an entry.
*
* @ Param $ name key to read from
* @ Return value associated with $ name
*/
Function offsetGet ($ name)
{
$ Data = dba_fetch ($ name, $ this-> db );
If ($ data ){
If (ini_get ('Magic _ quotes_runtime ')){
$ Data = stripslashes ($ data );
}
// Return unserialize ($ data );
Return $ data;
}
Else
{
Return NULL;
}
}

/**
* Set an entry.
*
* @ Param $ name key to write
* @ Param $ value to write
*/
Function offsetSet ($ name, $ value)
{
// Dba_replace ($ name, serialize ($ value), $ this-> db );
Dba_replace ($ name, $ value, $ this-> db );
Return $ value;
}

/**
* @ Return whether key $ name exists.
*/
Function offsetExists ($ name)
{
Return dba_exists ($ name, $ this-> db );
}

/**
* Delete a key/value pair.
*
* @ Param $ name key to delete.
*/
Function offsetUnset ($ name)
{
Return dba_delete ($ name, $ this-> db );
}
}
?>

Example
Build the text. ini file with the following content:
Copy codeThe Code is as follows:
Host = localhost
Password = password
Database = data

The file index. php. Code is as follows:
Copy codeThe Code is as follows:
<? Php
Function loadClass ($ class)
{
Require_once _ dir).directory_separator. $ class. '. php ';
}
Spl_autoload_register ('loadclass', false );

$ IniFile = _ dir0000.directory_separator. 'test. ini ';

$ Ini = new DbaArray ($ iniFile, 'inifile ');
Echo $ ini ['database'];
Var_dump ($ ini );
?>

-- EOF --

After reading the above section, is there any idea? Is it so convenient for ini operations? However, if it is purely read, I still recommend parse_ini_file and the like (suddenly forget, what if the encoding is different? Ansi/UTF-8, this is really an eternal pain .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.