File: Zend_Config_Yaml.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (280 lines) | stat: -rw-r--r-- 10,691 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.config.adapters.yaml">
    <title>Zend_Config_Yaml</title>

    <sect2 id="zend.config.adapters.yaml.intro">
        <title>Overview</title>

        <para>
            <ulink url="http://www.yaml.org/">YAML</ulink> is a recursive acronym meaning "YAML
            Ain't Markup Language", and is intended as a "human friendly data serialization
            standard for all programming languages." It is often used for application configuration.
        </para>

        <para>
            <classname>Zend_Config_Yaml</classname> is a lightweight
            <classname>Zend_Config</classname> extension. It includes a parser capable of
            recognizing most common YAML syntax used for purposes of configuration, and allows
            specifying other parsers should you want more complex syntax (e.g., ext/syck, spyc,
            sfYaml, etc.).
        </para>
    </sect2>

    <sect2 id="zend.config.adapters.yaml.quick-start">
        <title>Quick Start</title>

        <para>
            The following is a YAML version of a standard application configuration.
        </para>

        <programlisting language="yaml"><![CDATA[
production:
  phpSettings:
    display_startup_errors: false
    display_errors: false
  includePaths:
    library: APPLICATION_PATH/../library
  bootstrap:
    path: APPLICATION_PATH/Bootstrap.php
    class: "Bootstrap"
  appnamespace: "Application"
  resources:
    frontController:
      controllerDirectory: APPLICATION_PATH/controllers
      moduleDirectory: APPLICATION_PATH/modules
      params:
        displayExceptions: false
    modules:
    db:
      adapter: "pdo_sqlite"
      params:
        dbname: APPLICATION_PATH/../data/db/application.db
    layout:
      layoutPath: APPLICATION_PATH/layouts/scripts/

staging:
  _extends: production

testing:
  _extends: production
  phpSettings:
    display_startup_errors: true
    display_errors: true

development:
  _extends: production
  phpSettings:
    display_startup_errors: true
    display_errors: true
  resources:
    frontController:
      params:
        displayExceptions: true
]]></programlisting>

        <para>
            To utilize it, you simply instantiate <classname>Zend_Config_Yaml</classname>, pointing
            it to the location of this file and indicating the section of the file to load. By
            default, constant names found in values will be substituted with their appropriate
            values.
        </para>

        <programlisting language="php"><![CDATA[
$config = new Zend_Config_Yaml(
    APPLICATION_PATH . '/configs/application.yaml',
    APPLICATION_ENV
);
]]></programlisting>

        <para>
            Once instantiated, you use it as you would any other configuration object.
        </para>

        <programlisting language="php"><![CDATA[
$db = Zend_Db::factory($config->resources->db);
]]></programlisting>
    </sect2>

    <sect2 id="zend.config.adapters.yaml.options">
        <title>Configuration Options</title>

        <para>
            The following options may be passed as keys to the third, <varname>$options</varname>
            argument of the constructor.
        </para>

        <variablelist>
            <title>Zend_Config_Yaml Options</title>

            <varlistentry>
                <term>allow_modifications</term>

                <listitem>
                    <para>
                        The default behavior of <classname>Zend_Config</classname> is to mark the
                        object as immutable once loaded. Passing this flag with a boolean
                        <constant>true</constant> will enable modifications to the object.
                    </para>
                </listitem>
            </varlistentry>

            <varlistentry>
                <term>skip_extends</term>

                <listitem>
                    <para>
                        By default, any time a section extends another,
                        <classname>Zend_Config</classname> will merge the section with the section
                        it extends. Speciying a boolean <constant>true</constant> value to this
                        option will disable this feature, giving you only the configuration defined
                        explicitly in that section.
                    </para>
                </listitem>
            </varlistentry>

            <varlistentry>
                <term>ignore_constants</term>

                <listitem>
                    <para>
                        By default, <classname>Zend_Config_Yaml</classname> will replace constant
                        names found in values with the defined constant value. You map pass a
                        boolean <constant>true</constant> to this option to disable this
                        functionality.
                    </para>
                </listitem>
            </varlistentry>

            <varlistentry>
                <term>yaml_decoder</term>

                <listitem>
                    <para>
                        By default, <classname>Zend_Config_Yaml</classname> uses a built in decoder,
                        <methodname>Zend_Config_Yaml::decode()</methodname>, to parse and process
                        YAML files. You may specify an alternate callback to use in place of the
                        built-in one using this option.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </sect2>

    <sect2 id="zend.config.adapters.yaml.methods">
        <title>Available Methods</title>

        <variablelist>
            <varlistentry id="zend.config.adapters.yaml.methods.constructor">
                <term>
                    <methodsynopsis>
                        <methodname>__construct</methodname>
                        <methodparam>
                            <funcparams>$yaml, $section = null, $options = false</funcparams>
                        </methodparam>
                    </methodsynopsis>
                </term>

                <listitem>
                    <para>
                        Constructor. <varname>$yaml</varname> should refer to a valid filesystem
                        location containing a YAML configuration file. <varname>$section</varname>,
                        if specified, indicates a specific section of the configuration file to use.
                        <varname>$options</varname> is discussed in the <link
                            linkend="zend.config.adapters.yaml.options">options section</link>.
                    </para>
                </listitem>
            </varlistentry>

            <varlistentry id="zend.config.adapters.yaml.methods.decode">
                <term>
                    <methodsynopsis>
                        <methodname>decode</methodname>
                        <methodparam>
                            <funcparams>$yaml</funcparams>
                        </methodparam>
                    </methodsynopsis>
                </term>

                <listitem>
                    <para>
                        Parses a YAML string into a PHP array.
                    </para>
                </listitem>
            </varlistentry>

            <varlistentry id="zend.config.adapters.yaml.methods.set-ignore-constants">
                <term>
                    <methodsynopsis>
                        <methodname>setIgnoreConstants</methodname>
                        <methodparam>
                            <funcparams>$flag</funcparams>
                        </methodparam>
                    </methodsynopsis>
                </term>

                <listitem>
                    <para>
                        This <emphasis>static</emphasis> function may be used to globally override
                        the default settings for how constants found in YAML strings are handled. By
                        default, constant names are replaced with the appropriate constant values;
                        passing a boolean <constant>true</constant> value to this method will
                        override that behavior. (You can override it per-instance via the
                        <varname>ignore_constants</varname> option as well.)
                    </para>
                </listitem>
            </varlistentry>

            <varlistentry id="zend.config.adapters.yaml.methods.ignore-constants">
                <term>
                    <methodsynopsis>
                        <methodname>ignoreConstants</methodname>
                        <methodparam>
                            <funcparams></funcparams>
                        </methodparam>
                    </methodsynopsis>
                </term>

                <listitem>
                    <para>
                        This <emphasis>static</emphasis> method gives you the current setting for
                        the <varname>ignore_constants</varname> flag.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </sect2>

    <sect2 id="zend.config.adapters.yaml.examples">
        <title>Examples</title>

        <example id="zend.config.adapters.yaml.examples.sf-yaml">
            <title>Using Zend_Config_Yaml with sfYaml</title>

            <para>
                As noted in the <link linkend="zend.config.adapters.yaml.options">options
                    section</link>, <classname>Zend_Config_Yaml</classname> allows you to specify an
                alternate YAML parser at instantiation.
            </para>

            <para>
                <ulink url="http://components.symfony-project.org/yaml/">sfYaml</ulink> is a <ulink
                    url="http://components.symfony-project.org/">Symfony component</ulink> that
                implements a complete YAML parser in PHP, and includes a number of additional
                features including the ability to parse PHP expressions embedded in the YAML. In
                this example, we use the <methodname>sfYaml::load()</methodname> method as our YAML
                decoder callback. <emphasis>(Note: this assumes that the
                    <classname>sfYaml</classname> class is either already loaded or available via
                    autoloading.)</emphasis>
            </para>

            <programlisting language="php"><![CDATA[
$config = new Zend_Config_Yaml(
    APPLICATION_PATH . '/configs/application.yaml',
    APPLICATION_ENV,
    array('yaml_decoder' => array('sfYaml', 'load'))
);
]]></programlisting>
        </example>
    </sect2>
</sect1>