Zend_Config_Writer.xml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 three 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_Xml</classname>
  32. </para>
  33. </listitem>
  34. </itemizedlist>
  35. <para>
  36. The <acronym>INI</acronym> writer has two modes for rendering with regard to sections.
  37. By default the top-level configuration is always written into section names.
  38. By calling <command>$writer->setRenderWithoutSections();</command> all options are written
  39. into the global namespace of the <acronym>INI</acronym> file and no sections are applied.
  40. </para>
  41. <para>
  42. As an addition <classname>Zend_Config_Writer_Ini</classname> has an additional
  43. option parameter <emphasis>nestSeparator</emphasis>, which defines with which
  44. character the single nodes are separated. The default is a single dot,
  45. like it is accepted by <classname>Zend_Config_Ini</classname> by default.
  46. </para>
  47. <para>
  48. When modifying or creating a <classname>Zend_Config</classname> object, there are
  49. some things to know. To create or modify a value, you simply say set
  50. the parameter of the <classname>Zend_Config</classname> object via the parameter
  51. accessor (<emphasis>-&gt;</emphasis>). To create a section in the root or to
  52. create a branch, you just create a new array
  53. ("<command>$config-&gt;branch = array();</command>"). To define which section
  54. extends another one, you call the <methodname>setExtend()</methodname> method
  55. on the root <classname>Zend_Config</classname> object.
  56. </para>
  57. <example id="zend.config.writer.example.using">
  58. <title>Using Zend_Config_Writer</title>
  59. <para>
  60. This example illustrates the basic use of
  61. <classname>Zend_Config_Writer_Xml</classname> to create a new config file:
  62. </para>
  63. <programlisting language="php"><![CDATA[
  64. // Create the config object
  65. $config = new Zend_Config(array(), true);
  66. $config->production = array();
  67. $config->staging = array();
  68. $config->setExtend('staging', 'production');
  69. $config->production->db = array();
  70. $config->production->db->hostname = 'localhost';
  71. $config->production->db->username = 'production';
  72. $config->staging->db = array();
  73. $config->staging->db->username = 'staging';
  74. // Write the config file in one of the following ways:
  75. // a)
  76. $writer = new Zend_Config_Writer_Xml(array('config' => $config,
  77. 'filename' => 'config.xml'));
  78. $writer->write();
  79. // b)
  80. $writer = new Zend_Config_Writer_Xml();
  81. $writer->setConfig($config)
  82. ->setFilename('config.xml')
  83. ->write();
  84. // c)
  85. $writer = new Zend_Config_Writer_Xml();
  86. $writer->write('config.xml', $config);
  87. ]]></programlisting>
  88. <para>
  89. This will create an <acronym>XML</acronym> config file with the sections production
  90. and staging, where staging extends production.
  91. </para>
  92. </example>
  93. <example id="zend.config.writer.modifying">
  94. <title>Modifying an Existing Config</title>
  95. <para>
  96. This example demonstrates how to edit an existing config file.
  97. </para>
  98. <programlisting language="php"><![CDATA[
  99. // Load all sections from an existing config file, while skipping the extends.
  100. $config = new Zend_Config_Ini('config.ini',
  101. null,
  102. array('skipExtends' => true,
  103. 'allowModifications' => true));
  104. // Modify a value
  105. $config->production->hostname = 'foobar';
  106. // Write the config file
  107. $writer = new Zend_Config_Writer_Ini(array('config' => $config,
  108. 'filename' => 'config.ini'));
  109. $writer->write();
  110. ]]></programlisting>
  111. </example>
  112. <note>
  113. <title>Loading a Config File</title>
  114. <para>
  115. When loading an existing config file for modifications it is very
  116. important to load all sections and to skip the extends, so that
  117. no values are merged. This is done by giving the
  118. <emphasis>skipExtends</emphasis> as option to the constructor.
  119. </para>
  120. </note>
  121. <para>
  122. For all the File-Based writers (<acronym>INI</acronym>, <acronym>XML</acronym> and
  123. <acronym>PHP</acronym> Array) internally the <methodname>render()</methodname> is used to
  124. build the configuration string. This method can be used from the outside also if you need
  125. to access the string-representation of the configuration data.
  126. </para>
  127. </sect1>
  128. <!--
  129. vim:se ts=4 sw=4 et:
  130. -->