Skip to content

Commit 650dd51

Browse files
committed
Merge pull request #3264 from bicpi/improve_method_description
[Components][OptionsResolver] Fix&improve replaceDefaults() description
2 parents ee25fc6 + e91802f commit 650dd51

File tree

1 file changed

+77
-12
lines changed

1 file changed

+77
-12
lines changed

components/options_resolver.rst

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,12 @@ override this default. You don't need to configure ``username`` as an optional
177177
option. The ``OptionsResolver`` already knows that options with a default
178178
value are optional.
179179

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-
189180
Default Values that depend on another Option
190181
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191182

192183
Suppose you add a ``port`` option to the ``Mailer`` class, whose default
193184
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::
195186

196187
use Symfony\Component\OptionsResolver\Options;
197188
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
@@ -214,9 +205,83 @@ Closure as the default value::
214205

215206
.. caution::
216207

217-
The first argument of the Closure must be typehinted as ``Options``,
208+
The first argument of the closure must be typehinted as ``Options``,
218209
otherwise it is considered as the value.
219210

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+
220285
Configure allowed Values
221286
~~~~~~~~~~~~~~~~~~~~~~~~
222287

@@ -270,7 +335,7 @@ Normalize the Options
270335

271336
Some values need to be normalized before you can use them. For instance,
272337
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
274339
are passed and should return the normalized value. You can configure these
275340
normalizers by calling
276341
:method:`Symfony\\Components\\OptionsResolver\\OptionsResolver::setNormalizers`::

0 commit comments

Comments
 (0)