Issues

ZF-3196: E_WARNING is thrown (and no exception) when parse_ini_file fails in Zend_Config_Ini

Description

When there is a problem parsing the ini file passed to Zend_Config_Ini constructor, the PHP warning message is not caught and no exception is thrown. This is problematic because the developer has no way of knowing that a problem has happened - plus the E_WARNING message should not be displayed.

Use the following ini file to reproduce:


[default]
foo = ("bar")

Then, try to load this file with Zend_Config_Ini

Expected result: An exception thrown

Comments

I cannot reproduce your use-cased bug with PHP 5.2.5 and ZF at r9372

However, there is a PHP warning if the file loaded cannot be found or is not readable You can find more info in #ZF-2207

For PHP 5.2.0 or higher we can use error_get_last() to find the warning. e.g:


        $iniArray = @parse_ini_file($filename, true); // convert any warnings into exceptions
        $lastError = error_get_last();
        if($lastError && $lastError['line'] = (__LINE__-2) && strstr($lastError['file'], 'Ini.php')) {
            /**
             * @see Zend_Config_Exception
             */
            require_once 'Zend/Config/Exception.php';
            throw new Zend_Config_Exception($lastError['message']);
        }

As we need to support PHP 5.1.4, we have to use a custom error handler:


        $old_error_handler = set_error_handler(array('Zend_Config_Ini', 'parseIniFileErrorHandler'));
        $iniArray = parse_ini_file($filename, true); // convert any warnings into exceptions
        restore_error_handler();

Fixed on trunk in svn r9636.

Note that this introduces a small BC break if anyone was using the code with display_errors off, as now an exception will happen if this error occurs. Fixed only on trunk as a result.

You could use the track_errors cheat as well :


<?php
unset($php_errormsg);
if (ini_get('track_errors') == 0) {
    ini_set('track_errors',1);
}
@my_php_function();
ini_restore('track_errors');
if(isset($php_errormsg)) {
    throw new exception($php_errormsg);
}

Updating for the 1.6.0 release.