Zend_Config_Writer.xml 5.0 KB

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