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

Commit676e79d

Browse files
committed
Show deprecated options definition on debug:form command
1 parente379146 commit676e79d

File tree

15 files changed

+308
-46
lines changed

15 files changed

+308
-46
lines changed

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected function configure()
5858
->setDefinition(array(
5959
newInputArgument('class', InputArgument::OPTIONAL,'The form type class'),
6060
newInputArgument('option', InputArgument::OPTIONAL,'The form type option'),
61+
newInputOption('show-deprecated',null, InputOption::VALUE_NONE,'Used to show deprecated options in form types'),
6162
newInputOption('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>
@@ -96,6 +102,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
96102
$object =null;
97103
$options['core_types'] =$this->getCoreTypes();
98104
$options['service_types'] =array_values(array_diff($this->types,$options['core_types']));
105+
if ($input->getOption('show-deprecated')) {
106+
$options['core_types'] =$this->filterTypesByDeprecated($options['core_types']);
107+
$options['service_types'] =$this->filterTypesByDeprecated($options['service_types']);
108+
}
99109
$options['extensions'] =$this->extensions;
100110
$options['guessers'] =$this->guessers;
101111
foreach ($optionsas$k =>$list) {
@@ -111,10 +121,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
111121
$object =$resolvedType->getOptionsResolver();
112122

113123
if (!$object->isDefined($option)) {
114-
$message =sprintf('Option "%s" is not defined in "%s".',$option,get_class($resolvedType->getInnerType()));
124+
$message =sprintf('Option "%s" is not defined in "%s".',$option,\get_class($resolvedType->getInnerType()));
115125

116126
if ($alternatives =$this->findAlternatives($option,$object->getDefinedOptions())) {
117-
if (1 ==count($alternatives)) {
127+
if (1 ===\count($alternatives)) {
118128
$message .="\n\nDid you mean this?\n";
119129
}else {
120130
$message .="\n\nDid you mean one of these?\n";
@@ -134,6 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
134144

135145
$helper =newDescriptorHelper();
136146
$options['format'] =$input->getOption('format');
147+
$options['show_deprecated'] =$input->getOption('show-deprecated');
137148
$helper->describe($io,$object,$options);
138149
}
139150

@@ -147,12 +158,12 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, $shor
147158
}
148159
}
149160

150-
if (0 ===$count =count($classes)) {
161+
if (0 ===$count =\count($classes)) {
151162
$message =sprintf("Could not find type\"%s\" into the following namespaces:\n %s",$shortClassName,implode("\n",$this->namespaces));
152163

153164
$allTypes =array_merge($this->getCoreTypes(),$this->types);
154165
if ($alternatives =$this->findAlternatives($shortClassName,$allTypes)) {
155-
if (1 ==count($alternatives)) {
166+
if (1 ===\count($alternatives)) {
156167
$message .="\n\nDid you mean this?\n";
157168
}else {
158169
$message .="\n\nDid you mean one of these?\n";
@@ -184,12 +195,28 @@ private function getCoreTypes()
184195
return$coreTypes;
185196
}
186197

198+
privatefunctionfilterTypesByDeprecated(array$types):array
199+
{
200+
$typesWithDeprecatedOptions =array();
201+
foreach ($typesas$class) {
202+
$optionsResolver =$this->formRegistry->getType($class)->getOptionsResolver();
203+
foreach ($optionsResolver->getDefinedOptions()as$option) {
204+
if ($optionsResolver->isDeprecated($option)) {
205+
$typesWithDeprecatedOptions[] =$class;
206+
break;
207+
}
208+
}
209+
}
210+
211+
return$typesWithDeprecatedOptions;
212+
}
213+
187214
privatefunctionfindAlternatives($name,array$collection)
188215
{
189216
$alternatives =array();
190217
foreach ($collectionas$item) {
191218
$lev =levenshtein($name,$item);
192-
if ($lev <=strlen($name) /3 ||false !==strpos($item,$name)) {
219+
if ($lev <=\strlen($name) /3 ||false !==strpos($item,$name)) {
193220
$alternatives[$item] =isset($alternatives[$item]) ?$alternatives[$item] -$lev :$lev;
194221
}
195222
}

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ protected function collectOptions(ResolvedFormTypeInterface $type)
108108

109109
protectedfunctiongetOptionDefinition(OptionsResolver$optionsResolver,$option)
110110
{
111-
$definition =array('required' =>$optionsResolver->isRequired($option));
111+
$definition =array(
112+
'required' =>$optionsResolver->isRequired($option),
113+
'deprecated' =>$optionsResolver->isDeprecated($option),
114+
);
112115

113116
$introspector =newOptionsResolverIntrospector($optionsResolver);
114117

@@ -118,6 +121,7 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, $option
118121
'allowedTypes' =>'getAllowedTypes',
119122
'allowedValues' =>'getAllowedValues',
120123
'normalizer' =>'getNormalizer',
124+
'deprecationMessage' =>'getDeprecationMessage',
121125
);
122126

123127
foreach ($mapas$key =>$method) {
@@ -128,9 +132,41 @@ protected function getOptionDefinition(OptionsResolver $optionsResolver, $option
128132
}
129133
}
130134

135+
if (isset($definition['deprecationMessage']) &&\is_string($definition['deprecationMessage'])) {
136+
$definition['deprecationMessage'] =strtr($definition['deprecationMessage'],array('%name%' =>$option));
137+
}
138+
131139
return$definition;
132140
}
133141

142+
protectedfunctionfilterOptionsByDeprecated(ResolvedFormTypeInterface$type)
143+
{
144+
$deprecatedOptions =array();
145+
$resolver =$type->getOptionsResolver();
146+
foreach ($resolver->getDefinedOptions()as$option) {
147+
if ($resolver->isDeprecated($option)) {
148+
$deprecatedOptions[] =$option;
149+
}
150+
}
151+
152+
$filterByDeprecated =function (array$options)use ($deprecatedOptions) {
153+
foreach ($optionsas$class =>$opts) {
154+
if ($deprecated =array_intersect($deprecatedOptions,$opts)) {
155+
$options[$class] =$deprecated;
156+
}else {
157+
unset($options[$class]);
158+
}
159+
}
160+
161+
return$options;
162+
};
163+
164+
$this->ownOptions =array_intersect($deprecatedOptions,$this->ownOptions);
165+
$this->overriddenOptions =$filterByDeprecated($this->overriddenOptions);
166+
$this->parentOptions =$filterByDeprecated($this->parentOptions);
167+
$this->extensionOptions =$filterByDeprecated($this->extensionOptions);
168+
}
169+
134170
privatefunctiongetParentOptionsResolver(ResolvedFormTypeInterface$type)
135171
{
136172
$this->parents[$class =get_class($type->getInnerType())] =array();

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ protected function describeDefaults(array $options)
2525
{
2626
$data['builtin_form_types'] =$options['core_types'];
2727
$data['service_form_types'] =$options['service_types'];
28-
$data['type_extensions'] =$options['extensions'];
29-
$data['type_guessers'] =$options['guessers'];
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 ($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: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,40 @@ class TextDescriptor extends Descriptor
2727
{
2828
protectedfunctiondescribeDefaults(array$options)
2929
{
30-
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
31-
$shortClassNames =array_map(function ($fqcn) {returnarray_slice(explode('\\',$fqcn), -1)[0]; },$options['core_types']);
32-
for ($i =0;$i *5 <count($shortClassNames); ++$i) {
33-
$this->output->writeln(''.implode(',',array_slice($shortClassNames,$i *5,5)));
30+
if ($options['core_types']) {
31+
$this->output->section('Built-in form types (Symfony\Component\Form\Extension\Core\Type)');
32+
$shortClassNames =array_map(function ($fqcn) {return\array_slice(explode('\\',$fqcn), -1)[0]; },$options['core_types']);
33+
for ($i =0,$loopsMax =\count($shortClassNames);$i *5 <$loopsMax; ++$i) {
34+
$this->output->writeln(''.implode(',',\array_slice($shortClassNames,$i *5,5)));
35+
}
3436
}
3537

36-
$this->output->section('Service form types');
37-
$this->output->listing($options['service_types']);
38+
if ($options['service_types']) {
39+
$this->output->section('Service form types');
40+
$this->output->listing($options['service_types']);
41+
}
3842

39-
$this->output->section('Type extensions');
40-
$this->output->listing($options['extensions']);
43+
if (!$options['show_deprecated']) {
44+
if ($options['extensions']) {
45+
$this->output->section('Type extensions');
46+
$this->output->listing($options['extensions']);
47+
}
4148

42-
$this->output->section('Type guessers');
43-
$this->output->listing($options['guessers']);
49+
if ($options['guessers']) {
50+
$this->output->section('Type guessers');
51+
$this->output->listing($options['guessers']);
52+
}
53+
}
4454
}
4555

4656
protectedfunctiondescribeResolvedFormType(ResolvedFormTypeInterface$resolvedFormType,array$options =array())
4757
{
4858
$this->collectOptions($resolvedFormType);
4959

60+
if ($options['show_deprecated']) {
61+
$this->filterOptionsByDeprecated($resolvedFormType);
62+
}
63+
5064
$formOptions =$this->normalizeAndSortOptionsColumns(array_filter(array(
5165
'own' =>$this->ownOptions,
5266
'overridden' =>$this->overriddenOptions,
@@ -62,29 +76,12 @@ protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedF
6276
'extension' =>'Extension options',
6377
),$formOptions);
6478

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-
}
79+
$this->output->title(sprintf('%s (Block prefix: "%s")',get_class($resolvedFormType->getInnerType()),$resolvedFormType->getInnerType()->getBlockPrefix()));
7680

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

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

103100
$dump =$this->getDumpFunction();
104-
$map =array(
101+
$map =array();
102+
if ($definition['deprecated']) {
103+
$map =array(
104+
'Deprecated' =>'deprecated',
105+
'Deprecation message' =>'deprecationMessage',
106+
);
107+
}
108+
$map +=array(
105109
'Required' =>'required',
106110
'Default' =>'default',
107111
'Allowed types' =>'allowedTypes',
@@ -124,6 +128,25 @@ protected function describeOption(OptionsResolver $optionsResolver, array $optio
124128
$this->output->table(array(),$rows);
125129
}
126130

131+
privatefunctionbuildTableRows(array$headers,array$options):array
132+
{
133+
$tableRows =array();
134+
$count =\count(max($options));
135+
for ($i =0;$i <$count; ++$i) {
136+
$cells =array();
137+
foreach (array_keys($headers)as$group) {
138+
$option =$options[$group][$i] ??null;
139+
if (\is_string($option) &&\in_array($option,$this->requiredOptions,true)) {
140+
$option .=' <info>(required)</info>';
141+
}
142+
$cells[] =$option;
143+
}
144+
$tableRows[] =$cells;
145+
}
146+
147+
return$tableRows;
148+
}
149+
127150
privatefunctionnormalizeAndSortOptionsColumns(array$options)
128151
{
129152
foreach ($optionsas$group =>$opts) {

‎src/Symfony/Component/Form/Tests/Command/DebugCommandTest.php‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
useSymfony\Component\Console\Exception\InvalidArgumentException;
1717
useSymfony\Component\Console\Tester\CommandTester;
1818
useSymfony\Component\Form\Command\DebugCommand;
19+
useSymfony\Component\Form\Extension\Core\Type\TextType;
1920
useSymfony\Component\Form\FormRegistry;
2021
useSymfony\Component\Form\ResolvedFormTypeFactory;
22+
useSymfony\Component\Form\Tests\Console\Descriptor\FooType;
2123

2224
class DebugCommandTestextends TestCase
2325
{
@@ -30,6 +32,24 @@ public function testDebugDefaults()
3032
$this->assertContains('Built-in form types',$tester->getDisplay());
3133
}
3234

35+
publicfunctiontestDebugDeprecatedDefaults()
36+
{
37+
$tester =$this->createCommandTester(array('Symfony\Component\Form\Tests\Console\Descriptor'),array(TextType::class, FooType::class));
38+
$ret =$tester->execute(array('--show-deprecated' =>true),array('decorated' =>false));
39+
40+
$this->assertEquals(0,$ret,'Returns 0 in case of success');
41+
$this->assertSame(<<<TXT
42+
43+
Service form types
44+
------------------
45+
46+
* Symfony\Component\Form\Tests\Console\Descriptor\FooType
47+
48+
49+
TXT
50+
,$tester->getDisplay(true));
51+
}
52+
3353
publicfunctiontestDebugSingleFormType()
3454
{
3555
$tester =$this->createCommandTester();
@@ -117,10 +137,10 @@ public function testDebugInvalidFormType()
117137
$this->createCommandTester()->execute(array('class' =>'test'));
118138
}
119139

120-
privatefunctioncreateCommandTester(array$namespaces =null)
140+
privatefunctioncreateCommandTester(array$namespaces =array('Symfony\Component\Form\Extension\Core\Type'),array$types =array())
121141
{
122142
$formRegistry =newFormRegistry(array(),newResolvedFormTypeFactory());
123-
$command =null ===$namespaces ?newDebugCommand($formRegistry) :newDebugCommand($formRegistry,$namespaces);
143+
$command =newDebugCommand($formRegistry,$namespaces,$types);
124144
$application =newApplication();
125145
$application->add($command);
126146

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp