Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f02ee12

Browse files
committed
Show deprecated options definition on debug:form command
1 parent e379146 commit f02ee12

14 files changed

+280
-37
lines changed

src/Symfony/Component/Form/Command/DebugCommand.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function configure()
5858
->setDefinition(array(
5959
new InputArgument('class', InputArgument::OPTIONAL, 'The form type class'),
6060
new InputArgument('option', InputArgument::OPTIONAL, 'The form type option'),
61+
new InputOption('show-deprecated', null, InputOption::VALUE_NONE, 'Used to show deprecated options in form types'),
6162
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt or json)', 'txt'),
6263
))
6364
->setDescription('Displays form type information')
@@ -75,6 +76,11 @@ protected function configure()
7576
7677
<info>php %command.full_name% ChoiceType choice_value</info>
7778
79+
Use the <info>--show-deprecated</info> option to display form types with deprecated options or the deprecated options of the given form type:
80+
81+
<info>php %command.full_name% --show-deprecated</info>
82+
<info>php %command.full_name% ChoiceType --show-deprecated</info>
83+
7884
The command displays the definition of the given option name.
7985
8086
<info>php %command.full_name% --format=json</info>
@@ -134,6 +140,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
134140

135141
$helper = new DescriptorHelper();
136142
$options['format'] = $input->getOption('format');
143+
$options['show_deprecated'] = $input->getOption('show-deprecated');
144+
$options['form_registry'] = $this->formRegistry;
137145
$helper->describe($io, $object, $options);
138146
}
139147

src/Symfony/Component/Form/Console/Descriptor/Descriptor.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Output\OutputInterface;
1717
use Symfony\Component\Console\Style\OutputStyle;
1818
use Symfony\Component\Console\Style\SymfonyStyle;
19+
use Symfony\Component\Form\FormRegistryInterface;
1920
use Symfony\Component\Form\ResolvedFormTypeInterface;
2021
use Symfony\Component\Form\Util\OptionsResolverWrapper;
2122
use Symfony\Component\OptionsResolver\Debug\OptionsResolverIntrospector;
@@ -39,13 +40,16 @@ abstract class Descriptor implements DescriptorInterface
3940
protected $requiredOptions = array();
4041
protected $parents = array();
4142
protected $extensions = array();
43+
/** @var FormRegistryInterface */
44+
private $formRegistry;
4245

4346
/**
4447
* {@inheritdoc}
4548
*/
4649
public function describe(OutputInterface $output, $object, array $options = array())
4750
{
4851
$this->output = $output instanceof OutputStyle ? $output : new SymfonyStyle(new ArrayInput(array()), $output);
52+
$this->formRegistry = $options['form_registry'] ?? null;
4953

5054
switch (true) {
5155
case null === $object:
@@ -108,7 +112,10 @@ protected function collectOptions(ResolvedFormTypeInterface $type)
108112

109113
protected function getOptionDefinition(OptionsResolver $optionsResolver, $option)
110114
{
111-
$definition = array('required' => $optionsResolver->isRequired($option));
115+
$definition = array(
116+
'required' => $optionsResolver->isRequired($option),
117+
'deprecated' => $optionsResolver->isDeprecated($option),
118+
);
112119

113120
$introspector = new OptionsResolverIntrospector($optionsResolver);
114121

@@ -118,6 +125,7 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, $option
118125
'allowedTypes' => 'getAllowedTypes',
119126
'allowedValues' => 'getAllowedValues',
120127
'normalizer' => 'getNormalizer',
128+
'deprecationMessage' => 'getDeprecationMessage',
121129
);
122130

123131
foreach ($map as $key => $method) {
@@ -128,9 +136,57 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, $option
128136
}
129137
}
130138

139+
if (isset($definition['deprecationMessage']) && \is_string($definition['deprecationMessage'])) {
140+
$definition['deprecationMessage'] = strtr($definition['deprecationMessage'], array('%name%' => $option));
141+
}
142+
131143
return $definition;
132144
}
133145

146+
protected function filterTypesByDeprecated(array $types): array
147+
{
148+
$typesWithDeprecatedOptions = array();
149+
foreach ($types as $class) {
150+
$optionsResolver = $this->formRegistry->getType($class)->getOptionsResolver();
151+
foreach ($optionsResolver->getDefinedOptions() as $option) {
152+
if ($optionsResolver->isDeprecated($option)) {
153+
$typesWithDeprecatedOptions[] = $class;
154+
break;
155+
}
156+
}
157+
}
158+
159+
return $typesWithDeprecatedOptions;
160+
}
161+
162+
protected function filterOptionsByDeprecated(ResolvedFormTypeInterface $type)
163+
{
164+
$deprecatedOptions = array();
165+
$resolver = $type->getOptionsResolver();
166+
foreach ($resolver->getDefinedOptions() as $option) {
167+
if ($resolver->isDeprecated($option)) {
168+
$deprecatedOptions[] = $option;
169+
}
170+
}
171+
172+
$filterByDeprecated = function (array $options) use ($deprecatedOptions) {
173+
foreach ($options as $class => $opts) {
174+
if ($deprecated = array_intersect($deprecatedOptions, $opts)) {
175+
$options[$class] = $deprecated;
176+
} else {
177+
unset($options[$class]);
178+
}
179+
}
180+
181+
return $options;
182+
};
183+
184+
$this->ownOptions = array_intersect($deprecatedOptions, $this->ownOptions);
185+
$this->overriddenOptions = $filterByDeprecated($this->overriddenOptions);
186+
$this->parentOptions = $filterByDeprecated($this->parentOptions);
187+
$this->extensionOptions = $filterByDeprecated($this->extensionOptions);
188+
}
189+
134190
private function getParentOptionsResolver(ResolvedFormTypeInterface $type)
135191
{
136192
$this->parents[$class = get_class($type->getInnerType())] = array();

src/Symfony/Component/Form/Console/Descriptor/JsonDescriptor.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ class JsonDescriptor extends Descriptor
2323
{
2424
protected function describeDefaults(array $options)
2525
{
26-
$data['builtin_form_types'] = $options['core_types'];
27-
$data['service_form_types'] = $options['service_types'];
28-
$data['type_extensions'] = $options['extensions'];
29-
$data['type_guessers'] = $options['guessers'];
26+
$data['builtin_form_types'] = $options['show_deprecated'] ? $this->filterTypesByDeprecated($options['core_types']) : $options['core_types'];
27+
$data['service_form_types'] = $options['show_deprecated'] ? $this->filterTypesByDeprecated($options['service_types']) : $options['service_types'];
28+
if (!$options['show_deprecated']) {
29+
$data['type_extensions'] = $options['extensions'];
30+
$data['type_guessers'] = $options['guessers'];
31+
}
3032

3133
$this->writeData($data, $options);
3234
}
@@ -35,6 +37,10 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
3537
{
3638
$this->collectOptions($resolvedFormType);
3739

40+
if (!empty($options['show_deprecated'])) {
41+
$this->filterOptionsByDeprecated($resolvedFormType);
42+
}
43+
3844
$formOptions = array(
3945
'own' => $this->ownOptions,
4046
'overridden' => $this->overriddenOptions,
@@ -59,7 +65,14 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
5965
{
6066
$definition = $this->getOptionDefinition($optionsResolver, $options['option']);
6167

62-
$map = array(
68+
$map = array();
69+
if ($definition['deprecated']) {
70+
$map['deprecated'] = 'deprecated';
71+
if (\is_string($definition['deprecationMessage'])) {
72+
$map['deprecation_message'] = 'deprecationMessage';
73+
}
74+
}
75+
$map += array(
6376
'required' => 'required',
6477
'default' => 'default',
6578
'allowed_types' => 'allowedTypes',

src/Symfony/Component/Form/Console/Descriptor/TextDescriptor.php

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,35 @@ class TextDescriptor extends Descriptor
2727
{
2828
protected function describeDefaults(array $options)
2929
{
30+
$coreTypes = $options['show_deprecated'] ? $this->filterTypesByDeprecated($options['core_types']) : $options['core_types'];
31+
$serviceTypes = $options['show_deprecated'] ? $this->filterTypesByDeprecated($options['service_types']) : $options['service_types'];
32+
3033
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
31-
$shortClassNames = array_map(function ($fqcn) { return array_slice(explode('\\', $fqcn), -1)[0]; }, $options['core_types']);
34+
$shortClassNames = array_map(function ($fqcn) { return array_slice(explode('\\', $fqcn), -1)[0]; }, $coreTypes);
3235
for ($i = 0; $i * 5 < count($shortClassNames); ++$i) {
3336
$this->output->writeln(' '.implode(', ', array_slice($shortClassNames, $i * 5, 5)));
3437
}
3538

3639
$this->output->section('Service form types');
37-
$this->output->listing($options['service_types']);
40+
$this->output->listing($serviceTypes);
3841

39-
$this->output->section('Type extensions');
40-
$this->output->listing($options['extensions']);
42+
if (!$options['show_deprecated']) {
43+
$this->output->section('Type extensions');
44+
$this->output->listing($options['extensions']);
4145

42-
$this->output->section('Type guessers');
43-
$this->output->listing($options['guessers']);
46+
$this->output->section('Type guessers');
47+
$this->output->listing($options['guessers']);
48+
}
4449
}
4550

4651
protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = array())
4752
{
4853
$this->collectOptions($resolvedFormType);
4954

55+
if (!empty($options['show_deprecated'])) {
56+
$this->filterOptionsByDeprecated($resolvedFormType);
57+
}
58+
5059
$formOptions = $this->normalizeAndSortOptionsColumns(array_filter(array(
5160
'own' => $this->ownOptions,
5261
'overridden' => $this->overriddenOptions,
@@ -62,29 +71,12 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
6271
'extension' => 'Extension options',
6372
), $formOptions);
6473

65-
$tableRows = array();
66-
$count = count(max($formOptions));
67-
for ($i = 0; $i < $count; ++$i) {
68-
$cells = array();
69-
foreach (array_keys($tableHeaders) as $group) {
70-
if (isset($formOptions[$group][$i])) {
71-
$option = $formOptions[$group][$i];
72-
73-
if (is_string($option) && in_array($option, $this->requiredOptions)) {
74-
$option .= ' <info>(required)</info>';
75-
}
74+
$this->output->title(sprintf('%s (Block prefix: "%s")', get_class($resolvedFormType->getInnerType()), $resolvedFormType->getInnerType()->getBlockPrefix()));
7675

77-
$cells[] = $option;
78-
} else {
79-
$cells[] = null;
80-
}
81-
}
82-
$tableRows[] = $cells;
76+
if ($formOptions) {
77+
$this->output->table($tableHeaders, $this->buildTableRows($tableHeaders, $formOptions));
8378
}
8479

85-
$this->output->title(sprintf('%s (Block prefix: "%s")', get_class($resolvedFormType->getInnerType()), $resolvedFormType->getInnerType()->getBlockPrefix()));
86-
$this->output->table($tableHeaders, $tableRows);
87-
8880
if ($this->parents) {
8981
$this->output->section('Parent types');
9082
$this->output->listing($this->parents);
@@ -101,7 +93,14 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
10193
$definition = $this->getOptionDefinition($optionsResolver, $options['option']);
10294

10395
$dump = $this->getDumpFunction();
104-
$map = array(
96+
$map = array();
97+
if ($definition['deprecated']) {
98+
$map = array(
99+
'Deprecated' => 'deprecated',
100+
'Deprecation message' => 'deprecationMessage',
101+
);
102+
}
103+
$map += array(
105104
'Required' => 'required',
106105
'Default' => 'default',
107106
'Allowed types' => 'allowedTypes',
@@ -124,6 +123,25 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
124123
$this->output->table(array(), $rows);
125124
}
126125

126+
private function buildTableRows(array $headers, array $options): array
127+
{
128+
$tableRows = array();
129+
$count = \count(max($options));
130+
for ($i = 0; $i < $count; ++$i) {
131+
$cells = array();
132+
foreach (array_keys($headers) as $group) {
133+
$option = $options[$group][$i] ?? null;
134+
if (\is_string($option) && \in_array($option, $this->requiredOptions, true)) {
135+
$option .= ' <info>(required)</info>';
136+
}
137+
$cells[] = $option;
138+
}
139+
$tableRows[] = $cells;
140+
}
141+
142+
return $tableRows;
143+
}
144+
127145
private function normalizeAndSortOptionsColumns(array $options)
128146
{
129147
foreach ($options as $group => $opts) {

src/Symfony/Component/Form/Tests/Console/Descriptor/AbstractDescriptorTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
use Symfony\Component\Form\Extension\Core\Type\FormType;
2121
use Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension;
2222
use Symfony\Component\Form\FormInterface;
23+
use Symfony\Component\Form\FormRegistry;
2324
use Symfony\Component\Form\ResolvedFormType;
25+
use Symfony\Component\Form\ResolvedFormTypeFactory;
2426
use Symfony\Component\Form\ResolvedFormTypeInterface;
2527
use Symfony\Component\OptionsResolver\Options;
2628
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -74,23 +76,30 @@ public function getDescribeDefaultsTestData()
7476
$options['extensions'] = array('Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension');
7577
$options['guessers'] = array('Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser');
7678
$options['decorated'] = false;
77-
79+
$options['show_deprecated'] = false;
7880
yield array(null, $options, 'defaults_1');
81+
82+
$options['service_types'] = array(FooType::class);
83+
$options['form_registry'] = new FormRegistry(array(), new ResolvedFormTypeFactory());
84+
$options['show_deprecated'] = true;
85+
yield array(null, $options, 'types_with_deprecated_options');
7986
}
8087

8188
public function getDescribeResolvedFormTypeTestData()
8289
{
8390
$typeExtensions = array(new FormTypeCsrfExtension(new CsrfTokenManager()));
8491
$parent = new ResolvedFormType(new FormType(), $typeExtensions);
8592

86-
yield array(new ResolvedFormType(new ChoiceType(), array(), $parent), array('decorated' => false), 'resolved_form_type_1');
87-
yield array(new ResolvedFormType(new FormType()), array('decorated' => false), 'resolved_form_type_2');
93+
yield array(new ResolvedFormType(new ChoiceType(), array(), $parent), array('decorated' => false, 'show_deprecated' => false), 'resolved_form_type_1');
94+
yield array(new ResolvedFormType(new FormType()), array('decorated' => false, 'show_deprecated' => false), 'resolved_form_type_2');
95+
yield array(new ResolvedFormType(new FooType(), array(), $parent), array('decorated' => false, 'show_deprecated' => true), 'deprecated_options_of_type');
8896
}
8997

9098
public function getDescribeOptionTestData()
9199
{
92100
$parent = new ResolvedFormType(new FormType());
93101
$options['decorated'] = false;
102+
$options['show_deprecated'] = false;
94103

95104
$resolvedType = new ResolvedFormType(new ChoiceType(), array(), $parent);
96105
$options['type'] = $resolvedType->getInnerType();
@@ -104,6 +113,11 @@ public function getDescribeOptionTestData()
104113

105114
$options['option'] = 'empty_data';
106115
yield array($resolvedType->getOptionsResolver(), $options, 'overridden_option_with_default_closures');
116+
117+
$resolvedType = new ResolvedFormType(new FooType(), array(), $parent);
118+
$options['type'] = $resolvedType->getInnerType();
119+
$options['option'] = 'bar';
120+
yield array($resolvedType->getOptionsResolver(), $options, 'deprecated_option');
107121
}
108122

109123
abstract protected function getDescriptor();
@@ -136,6 +150,8 @@ class FooType extends AbstractType
136150
public function configureOptions(OptionsResolver $resolver)
137151
{
138152
$resolver->setRequired('foo');
153+
$resolver->setDefined('bar');
154+
$resolver->setDeprecated('bar');
139155
$resolver->setDefault('empty_data', function (Options $options, $value) {
140156
$foo = $options['foo'];
141157

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"deprecated": true,
3+
"deprecation_message": "The option \"bar\" is deprecated.",
4+
"required": false,
5+
"has_normalizer": false
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Symfony\Component\Form\Tests\Console\Descriptor\FooType (bar)
2+
=============================================================
3+
4+
--------------------- -----------------------------------
5+
Deprecated true
6+
--------------------- -----------------------------------
7+
Deprecation message "The option "bar" is deprecated."
8+
--------------------- -----------------------------------
9+
Required false
10+
--------------------- -----------------------------------
11+
Default -
12+
--------------------- -----------------------------------
13+
Allowed types -
14+
--------------------- -----------------------------------
15+
Allowed values -
16+
--------------------- -----------------------------------
17+
Normalizer -
18+
--------------------- -----------------------------------

0 commit comments

Comments
 (0)