Zend_Config_Writer.xml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <sect1 id="zend.config.writer.introduction">
  2. <title>Zend_Config_Writer</title>
  3. <para>
  4. <code>Zend_Config_Writer</code> gives you the ability to write config
  5. files out of <code>Zend_Config</code> objects. It works with an
  6. adapter-less system and thus is very easy to use. By default
  7. <code>Zend_Config_Writer</code> ships with three adapters, which all
  8. work the same. You instantiate a writer with specific options, which
  9. can be <code>filename</code> and <code>config</code>. Then
  10. you call the <code>write()</code> method of the writer and the config
  11. file is created. You can also give <code>$filename</code> and
  12. <code>$config</code> directly to the <code>write()</code> method.
  13. Currently the following writers are shipped with
  14. <code>Zend_Config_Writer</code>:
  15. </para>
  16. <itemizedlist>
  17. <listitem>
  18. <para>
  19. <code>Zend_Config_Writer_Array</code>
  20. </para>
  21. </listitem>
  22. <listitem>
  23. <para>
  24. <code>Zend_Config_Writer_Ini</code>
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <code>Zend_Config_Writer_Xml</code>
  30. </para>
  31. </listitem>
  32. </itemizedlist>
  33. <para>
  34. As an exception, <code>Zend_Config_Writer_Ini</code> has an additional
  35. option parameter <code>nestSeparator</code>, which defines with which
  36. character the single nodes are separated. The default is a single dot,
  37. like it is accepted by <code>Zend_Config_Ini</code> by default.
  38. </para>
  39. <para>
  40. When modifying or creating a <code>Zend_Config</code> object, there are
  41. some things to know. To create or modify a value, you simply say set
  42. the parameter of the <code>Zend_Config</code> object via the parameter
  43. accessor (<code>-&gt;</code>). To create a section in the root or to
  44. create a branch, you just create a new array
  45. (<code>$config-&gt;branch = array();</code>). To define which section
  46. extends another one, you call the <code>setExtend()</code> method
  47. on the root <code>Zend_Config</code> object.
  48. </para>
  49. <example id="zend.config.writer.example.using">
  50. <title>Using Zend_Config_Writer</title>
  51. <para>
  52. This example illustrates the basic use of
  53. <code>Zend_Config_Writer_Xml</code> to create a new config file:
  54. </para>
  55. <programlisting role="php"><![CDATA[
  56. // Create the config object
  57. $config = new Zend_Config(array(), true);
  58. $config->production = array();
  59. $config->staging = array();
  60. $config->setExtend('staging', 'production');
  61. $config->production->db = array();
  62. $config->production->db->hostname = 'localhost';
  63. $config->production->db->username = 'production';
  64. $config->staging->db = array();
  65. $config->staging->db->username = 'staging';
  66. // Write the config file in one of the following ways:
  67. // a)
  68. $writer = new Zend_Config_Writer_Xml(array('config' => $config,
  69. 'filename' => 'config.xml'));
  70. $writer->write();
  71. // b)
  72. $writer = new Zend_Config_Writer_Xml();
  73. $writer->setConfig($config)
  74. ->setFilename('config.xml')
  75. ->write();
  76. // c)
  77. $writer = new Zend_Config_Writer_Xml();
  78. $writer->write('config.xml', $config);
  79. ]]>
  80. </programlisting>
  81. <para>
  82. This will create an XML config file with the sections production
  83. and staging, where staging extends production.
  84. </para>
  85. </example>
  86. <example id="zend.config.writer.modifying">
  87. <title>Modifying an existing config</title>
  88. <para>
  89. This example demonstrates how to edit an existing config file.
  90. </para>
  91. <programlisting role="php"><![CDATA[
  92. // Load all sections from an existing config file, while skipping the extends.
  93. $config = new Zend_Config_Ini('config.ini',
  94. null,
  95. array('skipExtends' => true,
  96. 'allowModifications' => true));
  97. // Modify a value
  98. $config->production->hostname = 'foobar';
  99. // Write the config file
  100. $writer = new Zend_Config_Writer_Ini(array('config' => $config,
  101. 'filename' => 'config.ini'));
  102. $writer->write();
  103. ]]>
  104. </programlisting>
  105. </example>
  106. <note>
  107. <title>Loading a config file</title>
  108. <para>
  109. When loading an existing config file for modifications it is very
  110. important to load all sections and to skip the extends, so that
  111. no values are merged. This is done by giving the
  112. <code>skipExtends</code> as option to the constructor.
  113. </para>
  114. </note>
  115. </sect1>
  116. <!--
  117. vim:se ts=4 sw=4 et:
  118. -->