Skip to content

Commit 9a0b4e1

Browse files
committed
[Config] Fixing up most of the Configuration examples for (mostly) missing children nodes - special thanks to @pierswarmers for pointing this out
1 parent 40e34d4 commit 9a0b4e1

File tree

1 file changed

+88
-65
lines changed

1 file changed

+88
-65
lines changed

components/config/definition.rst

+88-65
Original file line numberDiff line numberDiff line change
@@ -109,31 +109,33 @@ Array nodes
109109
~~~~~~~~~~~
110110

111111
It is possible to add a deeper level to the hierarchy, by adding an array
112-
node. The array node itself, may have a pre-defined set of variable nodes:
113-
114-
.. code-block:: php
112+
node. The array node itself, may have a pre-defined set of variable nodes::
115113

116114
$rootNode
117-
->arrayNode('connection')
118-
->scalarNode('driver')->end()
119-
->scalarNode('host')->end()
120-
->scalarNode('username')->end()
121-
->scalarNode('password')->end()
115+
->children()
116+
->arrayNode('connection')
117+
->children()
118+
->scalarNode('driver')->end()
119+
->scalarNode('host')->end()
120+
->scalarNode('username')->end()
121+
->scalarNode('password')->end()
122+
->end()
123+
->end()
122124
->end()
123125
;
124126

125-
Or you may define a prototype for each node inside an array node:
126-
127-
.. code-block:: php
127+
Or you may define a prototype for each node inside an array node::
128128

129129
$rootNode
130-
->arrayNode('connections')
131-
->prototype('array')
130+
->children()
131+
->arrayNode('connections')
132+
->prototype('array')
132133
->children()
133134
->scalarNode('driver')->end()
134135
->scalarNode('host')->end()
135136
->scalarNode('username')->end()
136137
->scalarNode('password')->end()
138+
->end()
137139
->end()
138140
->end()
139141
->end()
@@ -155,24 +157,36 @@ Before defining the children of an array node, you can provide options like:
155157
There should be at least one element in the array (works only when ``isRequired()`` is also
156158
called).
157159

158-
An example of this:
159-
160-
.. code-block:: php
160+
An example of this::
161161

162162
$rootNode
163-
->arrayNode('parameters')
164-
->isRequired()
165-
->requiresAtLeastOneElement()
166-
->useAttributeAsKey('name')
167-
->prototype('array')
168-
->children()
169-
->scalarNode('name')->isRequired()->end()
170-
->scalarNode('value')->isRequired()->end()
163+
->children()
164+
->arrayNode('parameters')
165+
->isRequired()
166+
->requiresAtLeastOneElement()
167+
->useAttributeAsKey('name')
168+
->prototype('array')
169+
->children()
170+
->scalarNode('value')->isRequired()->end()
171+
->end()
171172
->end()
172173
->end()
173174
->end()
174175
;
175176

177+
In YAML, the configuration might look like this:
178+
179+
.. code-block:: yaml
180+
181+
database:
182+
parameters:
183+
param1: { value: param1val }
184+
185+
In XML, each ``parameters`` node would have a ``name`` attribute (along with
186+
``value``), which would be removed and used as the key for that element in
187+
the final array. The ``useAttributeAsKey`` is useful for normalizing how
188+
arrays are specified between different formats like XML and YAML.
189+
176190
Default and required values
177191
---------------------------
178192

@@ -194,19 +208,21 @@ has a certain value:
194208
.. code-block:: php
195209
196210
$rootNode
197-
->arrayNode('connection')
198-
->children()
199-
->scalarNode('driver')
200-
->isRequired()
201-
->cannotBeEmpty()
202-
->end()
203-
->scalarNode('host')
204-
->defaultValue('localhost')
205-
->end()
206-
->scalarNode('username')->end()
207-
->scalarNode('password')->end()
208-
->booleanNode('memory')
209-
->defaultFalse()
211+
->children()
212+
->arrayNode('connection')
213+
->children()
214+
->scalarNode('driver')
215+
->isRequired()
216+
->cannotBeEmpty()
217+
->end()
218+
->scalarNode('host')
219+
->defaultValue('localhost')
220+
->end()
221+
->scalarNode('username')->end()
222+
->scalarNode('password')->end()
223+
->booleanNode('memory')
224+
->defaultFalse()
225+
->end()
210226
->end()
211227
->end()
212228
->end()
@@ -240,22 +256,24 @@ with ``append()``::
240256
$rootNode = $treeBuilder->root('database');
241257

242258
$rootNode
243-
->arrayNode('connection')
244-
->children()
245-
->scalarNode('driver')
246-
->isRequired()
247-
->cannotBeEmpty()
248-
->end()
249-
->scalarNode('host')
250-
->defaultValue('localhost')
251-
->end()
252-
->scalarNode('username')->end()
253-
->scalarNode('password')->end()
254-
->booleanNode('memory')
255-
->defaultFalse()
259+
->children()
260+
->arrayNode('connection')
261+
->children()
262+
->scalarNode('driver')
263+
->isRequired()
264+
->cannotBeEmpty()
265+
->end()
266+
->scalarNode('host')
267+
->defaultValue('localhost')
268+
->end()
269+
->scalarNode('username')->end()
270+
->scalarNode('password')->end()
271+
->booleanNode('memory')
272+
->defaultFalse()
273+
->end()
256274
->end()
275+
->append($this->addParametersNode())
257276
->end()
258-
->append($this->addParametersNode())
259277
->end()
260278
;
261279

@@ -273,7 +291,6 @@ with ``append()``::
273291
->useAttributeAsKey('name')
274292
->prototype('array')
275293
->children()
276-
->scalarNode('name')->isRequired()->end()
277294
->scalarNode('value')->isRequired()->end()
278295
->end()
279296
->end()
@@ -378,13 +395,17 @@ you can allow the following as well:
378395
By changing a string value into an associative array with ``name`` as the key::
379396

380397
$rootNode
381-
->arrayNode('connection')
382-
->beforeNormalization()
383-
->ifString()
384-
->then(function($v) { return array('name'=> $v); })
385-
->end()
386-
->scalarNode('name')->isRequired()
387-
// ...
398+
->children()
399+
->arrayNode('connection')
400+
->beforeNormalization()
401+
->ifString()
402+
->then(function($v) { return array('name'=> $v); })
403+
->end()
404+
->children()
405+
->scalarNode('name')->isRequired()
406+
// ...
407+
->end()
408+
->end()
388409
->end()
389410
;
390411

@@ -397,13 +418,15 @@ builder implements a fluent interface for a well-known control structure.
397418
The builder is used for adding advanced validation rules to node definitions, like::
398419

399420
$rootNode
400-
->arrayNode('connection')
401-
->children()
402-
->scalarNode('driver')
403-
->isRequired()
404-
->validate()
421+
->children()
422+
->arrayNode('connection')
423+
->children()
424+
->scalarNode('driver')
425+
->isRequired()
426+
->validate()
405427
->ifNotInArray(array('mysql', 'sqlite', 'mssql'))
406-
->thenInvalid('Invalid database driver "%s"')
428+
->thenInvalid('Invalid database driver "%s"')
429+
->end()
407430
->end()
408431
->end()
409432
->end()

0 commit comments

Comments
 (0)