Pragmatism in the real world

Zend_Config_Ini and a string

One thing that is different between Zend_Config_Xml and Zend_Config_Ini is that with Zend_Config_Xml you can pass in an XML string as the first parameter of the constructor and it will work. This doesn’t work with Zend_Config_Ini as we use parse_ini_file() under the hood.

With PHP 5.3 however there is is a new function called parse_ini_string() which will allow us to load arbitrary ini string into Zend_Config objects. This can’t go into Zend Framework 1 though due to our PHP 5.2.4 minimum version requirement.

As I needed this for a project, I extended Zend_Config_Ini to support this feature, which means simply overloading a single method

class App_Config_Ini extends Zend_Config_Ini
{
/**
* Load the INI file from disk using parse_ini_file(). Use a private error
* handler to convert any loading errors into a Zend_Config_Exception
*
* @param string $filename
* @throws Zend_Config_Exception
* @return array
*/
protected function _parseIniFile($filename)
{
set_error_handler(array($this, '_loadFileErrorHandler'));
if (substr($filename, -4) == '.ini') {
$iniArray = parse_ini_file($filename, true);
} else {
$iniArray = parse_ini_string($filename, true);
}
restore_error_handler();

// Check if there was a error while loading file
if ($this->_loadFileErrorStr !== null) {
/**
* @see Zend_Config_Exception
*/
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception($this->_loadFileErrorStr);
}

return $iniArray;
}
}

The actual change is to see if the last 4 characters of the filename are “.ini” and if they aren’t then use parse_ini_string() instead of parse_ini_file(). The rest of the code is just error handling.

This is one area where I really like it when a class implements methods that done just one thing.

5 thoughts on “Zend_Config_Ini and a string

  1. This would be a great addition to ZF itself Rob, maybe ZF2 since it will raise requirements.

    What would you say are the disadvantages of using file_exists to figure out if its a file, since there might be a remote chance that the last config in the ini string could be something like:

    db.config = /path/to/file.ini

    Remote yes, but possible.

    Great snippet, does help lots of people.

    1. I think I'll definitely get it into ZF2.

      Main problem with file_exists() is that it's yet another stat call with much slower than a simple substr() check. If you follow the rules, you have to put any value that's not alphanumeric inside double quotes, so the last line of the string would be:

      db.config = "/path/to/file.ini"

      Regards,

      Rob…

  2. Just what I was looking for Rob, thank you!

    In the current project I'm working on we have paranoid sysadmins that don't like to have production values in Subversion, so we have to override the configs with a config file in /etc/, before I had to concat the files and create a new one in /tmp, this is much better (saves some disk IO and more secure).

    Even though I'm pretty sure you're not going to come after me with your lawyer, is there any chance you could mention what license the snippet is published under?

  3. Rob — you can actually slip-stream this into ZF1 if you simply check for a PHP version >= 5.3.0 in the code. (We do this in a number of places already, which allows us to build forward-compatible features and/or use more performant methods from newer versions than the minimum supported version.)

Comments are closed.