@@ -177,21 +177,12 @@ override this default. You don't need to configure ``username`` as an optional
177
177
option. The ``OptionsResolver `` already knows that options with a default
178
178
value are optional.
179
179
180
- The OptionsResolver component also has an
181
- :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::replaceDefaults `
182
- method. This can be used to override the previous default value. The closure
183
- that is passed has 2 parameters:
184
-
185
- * ``$options `` (an :class: `Symfony\\ Component\\ OptionsResolver\\ Options `
186
- instance), with all the default options
187
- * ``$value ``, the previous set default value
188
-
189
180
Default Values that depend on another Option
190
181
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191
182
192
183
Suppose you add a ``port `` option to the ``Mailer `` class, whose default
193
184
value you guess based on the host. You can do that easily by using a
194
- Closure as the default value::
185
+ closure as the default value::
195
186
196
187
use Symfony\Component\OptionsResolver\Options;
197
188
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
@@ -214,9 +205,83 @@ Closure as the default value::
214
205
215
206
.. caution ::
216
207
217
- The first argument of the Closure must be typehinted as ``Options ``,
208
+ The first argument of the closure must be typehinted as ``Options ``,
218
209
otherwise it is considered as the value.
219
210
211
+ Overwriting Default Values
212
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
213
+
214
+ A previously set default value can be overwritten by invoking
215
+ :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::setDefaults `
216
+ again. When using a closure as the new value it is passed 2 arguments:
217
+
218
+ * ``$options ``: an :class: `Symfony\\ Component\\ OptionsResolver\\ Options `
219
+ instance with all the other default options
220
+ * ``$previousValue ``: the previous set default value
221
+
222
+ .. code-block :: php
223
+
224
+ use Symfony\Component\OptionsResolver\Options;
225
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
226
+
227
+ // ...
228
+ protected function setDefaultOptions(OptionsResolverInterface $resolver)
229
+ {
230
+ // ...
231
+ $resolver->setDefaults(array(
232
+ 'encryption' => 'ssl',
233
+ 'host' => 'localhost',
234
+ ));
235
+
236
+ // ...
237
+ $resolver->setDefaults(array(
238
+ 'encryption' => 'tls', // simple overwrite
239
+ 'host' => function (Options $options, $previousValue) {
240
+ return 'localhost' == $previousValue ? '127.0.0.1' : $previousValue;
241
+ },
242
+ ));
243
+ }
244
+
245
+ .. tip ::
246
+
247
+ If the previous default value is calculated by an expensive closure and
248
+ you don't need access to it, you can use the
249
+ :method: `Symfony\\ Component\\ OptionsResolver\\ OptionsResolver::replaceDefaults `
250
+ method instead. It acts like ``setDefaults `` but simply erases the
251
+ previous value to improve performance. This means that the previous
252
+ default value is not available when overwriting with another closure::
253
+
254
+ use Symfony\Component\OptionsResolver\Options;
255
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
256
+
257
+ // ...
258
+ protected function setDefaultOptions(OptionsResolverInterface $resolver)
259
+ {
260
+ // ...
261
+ $resolver->setDefaults(array(
262
+ 'encryption' => 'ssl',
263
+ 'heavy' => function (Options $options) {
264
+ // Some heavy calculations to create the $result
265
+
266
+ return $result;
267
+ },
268
+ ));
269
+
270
+ $resolver->replaceDefaults(array(
271
+ 'encryption' => 'tls', // simple overwrite
272
+ 'heavy' => function (Options $options) {
273
+ // $previousValue not available
274
+ // ...
275
+
276
+ return $someOtherResult;
277
+ },
278
+ ));
279
+ }
280
+
281
+ .. note ::
282
+
283
+ Existing option keys that you do not mention when overwriting are preserved.
284
+
220
285
Configure allowed Values
221
286
~~~~~~~~~~~~~~~~~~~~~~~~
222
287
@@ -270,7 +335,7 @@ Normalize the Options
270
335
271
336
Some values need to be normalized before you can use them. For instance,
272
337
pretend that the ``host `` should always start with ``http:// ``. To do that,
273
- you can write normalizers. These Closures will be executed after all options
338
+ you can write normalizers. These closures will be executed after all options
274
339
are passed and should return the normalized value. You can configure these
275
340
normalizers by calling
276
341
:method: `Symfony\\ Components\\ OptionsResolver\\ OptionsResolver::setNormalizers `::
0 commit comments