Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Critical
-
Resolution: Fixed
-
Affects Version/s: 1.7.4
-
Fix Version/s: 1.7.6
-
Component/s: Zend_Config_Writer
-
Labels:None
-
Tags:
Description
When trying to write a Zend_Config that contains unsectioned elements, Zend_Config_Writer_Xml will omit these elements in the resulting XML file.
This issue related to http://framework.zend.com/issues/browse/ZF-5544. Unlike Zend_Config_Writer_Ini Zend_Config_Writer_Xml will not throw an Exception though.
Since a Zend_Config can contain unsectioned elements, the behavior of both writers is unexpected. Both writers should be able to write Zend_Configs without a global section defined for them.
Zend_Config_Writer_Xml already wraps all data inside a <zend_config> root element, so any unsectioned elements can reside below this element without breaking validity of the XML file.
If global sections are required by ZF, the writers should at least both throw an Exception.
Example
// Code from Example 7.2. Using Zend_Config with a PHP Configuration File $myValues = array( 'webhost' => 'www.example.com', 'database' => array( 'adapter' => 'pdo_mysql', 'params' => array( 'host' => 'db.example.com', 'username' => 'dbuser', 'password' => 'secret', 'dbname' => 'mydatabase' ) ) ); $config = new Zend_Config($myValues); // will output the array as defined above Zend_Debug::dump($config->toArray()); // write config to an XML file $writer = new Zend_Config_Writer_Xml(array('config' => $config, 'filename' => 'test.xml')); // yields no error, but loses data $writer->write(); // Read in XML file $xml = new Zend_Config_Xml('test.xml'); // Webhost is missing Zend_Debug::dump($xml->toArray()); // Try to write an Ini file from the example config $writer = new Zend_Config_Writer_Ini(array('config' => $config, 'filename' => 'test.ini')); // will throw an Exception as stated in ZF-5544 $writer->write();
The resulting XML file
<?xml version="1.0"?> <zend-config> <database> <adapter>pdo_mysql</adapter> <params> <host>db.example.com</host> <username>dbuser</username> <password>secret</password> <dbname>mydatabase</dbname> </params> </database> </zend-config>
In fact, Zend_Config_Writers (except _Array) always expects the highest elements in Zend_Config to be the sections. What you are doing in your example is converting from an array config (which can never contain sections, thus Zend_Config_Writer_Array works fine with no sections) to a configuration format which requires sections when loaded with Zend_Config_Xml / Zend_Config_Ini. Thus this simply cannot work properly.
Zend_Config_Writer_Xml is natually omitting the webhost-key, as it is seen as an invalid section. That Zend_Config_Writer_Ini is throwing a catchable fatal error is surely wrong, it should have the same behaviour as Zend_Config_Writer_Xml at this point.