Zend_Config_Xml.xml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.config.adapters.xml">
  4. <title>Zend_Config_Xml</title>
  5. <para>
  6. <classname>Zend_Config_Xml</classname> enables developers to store configuration data in a
  7. simple <acronym>XML</acronym> format and read them via nested object property syntax. The
  8. root element of the <acronym>XML</acronym> file or string is irrelevant and may be named
  9. arbitrarily. The first level of <acronym>XML</acronym> elements correspond with
  10. configuration data sections. The <acronym>XML</acronym> format supports hierarchical
  11. organization through nesting of <acronym>XML</acronym> elements below the section-level
  12. elements. The content of a leaf-level <acronym>XML</acronym> element corresponds to the
  13. value of a configuration datum. Section inheritance is supported by a special
  14. <acronym>XML</acronym> attribute named <emphasis>extends</emphasis>, and the value of this
  15. attribute corresponds with the section from which data are to be inherited by the extending
  16. section.
  17. </para>
  18. <note>
  19. <title>Return Type</title>
  20. <para>
  21. Configuration data read into <classname>Zend_Config_Xml</classname> are always returned
  22. as strings. Conversion of data from strings to other types is left to developers to
  23. suit their particular needs.
  24. </para>
  25. </note>
  26. <example id="zend.config.adapters.xml.example.using">
  27. <title>Using Zend_Config_Xml</title>
  28. <para>
  29. This example illustrates a basic use of <classname>Zend_Config_Xml</classname> for
  30. loading configuration data from an <acronym>XML</acronym> file. In this example there
  31. are configuration data for both a production system and for a staging system. Because
  32. the staging system configuration data are very similar to those for production, the
  33. staging section inherits from the production section. In this case, the decision is
  34. arbitrary and could have been written conversely, with the production section
  35. inheriting from the staging section, though this may not be the case for more complex
  36. situations. Suppose, then, that the following configuration data are contained in
  37. <filename>/path/to/config.xml</filename>:
  38. </para>
  39. <programlisting language="xml"><![CDATA[
  40. <?xml version="1.0"?>
  41. <configdata>
  42. <production>
  43. <webhost>www.example.com</webhost>
  44. <database>
  45. <adapter>pdo_mysql</adapter>
  46. <params>
  47. <host>db.example.com</host>
  48. <username>dbuser</username>
  49. <password>secret</password>
  50. <dbname>dbname</dbname>
  51. </params>
  52. </database>
  53. </production>
  54. <staging extends="production">
  55. <database>
  56. <params>
  57. <host>dev.example.com</host>
  58. <username>devuser</username>
  59. <password>devsecret</password>
  60. </params>
  61. </database>
  62. </staging>
  63. </configdata>
  64. ]]></programlisting>
  65. <para>
  66. Next, assume that the application developer needs the staging configuration data from
  67. the <acronym>XML</acronym> file. It is a simple matter to load these data by specifying
  68. the <acronym>XML</acronym> file and the staging section:
  69. </para>
  70. <programlisting language="php"><![CDATA[
  71. $config = new Zend_Config_Xml('/path/to/config.xml', 'staging');
  72. echo $config->database->params->host; // prints "dev.example.com"
  73. echo $config->database->params->dbname; // prints "dbname"
  74. ]]></programlisting>
  75. </example>
  76. <example id="zend.config.adapters.xml.example.attributes">
  77. <title>Using Tag Attributes in Zend_Config_Xml</title>
  78. <para>
  79. <classname>Zend_Config_Xml</classname> also supports two additional ways of defining
  80. nodes in the configuration. Both make use of attributes. Since the
  81. <emphasis>extends</emphasis> and the <emphasis>value</emphasis> attributes are reserved
  82. keywords (the latter one by the second way of using attributes), they may not be
  83. used. The first way of making usage of attributes is to add attributes in a parent
  84. node, which then will be translated into children of that node:
  85. </para>
  86. <programlisting language="xml"><![CDATA[
  87. <?xml version="1.0"?>
  88. <configdata>
  89. <production webhost="www.example.com">
  90. <database adapter="pdo_mysql">
  91. <params host="db.example.com" username="dbuser" password="secret"
  92. dbname="dbname"/>
  93. </database>
  94. </production>
  95. <staging extends="production">
  96. <database>
  97. <params host="dev.example.com" username="devuser"
  98. password="devsecret"/>
  99. </database>
  100. </staging>
  101. </configdata>
  102. ]]></programlisting>
  103. <para>
  104. The other way does not really shorten the config, but keeps it easier to maintain since
  105. you don't have to write the tag name twice. You simply create an empty tag with the
  106. value in the <emphasis>value</emphasis> attribute:
  107. </para>
  108. <programlisting language="xml"><![CDATA[
  109. <?xml version="1.0"?>
  110. <configdata>
  111. <production>
  112. <webhost>www.example.com</webhost>
  113. <database>
  114. <adapter value="pdo_mysql"/>
  115. <params>
  116. <host value="db.example.com"/>
  117. <username value="dbuser"/>
  118. <password value="secret"/>
  119. <dbname value="dbname"/>
  120. </params>
  121. </database>
  122. </production>
  123. <staging extends="production">
  124. <database>
  125. <params>
  126. <host value="dev.example.com"/>
  127. <username value="devuser"/>
  128. <password value="devsecret"/>
  129. </params>
  130. </database>
  131. </staging>
  132. </configdata>
  133. ]]></programlisting>
  134. </example>
  135. <note>
  136. <title>XML strings</title>
  137. <para>
  138. <classname>Zend_Config_Xml</classname> is able to load an <acronym>XML</acronym> string
  139. directly, such as that retrieved from a database. The string is passed
  140. as the first parameter to the constructor and must start with the
  141. characters <emphasis>'&lt;?xml'</emphasis>:
  142. </para>
  143. <programlisting language="xml"><![CDATA[
  144. $string = <<<EOT
  145. <?xml version="1.0"?>
  146. <config>
  147. <production>
  148. <db>
  149. <adapter value="pdo_mysql"/>
  150. <params>
  151. <host value="db.example.com"/>
  152. </params>
  153. </db>
  154. </production>
  155. <staging extends="production">
  156. <db>
  157. <params>
  158. <host value="dev.example.com"/>
  159. </params>
  160. </db>
  161. </staging>
  162. </config>
  163. EOT;
  164. $config = new Zend_Config_Xml($string, 'staging');
  165. ]]></programlisting>
  166. </note>
  167. <note>
  168. <title>Zend_Config XML namespace</title>
  169. <para>
  170. <classname>Zend_Config</classname> comes with it's own <acronym>XML</acronym>
  171. namespace, which adds additional functionality to the parsing process. To take advantage
  172. of it, you have to define a namespace with the namespace <acronym>URI</acronym>
  173. <filename>http://framework.zend.com/xml/zend-config-xml/1.0/</filename> in
  174. your config root node.
  175. </para>
  176. <para>
  177. With the namespace enabled, you can now use <acronym>PHP</acronym> constants within
  178. your configuration files. Additionally, the <emphasis>extends</emphasis>
  179. attribute was moved to the new namespace and is deprecated in the
  180. <constant>NULL</constant> namespace. It will be completely removed there in
  181. Zend Framework 2.0.
  182. </para>
  183. <programlisting language="xml"><![CDATA[
  184. $string = <<<EOT
  185. <?xml version="1.0"?>
  186. <config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
  187. <production>
  188. <includePath>
  189. <zf:const zf:name="APPLICATION_PATH"/>/library</includePath>
  190. <db>
  191. <adapter value="pdo_mysql"/>
  192. <params>
  193. <host value="db.example.com"/>
  194. </params>
  195. </db>
  196. </production>
  197. <staging zf:extends="production">
  198. <db>
  199. <params>
  200. <host value="dev.example.com"/>
  201. </params>
  202. </db>
  203. </staging>
  204. </config>
  205. EOT;
  206. define('APPLICATION_PATH', dirname(__FILE__));
  207. $config = new Zend_Config_Xml($string, 'staging');
  208. echo $config->includePath; // Prints "/var/www/something/library"
  209. ]]></programlisting>
  210. </note>
  211. </sect1>
  212. <!--
  213. vim:se ts=4 sw=4 et:
  214. -->