Zend_Config_Yaml.xml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.config.adapters.yaml">
  4. <title>Zend_Config_Yaml</title>
  5. <sect2 id="zend.config.adapters.yaml.intro">
  6. <title>Overview</title>
  7. <para>
  8. <ulink url="http://www.yaml.org/">YAML</ulink> is a recursive acronym meaning "YAML
  9. Ain't Markup Language", and is intended as a "human friendly data serialization
  10. standard for all programming languages." It is often used for application configuration.
  11. </para>
  12. <para>
  13. <classname>Zend_Config_Yaml</classname> is a lightweight
  14. <classname>Zend_Config</classname> extension. It includes a parser capable of
  15. recognizing most common YAML syntax used for purposes of configuration, and allows
  16. specifying other parsers should you want more complex syntax (e.g., ext/syck, spyc,
  17. sfYaml, etc.).
  18. </para>
  19. </sect2>
  20. <sect2 id="zend.config.adapters.yaml.quick-start">
  21. <title>Quick Start</title>
  22. <para>
  23. The following is a YAML version of a standard application configuration.
  24. </para>
  25. <programlisting language="yaml"><![CDATA[
  26. production:
  27. phpSettings:
  28. display_startup_errors: false
  29. display_errors: false
  30. includePaths:
  31. library: APPLICATION_PATH/../library
  32. bootstrap:
  33. path: APPLICATION_PATH/Bootstrap.php
  34. class: "Bootstrap"
  35. appnamespace: "Application"
  36. resources:
  37. frontController:
  38. controllerDirectory: APPLICATION_PATH/controllers
  39. moduleDirectory: APPLICATION_PATH/modules
  40. params:
  41. displayExceptions: false
  42. modules:
  43. db:
  44. adapter: "pdo_sqlite"
  45. params:
  46. dbname: APPLICATION_PATH/../data/db/application.db
  47. layout:
  48. layoutPath: APPLICATION_PATH/layouts/scripts/
  49. staging:
  50. _extends: production
  51. testing:
  52. _extends: production
  53. phpSettings:
  54. display_startup_errors: true
  55. display_errors: true
  56. development:
  57. _extends: production
  58. phpSettings:
  59. display_startup_errors: true
  60. display_errors: true
  61. resources:
  62. frontController:
  63. params:
  64. displayExceptions: true
  65. ]]></programlisting>
  66. <para>
  67. To utilize it, you simply instantiate <classname>Zend_Config_Yaml</classname>, pointing
  68. it to the location of this file and indicating the section of the file to load. By
  69. default, constant names found in values will be substituted with their appropriate
  70. values.
  71. </para>
  72. <programlisting language="php"><![CDATA[
  73. $config = new Zend_Config_Yaml(
  74. APPLICATION_PATH . '/configs/application.yaml',
  75. APPLICATION_ENV
  76. );
  77. ]]></programlisting>
  78. <para>
  79. Once instantiated, you use it as you would any other configuration object.
  80. </para>
  81. <programlisting language="php"><![CDATA[
  82. $db = Zend_Db::factory($config->resources->db);
  83. ]]></programlisting>
  84. </sect2>
  85. <sect2 id="zend.config.adapters.yaml.options">
  86. <title>Configuration Options</title>
  87. <para>
  88. The following options may be passed as keys to the third, <varname>$options</varname>
  89. argument of the constructor.
  90. </para>
  91. <variablelist>
  92. <title>Zend_Config_Yaml Options</title>
  93. <varlistentry>
  94. <term>allow_modifications</term>
  95. <listitem>
  96. <para>
  97. The default behavior of <classname>Zend_Config</classname> is to mark the
  98. object as immutable once loaded. Passing this flag with a boolean
  99. <constant>true</constant> will enable modifications to the object.
  100. </para>
  101. </listitem>
  102. </varlistentry>
  103. <varlistentry>
  104. <term>skip_extends</term>
  105. <listitem>
  106. <para>
  107. By default, any time a section extends another,
  108. <classname>Zend_Config</classname> will merge the section with the section
  109. it extends. Speciying a boolean <constant>true</constant> value to this
  110. option will disable this feature, giving you only the configuration defined
  111. explicitly in that section.
  112. </para>
  113. </listitem>
  114. </varlistentry>
  115. <varlistentry>
  116. <term>ignore_constants</term>
  117. <listitem>
  118. <para>
  119. By default, <classname>Zend_Config_Yaml</classname> will replace constant
  120. names found in values with the defined constant value. You map pass a
  121. boolean <constant>true</constant> to this option to disable this
  122. functionality.
  123. </para>
  124. </listitem>
  125. </varlistentry>
  126. <varlistentry>
  127. <term>yaml_decoder</term>
  128. <listitem>
  129. <para>
  130. By default, <classname>Zend_Config_Yaml</classname> uses a built in decoder,
  131. <methodname>Zend_Config_Yaml::decode()</methodname>, to parse and process
  132. YAML files. You may specify an alternate callback to use in place of the
  133. built-in one using this option.
  134. </para>
  135. </listitem>
  136. </varlistentry>
  137. </variablelist>
  138. </sect2>
  139. <sect2 id="zend.config.adapters.yaml.methods">
  140. <title>Available Methods</title>
  141. <variablelist>
  142. <varlistentry id="zend.config.adapters.yaml.methods.constructor">
  143. <term>
  144. <methodsynopsis>
  145. <methodname>__construct</methodname>
  146. <methodparam>
  147. <funcparams>$yaml, $section = null, $options = false</funcparams>
  148. </methodparam>
  149. </methodsynopsis>
  150. </term>
  151. <listitem>
  152. <para>
  153. Constructor. <varname>$yaml</varname> should refer to a valid filesystem
  154. location containing a YAML configuration file. <varname>$section</varname>,
  155. if specified, indicates a specific section of the configuration file to use.
  156. <varname>$options</varname> is discussed in the <link
  157. linkend="zend.config.adapters.yaml.options">options section</link>.
  158. </para>
  159. </listitem>
  160. </varlistentry>
  161. <varlistentry id="zend.config.adapters.yaml.methods.decode">
  162. <term>
  163. <methodsynopsis>
  164. <methodname>decode</methodname>
  165. <methodparam>
  166. <funcparams>$yaml</funcparams>
  167. </methodparam>
  168. </methodsynopsis>
  169. </term>
  170. <listitem>
  171. <para>
  172. Parses a YAML string into a PHP array.
  173. </para>
  174. </listitem>
  175. </varlistentry>
  176. <varlistentry id="zend.config.adapters.yaml.methods.set-ignore-constants">
  177. <term>
  178. <methodsynopsis>
  179. <methodname>setIgnoreConstants</methodname>
  180. <methodparam>
  181. <funcparams>$flag</funcparams>
  182. </methodparam>
  183. </methodsynopsis>
  184. </term>
  185. <listitem>
  186. <para>
  187. This <emphasis>static</emphasis> function may be used to globally override
  188. the default settings for how constants found in YAML strings are handled. By
  189. default, constant names are replaced with the appropriate constant values;
  190. passing a boolean <constant>true</constant> value to this method will
  191. override that behavior. (You can override it per-instance via the
  192. <varname>ignore_constants</varname> option as well.)
  193. </para>
  194. </listitem>
  195. </varlistentry>
  196. <varlistentry id="zend.config.adapters.yaml.methods.ignore-constants">
  197. <term>
  198. <methodsynopsis>
  199. <methodname>ignoreConstants</methodname>
  200. <methodparam>
  201. <funcparams></funcparams>
  202. </methodparam>
  203. </methodsynopsis>
  204. </term>
  205. <listitem>
  206. <para>
  207. This <emphasis>static</emphasis> method gives you the current setting for
  208. the <varname>ignore_constants</varname> flag.
  209. </para>
  210. </listitem>
  211. </varlistentry>
  212. </variablelist>
  213. </sect2>
  214. <sect2 id="zend.config.adapters.yaml.examples">
  215. <title>Examples</title>
  216. <example id="zend.config.adapters.yaml.examples.sf-yaml">
  217. <title>Using Zend_Config_Yaml with sfYaml</title>
  218. <para>
  219. As noted in the <link linkend="zend.config.adapters.yaml.options">options
  220. section</link>, <classname>Zend_Config_Yaml</classname> allows you to specify an
  221. alternate YAML parser at instantiation.
  222. </para>
  223. <para>
  224. <ulink url="http://components.symfony-project.org/yaml/">sfYaml</ulink> is a <ulink
  225. url="http://components.symfony-project.org/">Symfony component</ulink> that
  226. implements a complete YAML parser in PHP, and includes a number of additional
  227. features including the ability to parse PHP expressions embedded in the YAML. In
  228. this example, we use the <methodname>sfYaml::load()</methodname> method as our YAML
  229. decoder callback. <emphasis>(Note: this assumes that the
  230. <classname>sfYaml</classname> class is either already loaded or available via
  231. autoloading.)</emphasis>
  232. </para>
  233. <programlisting language="php"><![CDATA[
  234. $config = new Zend_Config_Yaml(
  235. APPLICATION_PATH . '/configs/application.yaml',
  236. APPLICATION_ENV,
  237. array('yaml_decoder' => array('sfYaml', 'load'))
  238. );
  239. ]]></programlisting>
  240. </example>
  241. </sect2>
  242. </sect1>