8000 feature #12426 [OptionResolver] Document the OptionConfigurator (lmil… · symfony/symfony-docs@73e9af7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 73e9af7

Browse files
committed
feature #12426 [OptionResolver] Document the OptionConfigurator (lmillucci)
This PR was squashed before being merged into the master branch. Discussion ---------- [OptionResolver] Document the OptionConfigurator Add `define()` method to OptionResolver to enhance readability of option configuration. See symfony/symfony#33848 Commits ------- 921c73f [OptionResolver] Document the OptionConfigurator
2 parents f8c1e14 + 921c73f commit 73e9af7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

components/options_resolver.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,38 @@ the option::
778778
This closure receives as argument the value of the option after validating it
779779
and before normalizing it when the option is being resolved.
780780

781+
Chaining option configurations
782+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
783+
784+
In many cases you may need to define multiple configurations for each option.
785+
For example, suppose the ``Mailer`` class has an ``host`` option that is required
786+
and a ``transport`` option which can be one of ``sendmail``, ``mail`` and ``smtp``.
787+
You can improve the readability of the code avoiding to duplicate option name for
788+
each configuration using the method
789+
:method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::define``::
790+
791+
// ...
792+
class Mailer
793+
{
794+
// ...
795+
public function configureOptions(OptionsResolver $resolver)
796+
{
797+
// ...
798+
$resolver->define('host')
799+
->required()
800+
->default('smtp.example.org')
801+
->allowedTypes('string');
802+
$resolver->define('transport')
803+
->required()
804+
->default('transport')
805+
->allowedValues(['sendmail', 'mail', 'smtp']);
806+
}
807+
}
808+
809+
.. versionadded:: 5.1
810+
811+
The ``define()`` method was introduced in Symfony 5.1.
812+
781813
Performance Tweaks
782814
~~~~~~~~~~~~~~~~~~
783815

0 commit comments

Comments
 (0)
0