Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[OptionsResolver] Update nested options examples to usesetOptions instead ofsetDefault#20719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletionscomponents/options_resolver.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -662,7 +662,7 @@ default value::

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('spool', function (OptionsResolver $spoolResolver): void {
$resolver->setOptions('spool', function (OptionsResolver $spoolResolver): void {
$spoolResolver->setDefaults([
'type' => 'file',
'path' => '/path/to/spool',
Expand All@@ -686,6 +686,16 @@ default value::
],
]);

.. deprecated:: 7.3

Defining nested option via :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setDefault`
was deprecated since Symfony 7.3, use the :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setOptions`
instead. Allowing to define default values for prototyped options.
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I'm not sure about this deprecation block. Is it needed in this case, or would the replacement + versionadded note be enough?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes, sometimes it's difficult to decide if we should add such a block. At the end, I left it so anyone searching for "setDefault" can still find this page. Thanks!

yceruto reacted with thumbs up emoji

.. versionadded:: 7.3

The :method:`Symfony\\Component\\OptionsResolver\\OptionsResolver::setOptions` was introduced in Symfony 7.3.

Nested options also support required options, validation (type, value) and
normalization of their values. If the default value of a nested option depends
on another option defined in the parent level, add a second ``Options`` argument
Expand All@@ -698,7 +708,7 @@ to the closure to access to them::
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('sandbox', false);
$resolver->setDefault('spool', function (OptionsResolver $spoolResolver, Options $parent): void {
$resolver->setOptions('spool', function (OptionsResolver $spoolResolver, Options $parent): void {
$spoolResolver->setDefaults([
'type' => $parent['sandbox'] ? 'memory' : 'file',
// ...
Expand All@@ -721,13 +731,13 @@ In same way, parent options can access to the nested options as normal arrays::

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('spool', function (OptionsResolver $spoolResolver): void {
$resolver->setOptions('spool', function (OptionsResolver $spoolResolver): void {
$spoolResolver->setDefaults([
'type' => 'file',
// ...
]);
});
$resolver->setDefault('profiling', function (Options $options): void {
$resolver->setOptions('profiling', function (Options $options): void {
return 'file' === $options['spool']['type'];
});
}
Expand All@@ -748,7 +758,7 @@ with ``host``, ``database``, ``user`` and ``password`` each.

The best way to implement this is to define the ``connections`` option as prototype::

$resolver->setDefault('connections', function (OptionsResolver $connResolver): void {
$resolver->setOptions('connections', function (OptionsResolver $connResolver): void {
$connResolver
->setPrototype(true)
->setRequired(['host', 'database'])
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp