Zend_Config_Writer.xml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24604 -->
  4. <sect1 id="zend.config.writer.introduction">
  5. <title>Zend_Config_Writer(日本語)</title>
  6. <para>
  7. <classname>Zend_Config_Writer</classname> は、設定ファイルに
  8. <classname>Zend_Config</classname> オブジェクトを書き出します。
  9. アダプタなしでも動作するので、使用するのも簡単です。
  10. デフォルトでは <classname>Zend_Config_Writer</classname>
  11. には 4 種類のアダプタが同梱されており、全てファイル・ベースです。
  12. まず、<emphasis>filename</emphasis> や <emphasis>config</emphasis>
  13. のオプションを指定してライターのインスタンスを作成します。
  14. それからライターの <methodname>write()</methodname> メソッドをコールすると、
  15. 設定ファイルが作成されます。<varname>$filename</varname> や
  16. <varname>$config</varname> を、直接 <methodname>write()</methodname>
  17. メソッドで設定することもできます。現在は、次のライターが
  18. <classname>Zend_Config_Writer</classname> に同梱されています。
  19. </para>
  20. <itemizedlist>
  21. <listitem>
  22. <para>
  23. <classname>Zend_Config_Writer_Array</classname>
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. <classname>Zend_Config_Writer_Ini</classname>
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. <classname>Zend_Config_Writer_Json</classname>
  34. </para>
  35. </listitem>
  36. <listitem>
  37. <para>
  38. <classname>Zend_Config_Writer_Xml</classname>
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. <classname>Zend_Config_Writer_Yaml</classname>
  44. </para>
  45. </listitem>
  46. </itemizedlist>
  47. <para>
  48. <classname>Zend_Config</classname> オブジェクトを変更したり作成したりする際には、
  49. 知っておくべきことがあります。値を作成したり変更したりするには、
  50. パラメータのアクセサ (<emphasis>-&gt;</emphasis>) で <classname>Zend_Config</classname>
  51. オブジェクトのパラメータを設定します。
  52. ルート内のセクションやブランチを作成するには、新規配列を作成します
  53. ("<command>$config-&gt;branch = array();</command>")。
  54. セクションの継承関係を定義するには、ルート
  55. <classname>Zend_Config</classname> オブジェクトの
  56. <methodname>setExtend()</methodname> メソッドをコールします。
  57. </para>
  58. <example id="zend.config.writer.example.using">
  59. <title>Zend_Config_Writer の使用法</title>
  60. <para>
  61. この例では、<classname>Zend_Config_Writer_Xml</classname>
  62. で新しい設定ファイルを作成する方法を説明します。
  63. </para>
  64. <programlisting language="php"><![CDATA[
  65. // config オブジェクトを作成します
  66. $config = new Zend_Config(array(), true);
  67. $config->production = array();
  68. $config->staging = array();
  69. $config->setExtend('staging', 'production');
  70. $config->production->db = array();
  71. $config->production->db->hostname = 'localhost';
  72. $config->production->db->username = 'production';
  73. $config->staging->db = array();
  74. $config->staging->db->username = 'staging';
  75. // 次のいずれかの方法で設定ファイルを書き出します
  76. // a)
  77. $writer = new Zend_Config_Writer_Xml(array('config' => $config,
  78. 'filename' => 'config.xml'));
  79. $writer->write();
  80. // b)
  81. $writer = new Zend_Config_Writer_Xml();
  82. $writer->setConfig($config)
  83. ->setFilename('config.xml')
  84. ->write();
  85. // c)
  86. $writer = new Zend_Config_Writer_Xml();
  87. $writer->write('config.xml', $config);
  88. ]]></programlisting>
  89. <para>
  90. これは、production と staging というセクションを持つ <acronym>XML</acronym>
  91. 設定ファイルを作成します。staging は production を継承しています。
  92. </para>
  93. </example>
  94. <example id="zend.config.writer.modifying">
  95. <title>既存の設定の変更</title>
  96. <para>
  97. この例では、既存の設定ファイルを編集する方法を説明します。
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. // すべてのセクションを既存の設定ファイルから読み込みますが継承は読み飛ばします
  101. $config = new Zend_Config_Ini('config.ini',
  102. null,
  103. array('skipExtends' => true,
  104. 'allowModifications' => true));
  105. // 値を変更します
  106. $config->production->hostname = 'foobar';
  107. // 設定ファイルを書き出します
  108. $writer = new Zend_Config_Writer_Ini(array('config' => $config,
  109. 'filename' => 'config.ini'));
  110. $writer->write();
  111. ]]></programlisting>
  112. </example>
  113. <note>
  114. <title>設定ファイルの読み込み</title>
  115. <para>
  116. 既存の設定ファイルを読み込んで変更をする場合は、
  117. すべてのセクションを読み込んで継承を読み飛ばすことが大切です。
  118. そうすることで、値がマージされてしまうことがなくなります。
  119. そのために、コンストラクタで
  120. <emphasis>skipExtends</emphasis> オプションを指定します。
  121. </para>
  122. </note>
  123. <para>
  124. 構成節の文字列を作成するために、ファイル・ベースのライタ
  125. ( <acronym>INI</acronym> 、 <acronym>JSON</acronym>、<acronym>XML</acronym>、
  126. <acronym>YAML</acronym> 及び <acronym>PHP</acronym> 配列)
  127. 全てで内部的に <methodname>render()</methodname> が使用されます。
  128. コンフィギュレーション・データの文字列表現にアクセスするために
  129. このメソッドをそれぞれ使用できます。
  130. </para>
  131. <sect2 id="zend.config.writer.introduction.ini-notes">
  132. <title>INI ライター特有の注意</title>
  133. <itemizedlist>
  134. <listitem>
  135. <para>
  136. <acronym>INI</acronym> ライターでは、セクションについてのレンダリング用のモードが2つあります。
  137. 既定では、トップレベルの構成節は、常にセクション名に含まれます。
  138. <command>$writer->setRenderWithoutSections()</command> を呼ぶことにより、
  139. オプションの全てが <acronym>INI</acronym> ファイルのグローバル名前空間に含まれます。
  140. そして、セクションは使用されません。
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <classname>Zend_Config_Writer_Ini</classname> にはオプションのパラメータ
  146. <emphasis>nestSeparator</emphasis> が用意されています。
  147. これは、ノードを区切る文字を定義します。デフォルトはドットひとつで、
  148. これは <classname>Zend_Config_Ini</classname> によりデフォルトで受け付けられます。
  149. </para>
  150. </listitem>
  151. </itemizedlist>
  152. </sect2>
  153. <sect2 id="zend.config.writer.introduction.yaml-notes">
  154. <title>YAML ライター特有の注意</title>
  155. <!-- TODO : to be translated -->
  156. <para>
  157. The <acronym>YAML</acronym> writer lets you optionally specify an alternate
  158. <acronym>YAML</acronym> encoder to use. By default, one is shipped with the framework
  159. that is suitable for most configuration tasks. If you find it insufficient, or wish to
  160. use more advanced YAML, you may provide an alternate encoder callback.
  161. </para>
  162. <para>
  163. The method for doing so is to use the
  164. <methodname>Zend_Config_Writer_Yaml::setYamlEncoder()</methodname> method, passing it a
  165. valid callback.
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. // Use the Symfony Yaml Component:
  169. $writer = new Zend_Config_Writer_Yaml($filename);
  170. $writer->setYamlEncoder(array('sfYaml', 'dump'));
  171. ]]></programlisting>
  172. <para>
  173. The above uses the Symfony Components' <classname>sfYaml</classname> component in order
  174. to encode the configuration to <acronym>YAML</acronym>.
  175. </para>
  176. </sect2>
  177. </sect1>
  178. <!--
  179. vim:se ts=4 sw=4 et:
  180. -->