diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 3b1fabfd17afd..45af0903317d0 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.3 +--- + + * Add support for displaying nested options in DebugCommand + 7.2 --- diff --git a/src/Symfony/Component/Form/Console/Descriptor/Descriptor.php b/src/Symfony/Component/Form/Console/Descriptor/Descriptor.php index c910acdf4d4ed..91778f8193a92 100644 --- a/src/Symfony/Component/Form/Console/Descriptor/Descriptor.php +++ b/src/Symfony/Component/Form/Console/Descriptor/Descriptor.php @@ -118,6 +118,7 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, string 'allowedValues' => 'getAllowedValues', 'normalizers' => 'getNormalizers', 'deprecation' => 'getDeprecation', + 'nestedOptions' => 'getNestedOptions', ]; foreach ($map as $key => $method) { diff --git a/src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php b/src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php index 1f5c7bfa55fb3..1eca762b73b4d 100644 --- a/src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php +++ b/src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php @@ -62,6 +62,12 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF } protected function describeOption(OptionsResolver $optionsResolver, array $options): void + { + $data = $this->getOptionDescription($optionsResolver, $options); + $this->writeData($data, $options); + } + + private function getOptionDescription(OptionsResolver $optionsResolver, array $options): array { $definition = $this->getOptionDefinition($optionsResolver, $options['option']); @@ -90,7 +96,17 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio } $data['has_normalizer'] = isset($definition['normalizers']); - $this->writeData($data, $options); + if ($data['has_nested_options'] = isset($definition['nestedOptions'])) { + $nestedResolver = new OptionsResolver(); + foreach ($definition['nestedOptions'] as $nestedOption) { + $nestedOption($nestedResolver, $optionsResolver); + } + foreach ($nestedResolver->getDefinedOptions() as $option) { + $data['nested_options'][$option] = $this->getOptionDescription($nestedResolver, ['option' => $option]); + } + } + + return $data; } private function writeData(array $data, array $options): void diff --git a/src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php b/src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php index 630b8725355f2..e12b3426283f9 100644 --- a/src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php +++ b/src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php @@ -118,12 +118,19 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio 'Allowed types' => 'allowedTypes', 'Allowed values' => 'allowedValues', 'Normalizers' => 'normalizers', + 'Nested Options' => 'nestedOptions', ]; $rows = []; foreach ($map as $label => $name) { $value = \array_key_exists($name, $definition) ? $dump($definition[$name]) : '-'; if ('default' === $name && isset($definition['lazy'])) { $value = "Value: $value\n\nClosure(s): ".$dump($definition['lazy']); + } elseif ('nestedOptions' === $name && isset($definition['nestedOptions'])) { + $nestedResolver = new OptionsResolver(); + foreach ($definition['nestedOptions'] as $nestedOption) { + $nestedOption($nestedResolver, $optionsResolver); + } + $value = $dump($nestedResolver->getDefinedOptions()); } $rows[] = ["$label", $value]; diff --git a/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php b/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php index 4537099c2dc47..cac92addbf790 100644 --- a/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php +++ b/src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php @@ -179,6 +179,8 @@ class:%s } %s ] %s ---------------- -----------%s + Nested Options - %s + ---------------- -----------%s TXT , $tester->getDisplay(true)); diff --git a/src/Symfony/Component/Form/Tests/Console/Descriptor/AbstractDescriptorTestCase.php b/src/Symfony/Component/Form/Tests/Console/Descriptor/AbstractDescriptorTestCase.php index 8a99c205cbf97..456b433eee115 100644 --- a/src/Symfony/Component/Form/Tests/Console/Descriptor/AbstractDescriptorTestCase.php +++ b/src/Symfony/Component/Form/Tests/Console/Descriptor/AbstractDescriptorTestCase.php @@ -130,6 +130,11 @@ public static function getDescribeOptionTestData() $options['option'] = 'bar'; $options['show_deprecated'] = true; yield [$resolvedType->getOptionsResolver(), $options, 'deprecated_option']; + + $resolvedType = new ResolvedFormType(new FooType(), [], $parent); + $options['type'] = $resolvedType->getInnerType(); + $options['option'] = 'baz'; + yield [$resolvedType->getOptionsResolver(), $options, 'nested_option']; } abstract protected function getDescriptor(); @@ -172,5 +177,9 @@ public function configureOptions(OptionsResolver $resolver): void $resolver->setAllowedTypes('foo', 'string'); $resolver->setAllowedValues('foo', ['bar', 'baz']); $resolver->setNormalizer('foo', fn (Options $options, $value) => (string) $value); + $resolver->setOptions('baz', function (OptionsResolver $baz) { + $baz->setRequired('foo'); + $baz->setDefaults(['foo' => true, 'bar' => true]); + }); } } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.json b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.json index 0ac903a954754..25b7c4525cefa 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.json +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.json @@ -7,5 +7,6 @@ "bool", "string" ], - "has_normalizer": true + "has_normalizer": true, + "has_nested_options": false } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.txt b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.txt index 0f6f1f40e728e..cd41f47a87b41 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.txt +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/default_option_with_normalizer.txt @@ -25,4 +25,6 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (choice_translation_domain } %s ] %s ---------------- -----------%s + Nested Options - %s + ---------------- -----------%s diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.json b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.json index b3b0cf3ecaff0..5c88933f9887c 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.json +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.json @@ -2,5 +2,6 @@ "deprecated": true, "deprecation_message": "The option \"bar\" is deprecated.", "required": false, - "has_normalizer": false + "has_normalizer": false, + "has_nested_options": false } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.txt b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.txt index 31ef796f905d7..f5da39c703d0f 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.txt +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/deprecated_option.txt @@ -21,4 +21,6 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (bar) Allowed values - --------------------- ----------------------------------- Normalizers - - --------------------- ----------------------------------- + --------------------- ----------------------------------- + Nested Options - + --------------------- ----------------------------------- diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/nested_option.json b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/nested_option.json new file mode 100644 index 0000000000000..a6fa2a93a2f6e --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/nested_option.json @@ -0,0 +1,23 @@ +{ + "required": false, + "default": [], + "is_lazy": false, + "has_normalizer": false, + "has_nested_options": true, + "nested_options": { + "foo": { + "required": true, + "default": true, + "is_lazy": false, + "has_normalizer": false, + "has_nested_options": false + }, + "bar": { + "required": false, + "default": true, + "is_lazy": false, + "has_normalizer": false, + "has_nested_options": false + } + } +} diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/nested_option.txt b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/nested_option.txt new file mode 100644 index 0000000000000..0f698abfc63a4 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/nested_option.txt @@ -0,0 +1,21 @@ +Symfony\Component\Form\Tests\Console\Descriptor\FooType (baz) +============================================================= + + ---------------- ---------- + Info - + ---------------- ---------- + Required false + ---------------- ---------- + Default [] + ---------------- ---------- + Allowed types - + ---------------- ---------- + Allowed values - + ---------------- ---------- + Normalizers - + ---------------- ---------- + Nested Options [ + "foo", + "bar" + ] + ---------------- ---------- diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.json b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.json index c41e377acd3db..bc79122e1eb28 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.json +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.json @@ -2,5 +2,6 @@ "required": false, "default": null, "is_lazy": true, - "has_normalizer": false + "has_normalizer": false, + "has_nested_options": false } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.txt b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.txt index cedca25a606e6..8bf9cb8c8d50a 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.txt +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/overridden_option_with_default_closures.txt @@ -23,6 +23,8 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (empty_data) ---------------- ----------------------%s Allowed values - %s ---------------- ----------------------%s - Normalizers - %s + Normalizers - %s + ---------------- ----------------------%s + Nested Options - %s ---------------- ----------------------%s diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.json b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.json index 126933c6b0485..5aad5becbc9f7 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.json +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.json @@ -7,5 +7,6 @@ "bar", "baz" ], - "has_normalizer": true + "has_normalizer": true, + "has_nested_options": false } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.txt b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.txt index fd2f8f3f19c20..a8371243b8c73 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.txt +++ b/src/Symfony/Component/Form/Tests/Fixtures/Descriptor/required_option_with_allowed_values.txt @@ -27,4 +27,5 @@ Symfony\Component\Form\Tests\Console\Descriptor\FooType (foo) } %s ] %s ---------------- -----------%s - + Nested Options - %s + ---------------- -----------%s diff --git a/src/Symfony/Component/Form/composer.json b/src/Symfony/Component/Form/composer.json index 40c021d915b22..f4403ba74d878 100644 --- a/src/Symfony/Component/Form/composer.json +++ b/src/Symfony/Component/Form/composer.json @@ -19,7 +19,7 @@ "php": ">=8.2", "symfony/deprecation-contracts": "^2.5|^3", "symfony/event-dispatcher": "^6.4|^7.0", - "symfony/options-resolver": "^6.4|^7.0", + "symfony/options-resolver": "^7.3", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-intl-icu": "^1.21", "symfony/polyfill-mbstring": "~1.0",