Zend_Config_Writer.xml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 all
  10. work the same. 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. As an exception, <classname>Zend_Config_Writer_Ini</classname> has an additional
  37. option parameter <emphasis>nestSeparator</emphasis>, which defines with which
  38. character the single nodes are separated. The default is a single dot,
  39. like it is accepted by <classname>Zend_Config_Ini</classname> by default.
  40. </para>
  41. <para>
  42. When modifying or creating a <classname>Zend_Config</classname> object, there are
  43. some things to know. To create or modify a value, you simply say set
  44. the parameter of the <classname>Zend_Config</classname> object via the parameter
  45. accessor (<emphasis>-&gt;</emphasis>). To create a section in the root or to
  46. create a branch, you just create a new array
  47. ("$config-&gt;branch = array();"). To define which section
  48. extends another one, you call the <methodname>setExtend()</methodname> method
  49. on the root <classname>Zend_Config</classname> object.
  50. </para>
  51. <example id="zend.config.writer.example.using">
  52. <title>Using Zend_Config_Writer</title>
  53. <para>
  54. This example illustrates the basic use of
  55. <classname>Zend_Config_Writer_Xml</classname> to create a new config file:
  56. </para>
  57. <programlisting language="php"><![CDATA[
  58. // Create the config object
  59. $config = new Zend_Config(array(), true);
  60. $config->production = array();
  61. $config->staging = array();
  62. $config->setExtend('staging', 'production');
  63. $config->production->db = array();
  64. $config->production->db->hostname = 'localhost';
  65. $config->production->db->username = 'production';
  66. $config->staging->db = array();
  67. $config->staging->db->username = 'staging';
  68. // Write the config file in one of the following ways:
  69. // a)
  70. $writer = new Zend_Config_Writer_Xml(array('config' => $config,
  71. 'filename' => 'config.xml'));
  72. $writer->write();
  73. // b)
  74. $writer = new Zend_Config_Writer_Xml();
  75. $writer->setConfig($config)
  76. ->setFilename('config.xml')
  77. ->write();
  78. // c)
  79. $writer = new Zend_Config_Writer_Xml();
  80. $writer->write('config.xml', $config);
  81. ]]></programlisting>
  82. <para>
  83. This will create an <acronym>XML</acronym> config file with the sections production
  84. and staging, where staging extends production.
  85. </para>
  86. </example>
  87. <example id="zend.config.writer.modifying">
  88. <title>Modifying an Existing Config</title>
  89. <para>
  90. This example demonstrates how to edit an existing config file.
  91. </para>
  92. <programlisting language="php"><![CDATA[
  93. // Load all sections from an existing config file, while skipping the extends.
  94. $config = new Zend_Config_Ini('config.ini',
  95. null,
  96. array('skipExtends' => true,
  97. 'allowModifications' => true));
  98. // Modify a value
  99. $config->production->hostname = 'foobar';
  100. // Write the config file
  101. $writer = new Zend_Config_Writer_Ini(array('config' => $config,
  102. 'filename' => 'config.ini'));
  103. $writer->write();
  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. <emphasis>skipExtends</emphasis> as option to the constructor.
  113. </para>
  114. </note>
  115. </sect1>
  116. <!--
  117. vim:se ts=4 sw=4 et:
  118. -->