Zend_Config_Writer - Zend_Config_Writer


Zend_Config_Writer gives you the ability to write config files out of Zend_Config objects. It works with an adapter-less system and thus is very easy to use. By default Zend_Config_Writer ships with three adapters, which are all file-based. You instantiate a writer with specific options, which can be filename and config. Then you call the write() method of the writer and the config file is created. You can also give $filename and $config directly to the write() method. Currently the following writers are shipped with Zend_Config_Writer:

  • Zend_Config_Writer_Array

  • Zend_Config_Writer_Ini

  • Zend_Config_Writer_Xml

The INI writer has two modes for rendering with regard to sections. By default the top-level configuration is always written into section names. By calling $writer->setRenderWithoutSections(); all options are written into the global namespace of the INI file and no sections are applied.

As an addition Zend_Config_Writer_Ini has an additional option parameter nestSeparator, which defines with which character the single nodes are separated. The default is a single dot, like it is accepted by Zend_Config_Ini by default.

When modifying or creating a Zend_Config object, there are some things to know. To create or modify a value, you simply say set the parameter of the Zend_Config object via the parameter accessor (->). To create a section in the root or to create a branch, you just create a new array ("$config->branch = array();"). To define which section extends another one, you call the setExtend() method on the root Zend_Config object.

Example #1 Using Zend_Config_Writer

This example illustrates the basic use of Zend_Config_Writer_Xml to create a new config file:

  1. // Create the config object
  2. $config = new Zend_Config(array(), true);
  3. $config->production = array();
  4. $config->staging    = array();
  6. $config->setExtend('staging', 'production');
  8. $config->production->db = array();
  9. $config->production->db->hostname = 'localhost';
  10. $config->production->db->username = 'production';
  12. $config->staging->db = array();
  13. $config->staging->db->username = 'staging';
  15. // Write the config file in one of the following ways:
  16. // a)
  17. $writer = new Zend_Config_Writer_Xml(array('config'   => $config,
  18.                                            'filename' => 'config.xml'));
  19. $writer->write();
  21. // b)
  22. $writer = new Zend_Config_Writer_Xml();
  23. $writer->setConfig($config)
  24.        ->setFilename('config.xml')
  25.        ->write();
  27. // c)
  28. $writer = new Zend_Config_Writer_Xml();
  29. $writer->write('config.xml', $config);

This will create an XML config file with the sections production and staging, where staging extends production.

Example #2 Modifying an Existing Config

This example demonstrates how to edit an existing config file.

  1. // Load all sections from an existing config file, while skipping the extends.
  2. $config = new Zend_Config_Ini('config.ini',
  3.                               null,
  4.                               array('skipExtends'        => true,
  5.                                     'allowModifications' => true));
  7. // Modify a value
  8. $config->production->hostname = 'foobar';
  10. // Write the config file
  11. $writer = new Zend_Config_Writer_Ini(array('config'   => $config,
  12.                                            'filename' => 'config.ini'));
  13. $writer->write();

Note: Loading a Config File
When loading an existing config file for modifications it is very important to load all sections and to skip the extends, so that no values are merged. This is done by giving the skipExtends as option to the constructor.

For all the File-Based writers (INI, XML and PHP Array) internally the render() is used to build the configuration string. This method can be used from the outside also if you need to access the string-representation of the configuration data.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.