| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- EN-Revision: 15103 -->
- <!-- Reviewed: no -->
- <sect1 id="zend.config.writer.introduction">
- <title>Zend_Config_Writer</title>
- <para>
- <classname>Zend_Config_Writer</classname>> gives you the ability to write config
- files out of <classname>Zend_Config</classname> objects. It works with an
- adapter-less system and thus is very easy to use. By default
- <classname>Zend_Config_Writer</classname>> ships with three adapters, which all
- work the same. You instantiate a writer with specific options, which
- can be <code>filename</code> and <code>config</code>. Then
- you call the <code>write()</code> method of the writer and the config
- file is created. You can also give <code>$filename</code> and
- <code>$config</code> directly to the <code>write()</code> method.
- Currently the following writers are shipped with
- <classname>Zend_Config_Writer</classname>>:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <classname>Zend_Config_Writer_Array</classname>
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Zend_Config_Writer_Ini</classname>
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Zend_Config_Writer_Xml</classname>
- </para>
- </listitem>
- </itemizedlist>
- <para>
- As an exception, <classname>Zend_Config_Writer_Ini</classname> has an additional
- option parameter <code>nestSeparator</code>, which defines with which
- character the single nodes are separated. The default is a single dot,
- like it is accepted by <classname>Zend_Config_Ini</classname> by default.
- </para>
- <para>
- When modifying or creating a <classname>Zend_Config</classname> object, there are
- some things to know. To create or modify a value, you simply say set
- the parameter of the <classname>Zend_Config</classname> object via the parameter
- accessor (<code>-></code>). To create a section in the root or to
- create a branch, you just create a new array
- (<code>$config->branch = array();</code>). To define which section
- extends another one, you call the <code>setExtend()</code> method
- on the root <classname>Zend_Config</classname> object.
- </para>
- <example id="zend.config.writer.example.using">
- <title>Using Zend_Config_Writer</title>
- <para>
- This example illustrates the basic use of
- <classname>Zend_Config_Writer_Xml</classname> to create a new config file:
- </para>
- <programlisting role="php"><![CDATA[
- // Create the config object
- $config = new Zend_Config(array(), true);
- $config->production = array();
- $config->staging = array();
- $config->setExtend('staging', 'production');
- $config->production->db = array();
- $config->production->db->hostname = 'localhost';
- $config->production->db->username = 'production';
- $config->staging->db = array();
- $config->staging->db->username = 'staging';
- // Write the config file in one of the following ways:
- // a)
- $writer = new Zend_Config_Writer_Xml(array('config' => $config,
- 'filename' => 'config.xml'));
- $writer->write();
- // b)
- $writer = new Zend_Config_Writer_Xml();
- $writer->setConfig($config)
- ->setFilename('config.xml')
- ->write();
- // c)
- $writer = new Zend_Config_Writer_Xml();
- $writer->write('config.xml', $config);
- ]]></programlisting>
- <para>
- This will create an XML config file with the sections production
- and staging, where staging extends production.
- </para>
- </example>
- <example id="zend.config.writer.modifying">
- <title>Modifying an existing config</title>
- <para>
- This example demonstrates how to edit an existing config file.
- </para>
- <programlisting role="php"><![CDATA[
- // Load all sections from an existing config file, while skipping the extends.
- $config = new Zend_Config_Ini('config.ini',
- null,
- array('skipExtends' => true,
- 'allowModifications' => true));
- // Modify a value
- $config->production->hostname = 'foobar';
- // Write the config file
- $writer = new Zend_Config_Writer_Ini(array('config' => $config,
- 'filename' => 'config.ini'));
- $writer->write();
- ]]></programlisting>
- </example>
- <note>
- <title>Loading a config file</title>
- <para>
- 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
- <code>skipExtends</code> as option to the constructor.
- </para>
- </note>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|