File: Zend_Config_Xml.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 (238 lines) | stat: -rw-r--r-- 8,806 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
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.config.adapters.xml">
    <title>Zend_Config_Xml</title>

    <para>
        <classname>Zend_Config_Xml</classname> enables developers to store configuration data in a
        simple <acronym>XML</acronym> format and read them via nested object property syntax. The
        root element of the <acronym>XML</acronym> file or string is irrelevant and may be named
        arbitrarily. The first level of <acronym>XML</acronym> elements correspond with
        configuration data sections. The <acronym>XML</acronym> format supports hierarchical
        organization through nesting of <acronym>XML</acronym> elements below the section-level
        elements. The content of a leaf-level <acronym>XML</acronym> element corresponds to the
        value of a configuration datum. Section inheritance is supported by a special
        <acronym>XML</acronym> attribute named <emphasis>extends</emphasis>, and the value of this
        attribute corresponds with the section from which data are to be inherited by the extending
        section.
    </para>

    <note>
        <title>Return Type</title>

        <para>
            Configuration data read into <classname>Zend_Config_Xml</classname> are always returned
            as strings. Conversion of data from strings to other types is left to developers to
            suit their particular needs.
        </para>
    </note>

    <example id="zend.config.adapters.xml.example.using">
        <title>Using Zend_Config_Xml</title>

        <para>
            This example illustrates a basic use of <classname>Zend_Config_Xml</classname> for
            loading configuration data from an <acronym>XML</acronym> file. In this example there
            are configuration data for both a production system and for a staging system. Because
            the staging system configuration data are very similar to those for production, the
            staging section inherits from the production section. In this case, the decision is
            arbitrary and could have been written conversely, with the production section
            inheriting from the staging section, though this may not be the case for more complex
            situations. Suppose, then, that the following configuration data are contained in
            <filename>/path/to/config.xml</filename>:
        </para>

        <programlisting language="xml"><![CDATA[
<?xml version="1.0"?>
<configdata>
    <production>
        <webhost>www.example.com</webhost>
        <database>
            <adapter>pdo_mysql</adapter>
            <params>
                <host>db.example.com</host>
                <username>dbuser</username>
                <password>secret</password>
                <dbname>dbname</dbname>
            </params>
        </database>
    </production>
    <staging extends="production">
        <database>
            <params>
                <host>dev.example.com</host>
                <username>devuser</username>
                <password>devsecret</password>
            </params>
        </database>
    </staging>
</configdata>
]]></programlisting>

        <para>
            Next, assume that the application developer needs the staging configuration data from
            the <acronym>XML</acronym> file. It is a simple matter to load these data by specifying
            the <acronym>XML</acronym> file and the staging section:
        </para>

        <programlisting language="php"><![CDATA[
$config = new Zend_Config_Xml('/path/to/config.xml', 'staging');

echo $config->database->params->host;   // prints "dev.example.com"
echo $config->database->params->dbname; // prints "dbname"
]]></programlisting>
    </example>

    <example id="zend.config.adapters.xml.example.attributes">
        <title>Using Tag Attributes in Zend_Config_Xml</title>

        <para>
            <classname>Zend_Config_Xml</classname> also supports two additional ways of defining
            nodes in the configuration. Both make use of attributes. Since the
            <emphasis>extends</emphasis> and the <emphasis>value</emphasis> attributes are reserved
            keywords (the latter one by the second way of using attributes), they may not be
            used. The first way of making usage of attributes is to add attributes in a parent
            node, which then will be translated into children of that node:
        </para>

        <programlisting language="xml"><![CDATA[
<?xml version="1.0"?>
<configdata>
    <production webhost="www.example.com">
        <database adapter="pdo_mysql">
            <params host="db.example.com" username="dbuser" password="secret"
                    dbname="dbname"/>
        </database>
    </production>
    <staging extends="production">
        <database>
            <params host="dev.example.com" username="devuser"
                    password="devsecret"/>
        </database>
    </staging>
</configdata>
]]></programlisting>

        <para>
            The other way does not really shorten the config, but keeps it easier to maintain since
            you don't have to write the tag name twice. You simply create an empty tag with the
            value in the <emphasis>value</emphasis> attribute:
        </para>

        <programlisting language="xml"><![CDATA[
<?xml version="1.0"?>
<configdata>
    <production>
        <webhost>www.example.com</webhost>
        <database>
            <adapter value="pdo_mysql"/>
            <params>
                <host value="db.example.com"/>
                <username value="dbuser"/>
                <password value="secret"/>
                <dbname value="dbname"/>
            </params>
        </database>
    </production>
    <staging extends="production">
        <database>
            <params>
                <host value="dev.example.com"/>
                <username value="devuser"/>
                <password value="devsecret"/>
            </params>
        </database>
    </staging>
</configdata>
]]></programlisting>
    </example>

    <note>
        <title>XML strings</title>

        <para>
            <classname>Zend_Config_Xml</classname> is able to load an <acronym>XML</acronym> string
            directly, such as that retrieved from a database. The string is passed
            as the first parameter to the constructor and must start with the
            characters <emphasis>'&lt;?xml'</emphasis>:
        </para>

        <programlisting language="xml"><![CDATA[
$string = <<<EOT
<?xml version="1.0"?>
<config>
    <production>
        <db>
            <adapter value="pdo_mysql"/>
            <params>
                <host value="db.example.com"/>
            </params>
        </db>
    </production>
    <staging extends="production">
        <db>
            <params>
                <host value="dev.example.com"/>
            </params>
        </db>
    </staging>
</config>
EOT;

$config = new Zend_Config_Xml($string, 'staging');
]]></programlisting>
    </note>

    <note>
        <title>Zend_Config XML namespace</title>

        <para>
            <classname>Zend_Config</classname> comes with it's own <acronym>XML</acronym>
            namespace, which adds additional functionality to the parsing process. To take advantage
            of it, you have to define a namespace with the namespace <acronym>URI</acronym>
            <filename>http://framework.zend.com/xml/zend-config-xml/1.0/</filename> in
            your config root node.
        </para>

        <para>
            With the namespace enabled, you can now use <acronym>PHP</acronym> constants within
            your configuration files. Additionally, the <emphasis>extends</emphasis>
            attribute was moved to the new namespace and is deprecated in the
            <constant>NULL</constant> namespace. It will be completely removed there in
            Zend Framework 2.0.
        </para>

        <programlisting language="xml"><![CDATA[
$string = <<<EOT
<?xml version="1.0"?>
<config xmlns:zf="http://framework.zend.com/xml/zend-config-xml/1.0/">
    <production>
        <includePath>
            <zf:const zf:name="APPLICATION_PATH"/>/library</includePath>
        <db>
            <adapter value="pdo_mysql"/>
            <params>
                <host value="db.example.com"/>
            </params>
        </db>
    </production>
    <staging zf:extends="production">
        <db>
            <params>
                <host value="dev.example.com"/>
            </params>
        </db>
    </staging>
</config>
EOT;

define('APPLICATION_PATH', dirname(__FILE__));
$config = new Zend_Config_Xml($string, 'staging');

echo $config->includePath; // Prints "/var/www/something/library"
]]></programlisting>
    </note>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->