Analysis of the parsing implementation of the PHP INI configuration file

  • 2020-03-31 21:28:03
  • OfStack

So when I read this article, I just knew that there is still a dba function that can be used. Well, I took a closer look at the installtion of this dba function and found that supporting inifile was also implemented from PHP5. Well, the corresponding dba related can see here: http://www.php.net/manual/en/dba.installation.php, detailed or look here: http://www.php.net/manual/en/book.dba.php

OK, the original, it comes from: http://www.cardii.net/php-spl-parse-ini-file/.

The various types of SPL interfaces and iterators have been described. Today, when browsing the PHP source directory, I found an example of parsing INI files. I thought it was good, so I compiled an example and Shared it.

In PHP applications, configuration files are indispensable, especially for mall, CMS and other products. Different customers have different needs. Of course, each customer will not develop a set of programs. As I said before, there are four main types of configuration files: PHP arrays (almost all other configuration methods end up parsed as PHP arrays), XML,YAML, and INI. Today I will only talk about INI files. The ZendFramework USES this configuration.

Let's look at a DbaReader class. The file name is dbareader.php:
 
<?php 
class DbaReader implements Iterator 
{ 

protected $db = NULL; 
private $key = false; 
private $val = false; 

 
function __construct($file, $handler) { 
if (!$this->db = dba_open($file, 'r', $handler)) { 
throw new exception('Could not open file ' . $file); 
} 
} 

 
function __destruct() { 
dba_close($this->db); 
} 

 
function rewind() { 
$this->key = dba_firstkey($this->db); 
$this->fetch_data(); 
} 

 
function next() { 
$this->key = dba_nextkey($this->db); 
$this->fetch_data(); 
} 

 
private function fetch_data() { 
if ($this->key !== false) { 
$this->val = dba_fetch($this->key, $this->db); 
} 
} 

 
function current() { 
return $this->val; 
} 

 
function valid() { 
if ($this->db && $this->key !== false) { 
return true; 
} else { 
return false; 
} 
} 

 
function key() { 
return $this->key; 
} 
} 
?> 

DbaReader USES the Iterator interface, of course, to implement the five iterated methods. The iterative method is used to parse the handlerhandlerINI file using the dba extension.

As an aside, what is a Dba? Why use dbas?
The Dba is a database, or rather, an indexed file storage system. Suitable for relatively static indexed data storage. All versions of Linux come with this database.
Why use a Dba when you're using files to store data? There are two reasons:
1. The storage length of data records may not be fixed;
Use indexes to store and retrieve data.

DbaReader provides an iterative method to access the data in the INI file. What if you need to store the deleted data? So DbaArray implements this functionality by inheriting from DbaReader.
 
<?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. The values 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 could not be opened"); 
} 
} 

 
function __destruct() 
{ 
parent::__destruct(); 
} 

 
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; 
} 
} 

 
function offsetSet($name, $value) 
{ 
//dba_replace($name, serialize($value), $this->db); 
dba_replace($name, $value, $this->db); 
return $value; 
} 

 
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); 
} 
} 
?> 

Use examples
The build file text.ini reads as follows:
 
host = localhost 
password = password 
database = data 

File index.php. The code is as follows:
 
<?php 
function loadClass($class) 
{ 
require_once __DIR__.DIRECTORY_SEPARATOR.$class.'.php'; 
} 
spl_autoload_register('loadClass',false); 

$iniFile = __DIR__.DIRECTORY_SEPARATOR.'test.ini'; 

$ini = new DbaArray($iniFile,'iniFile'); 
echo $ini['database']; 
var_dump($ini); 
?> 

- the EOF -

After reading the above paragraph, do you have any ideas? Is the operation of ini so convenient? However, if it's a pure read, I'd still recommend something like parse_ini_file (all of a sudden, what if the code is different? ANSI /utf-8, this is really an eternal pain.

Related articles: