Zend_Config_Json.xml 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.config.adapters.json">
  4. <title>Zend_Config_Json</title>
  5. <sect2 id="zend.config.adapters.json.intro">
  6. <title>Overview</title>
  7. <para>
  8. <ulink url="http://www.json.org/">JSON</ulink> is an acronym for "JavaScript Object
  9. Notation"; while compatible with JavaScript, it is also intended as a general-purpose,
  10. cross-language data interchange format. <classname>Zend_Config_Json</classname> is a
  11. lightweight <classname>Zend_Config</classname> extension using <acronym>JSON</acronym>
  12. as its serialization format.
  13. </para>
  14. </sect2>
  15. <sect2 id="zend.config.adapters.json.quick-start">
  16. <title>Quick Start</title>
  17. <para>
  18. The following is a <acronym>JSON</acronym> version of a standard application configuration.
  19. </para>
  20. <programlisting language="json"><![CDATA[
  21. {
  22. "production":{
  23. "phpSettings":{
  24. "display_startup_errors": false,
  25. "display_errors": false
  26. },
  27. "includePaths":{
  28. "library": "APPLICATION_PATH/../library"
  29. },
  30. "bootstrap":{
  31. "path": "APPLICATION_PATH/Bootstrap.php",
  32. "class": "Bootstrap"
  33. },
  34. "appnamespace": "Application",,
  35. "resources":{
  36. "frontController":{
  37. "controllerDirectory": "APPLICATION_PATH/controllers",
  38. "moduleDirectory": "APPLICATION_PATH/modules",
  39. "params":{
  40. "displayExceptions": false
  41. }
  42. },
  43. "modules":[],
  44. "db":{
  45. "adapter": "pdo_sqlite",
  46. "params":{
  47. "dbname": "APPLICATION_PATH/../data/db/application.db"
  48. }
  49. },
  50. "layout":{
  51. "layoutPath": "APPLICATION_PATH/layouts/scripts/"
  52. }
  53. }
  54. },
  55. "staging":{
  56. "_extends": "production"
  57. },
  58. "testing":{
  59. "_extends": "production",
  60. "phpSettings":{
  61. "display_startup_errors": true,
  62. "display_errors": true
  63. },
  64. },
  65. "development":{
  66. "_extends": "production",
  67. "phpSettings":{
  68. "display_startup_errors": true,
  69. "display_errors": true
  70. },
  71. "resources":{
  72. "frontController":{
  73. "params":{
  74. "displayExceptions": true
  75. }
  76. }
  77. }
  78. }
  79. }
  80. ]]></programlisting>
  81. <para>
  82. To utilize it, you simply instantiate <classname>Zend_Config_Json</classname>, pointing
  83. it to the location of this file and indicating the section of the file to load. By
  84. default, constant names found in values will be substituted with their appropriate
  85. values.
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. $config = new Zend_Config_Json(
  89. APPLICATION_PATH . '/configs/application.json',
  90. APPLICATION_ENV
  91. );
  92. ]]></programlisting>
  93. <para>
  94. Once instantiated, you use it as you would any other configuration object.
  95. </para>
  96. <programlisting language="php"><![CDATA[
  97. $db = Zend_Db::factory($config->resources->db);
  98. ]]></programlisting>
  99. <warning>
  100. <title>Use Constants With Care</title>
  101. <para>
  102. <acronym>JSON</acronym> has a strict structure with regards to data types. As such,
  103. you need to ensure that your constants are use correctly. For constants that have
  104. string values, put your constant values in double quotes (""). For non-string
  105. values, you can omit the quotes -- but be absolutely certain that they are not
  106. returning strings, as otherwise you will encounter parser errors with your
  107. configuration file. When in doubt, enclose the contant in double quotes.
  108. </para>
  109. </warning>
  110. </sect2>
  111. <sect2 id="zend.config.adapters.json.options">
  112. <title>Configuration Options</title>
  113. <para>
  114. The following options may be passed as keys to the third, <varname>$options</varname>
  115. argument of the constructor.
  116. </para>
  117. <variablelist>
  118. <title>Zend_Config_Json Options</title>
  119. <varlistentry>
  120. <term>allow_modifications/allowModifications</term>
  121. <listitem>
  122. <para>
  123. The default behavior of <classname>Zend_Config</classname> is to mark the
  124. object as immutable once loaded. Passing this flag with a boolean
  125. <constant>true</constant> will enable modifications to the object.
  126. </para>
  127. </listitem>
  128. </varlistentry>
  129. <varlistentry>
  130. <term>skip_extends/skipExtends</term>
  131. <listitem>
  132. <para>
  133. By default, any time a section extends another,
  134. <classname>Zend_Config</classname> will merge the section with the section
  135. it extends. Speciying a boolean <constant>true</constant> value to this
  136. option will disable this feature, giving you only the configuration defined
  137. explicitly in that section.
  138. </para>
  139. </listitem>
  140. </varlistentry>
  141. <varlistentry>
  142. <term>ignore_constants</term>
  143. <listitem>
  144. <para>
  145. By default, <classname>Zend_Config_Json</classname> will replace constant
  146. names found in values with the defined constant value. You map pass a
  147. boolean <constant>true</constant> to this option to disable this
  148. functionality.
  149. </para>
  150. <para>
  151. Please note that ignoring constants can potentially lead to parse errors,
  152. particularly if you are using constants for integer, float, or boolean
  153. values. The safest practice is to enclose constants within quotes.
  154. </para>
  155. </listitem>
  156. </varlistentry>
  157. </variablelist>
  158. </sect2>
  159. <sect2 id="zend.config.adapters.json.methods">
  160. <title>Available Methods</title>
  161. <variablelist>
  162. <varlistentry id="zend.config.adapters.json.methods.constructor">
  163. <term>
  164. <methodsynopsis>
  165. <methodname>__construct</methodname>
  166. <methodparam>
  167. <funcparams>$json, $section = null, $options = false</funcparams>
  168. </methodparam>
  169. </methodsynopsis>
  170. </term>
  171. <listitem>
  172. <para>
  173. Constructor. <varname>$json</varname> should be either a valid
  174. <acronym>JSON</acronym> string, or refer to a valid filesystem location
  175. containing a <acronym>JSON</acronym> configuration file.
  176. <varname>$section</varname>, if specified, indicates a specific section of
  177. the configuration file to use. <varname>$options</varname> is discussed in
  178. the <link linkend="zend.config.adapters.json.options">options
  179. section</link>.
  180. </para>
  181. </listitem>
  182. </varlistentry>
  183. <varlistentry id="zend.config.adapters.json.methods.set-ignore-constants">
  184. <term>
  185. <methodsynopsis>
  186. <methodname>setIgnoreConstants</methodname>
  187. <methodparam>
  188. <funcparams>$flag</funcparams>
  189. </methodparam>
  190. </methodsynopsis>
  191. </term>
  192. <listitem>
  193. <para>
  194. This <emphasis>static</emphasis> function may be used to globally override
  195. the default settings for how constants found in <acronym>JSON</acronym>
  196. strings are handled. By default, constant names are replaced with the
  197. appropriate constant values; passing a boolean <constant>true</constant>
  198. value to this method will override that behavior. (You can override it
  199. per-instance via the <varname>ignore_constants</varname> option as well.)
  200. </para>
  201. </listitem>
  202. </varlistentry>
  203. <varlistentry id="zend.config.adapters.json.methods.ignore-constants">
  204. <term>
  205. <methodsynopsis>
  206. <methodname>ignoreConstants</methodname>
  207. <methodparam>
  208. <funcparams></funcparams>
  209. </methodparam>
  210. </methodsynopsis>
  211. </term>
  212. <listitem>
  213. <para>
  214. This <emphasis>static</emphasis> method gives you the current setting for
  215. the <varname>ignore_constants</varname> flag.
  216. </para>
  217. </listitem>
  218. </varlistentry>
  219. </variablelist>
  220. </sect2>
  221. </sect1>