Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 73e9af7

Browse filesBrowse 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
Copy full SHA for 73e9af7

File tree

1 file changed

+32
-0
lines changed
Filter options

1 file changed

+32
-0
lines changed

‎components/options_resolver.rst

Copy file name to clipboardExpand all lines: components/options_resolver.rst
+32Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.