2
0

Zend_Config_Writer.xml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.config.writer.introduction">
  4. <title>Zend_Config_Writer</title>
  5. <para>
  6. <classname>Zend_Config_Writer</classname> gives you the ability to write config
  7. files out of <classname>Zend_Config</classname> objects. It works with an
  8. adapter-less system and thus is very easy to use. By default
  9. <classname>Zend_Config_Writer</classname> ships with four adapters, which are all
  10. file-based. You instantiate a writer with specific options, which
  11. can be <emphasis>filename</emphasis> and <emphasis>config</emphasis>. Then
  12. you call the <methodname>write()</methodname> method of the writer and the config
  13. file is created. You can also give <varname>$filename</varname> and
  14. <varname>$config</varname> directly to the <methodname>write()</methodname> method.
  15. Currently the following writers are shipped with
  16. <classname>Zend_Config_Writer</classname>:
  17. </para>
  18. <itemizedlist>
  19. <listitem>
  20. <para>
  21. <classname>Zend_Config_Writer_Array</classname>
  22. </para>
  23. </listitem>
  24. <listitem>
  25. <para>
  26. <classname>Zend_Config_Writer_Ini</classname>
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. <classname>Zend_Config_Writer_Json</classname>
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. <classname>Zend_Config_Writer_Xml</classname>
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. <classname>Zend_Config_Writer_Yaml</classname>
  42. </para>
  43. </listitem>
  44. </itemizedlist>
  45. <para>
  46. When modifying or creating a <classname>Zend_Config</classname> object, there are
  47. some things to know. To create or modify a value, you simply say set
  48. the parameter of the <classname>Zend_Config</classname> object via the parameter
  49. accessor (<emphasis>-&gt;</emphasis>). To create a section in the root or to
  50. create a branch, you just create a new array
  51. ("<command>$config-&gt;branch = array();</command>"). To define which section
  52. extends another one, you call the <methodname>setExtend()</methodname> method
  53. on the root <classname>Zend_Config</classname> object.
  54. </para>
  55. <example id="zend.config.writer.example.using">
  56. <title>Using Zend_Config_Writer</title>
  57. <para>
  58. This example illustrates the basic use of
  59. <classname>Zend_Config_Writer_Xml</classname> to create a new config file:
  60. </para>
  61. <programlisting language="php"><![CDATA[
  62. // Create the config object
  63. $config = new Zend_Config(array(), true);
  64. $config->production = array();
  65. $config->staging = array();
  66. $config->setExtend('staging', 'production');
  67. $config->production->db = array();
  68. $config->production->db->hostname = 'localhost';
  69. $config->production->db->username = 'production';
  70. $config->staging->db = array();
  71. $config->staging->db->username = 'staging';
  72. // Write the config file in one of the following ways:
  73. // a)
  74. $writer = new Zend_Config_Writer_Xml(array('config' => $config,
  75. 'filename' => 'config.xml'));
  76. $writer->write();
  77. // b)
  78. $writer = new Zend_Config_Writer_Xml();
  79. $writer->setConfig($config)
  80. ->setFilename('config.xml')
  81. ->write();
  82. // c)
  83. $writer = new Zend_Config_Writer_Xml();
  84. $writer->write('config.xml', $config);
  85. ]]></programlisting>
  86. <para>
  87. This will create an <acronym>XML</acronym> config file with the sections production
  88. and staging, where staging extends production.
  89. </para>
  90. </example>
  91. <example id="zend.config.writer.modifying">
  92. <title>Modifying an Existing Config</title>
  93. <para>
  94. This example demonstrates how to edit an existing config file.
  95. </para>
  96. <programlisting language="php"><![CDATA[
  97. // Load all sections from an existing config file, while skipping the extends.
  98. $config = new Zend_Config_Ini('config.ini',
  99. null,
  100. array('skipExtends' => true,
  101. 'allowModifications' => true));
  102. // Modify a value
  103. $config->production->hostname = 'foobar';
  104. // Write the config file
  105. $writer = new Zend_Config_Writer_Ini(array('config' => $config,
  106. 'filename' => 'config.ini'));
  107. $writer->write();
  108. ]]></programlisting>
  109. </example>
  110. <note>
  111. <title>Loading a Config File</title>
  112. <para>
  113. When loading an existing config file for modifications it is very
  114. important to load all sections and to skip the extends, so that
  115. no values are merged. This is done by giving the
  116. <emphasis>skipExtends</emphasis> as option to the constructor.
  117. </para>
  118. </note>
  119. <para>
  120. For all the File-Based writers (<acronym>INI</acronym>, <acronym>JSON</acronym>,
  121. <acronym>XML</acronym>, <acronym>YAML</acronym>, and <acronym>PHP</acronym> Array)
  122. internally the <methodname>render()</methodname> is used to build the configuration string.
  123. This method can be used independently to access the string-representation of the
  124. configuration data.
  125. </para>
  126. <sect2 id="zend.config.writer.introduction.ini-notes">
  127. <title>Notes specific to the INI writer</title>
  128. <itemizedlist>
  129. <listitem>
  130. <para>
  131. The <acronym>INI</acronym> writer has two modes for rendering with regard to
  132. sections. By default the top-level configuration is always written into section
  133. names. By calling <command>$writer->setRenderWithoutSections();</command> all
  134. options are written into the global namespace of the <acronym>INI</acronym> file
  135. and no sections are applied.
  136. </para>
  137. </listitem>
  138. <listitem>
  139. <para>
  140. <classname>Zend_Config_Writer_Ini</classname> has an additional option parameter
  141. <emphasis>nestSeparator</emphasis>, which defines with which character the
  142. single nodes are separated. The default is a single dot, which is accepted by
  143. <classname>Zend_Config_Ini</classname> by default.
  144. </para>
  145. </listitem>
  146. </itemizedlist>
  147. </sect2>
  148. <sect2 id="zend.config.writer.introduction.yaml-notes">
  149. <title>Notes specific to the YAML writer</title>
  150. <para>
  151. The <acronym>YAML</acronym> writer lets you optionally specify an alternate
  152. <acronym>YAML</acronym> encoder to use. By default, one is shipped with the framework
  153. that is suitable for most configuration tasks. If you find it insufficient, or wish to
  154. use more advanced YAML, you may provide an alternate encoder callback.
  155. </para>
  156. <para>
  157. The method for doing so is to use the
  158. <methodname>Zend_Config_Writer_Yaml::setYamlEncoder()</methodname> method, passing it a
  159. valid callback.
  160. </para>
  161. <programlisting language="php"><![CDATA[
  162. // Use the Symfony Yaml Component:
  163. $writer = new Zend_Config_Writer_Yaml($filename);
  164. $writer->setYamlEncoder(array('sfYaml', 'dump'));
  165. ]]></programlisting>
  166. <para>
  167. The above uses the Symfony Components' <classname>sfYaml</classname> component in order
  168. to encode the configuration to <acronym>YAML</acronym>.
  169. </para>
  170. </sect2>
  171. </sect1>
  172. <!--
  173. vim:se ts=4 sw=4 et:
  174. -->