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

Skip to content

Commit 71b119f

Browse files
committed
Display form defaults in debug:form
1 parent 005cf6b commit 71b119f

File tree

10 files changed

+214
-8
lines changed

10 files changed

+214
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@
100100
<service id="Symfony\Component\Form\Command\DebugCommand">
101101
<argument type="service" id="form.registry" />
102102
<argument type="collection" /> <!-- All form types namespaces are stored here by FormPass -->
103+
<argument type="collection" /> <!-- All services form types are stored here by FormPass -->
104+
<argument type="collection" /> <!-- All type extensions are stored here by FormPass -->
105+
<argument type="collection" /> <!-- All type guessers are stored here by FormPass -->
103106
<tag name="console.command" command="debug:form" />
104107
</service>
105108
</services>

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Command;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Exception\InvalidArgumentException;
1516
use Symfony\Component\Console\Input\InputArgument;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
@@ -31,13 +32,19 @@ class DebugCommand extends Command
3132

3233
private $formRegistry;
3334
private $namespaces;
35+
private $types;
36+
private $extensions;
37+
private $guessers;
3438

35-
public function __construct(FormRegistryInterface $formRegistry, array $namespaces = array('Symfony\Component\Form\Extension\Core\Type'))
39+
public function __construct(FormRegistryInterface $formRegistry, array $namespaces = array('Symfony\Component\Form\Extension\Core\Type'), array $types = array(), array $extensions = array(), array $guessers = array())
3640
{
3741
parent::__construct();
3842

3943
$this->formRegistry = $formRegistry;
4044
$this->namespaces = $namespaces;
45+
$this->types = $types;
46+
$this->extensions = $extensions;
47+
$this->guessers = $guessers;
4148
}
4249

4350
/**
@@ -47,7 +54,7 @@ protected function configure()
4754
{
4855
$this
4956
->setDefinition(array(
50-
new InputArgument('class', InputArgument::REQUIRED, 'The form type class'),
57+
new InputArgument('class', InputArgument::OPTIONAL, 'The form type class'),
5158
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (txt or json)', 'txt'),
5259
))
5360
->setDescription('Displays form type information')
@@ -71,12 +78,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
7178
{
7279
$io = new SymfonyStyle($input, $output);
7380

74-
if (!class_exists($class = $input->getArgument('class'))) {
75-
$class = $this->getFqcnTypeClass($input, $io, $class);
81+
if (null === $input->getArgument('class')) {
82+
$object = null;
83+
$options['types'] = $this->types;
84+
$options['extensions'] = $this->extensions;
85+
$options['guessers'] = $this->guessers;
86+
} else {
87+
if (!class_exists($class = $input->getArgument('class'))) {
88+
$class = $this->getFqcnTypeClass($input, $io, $class);
89+
}
90+
$object = $this->formRegistry->getType($class);
7691
}
7792

78-
$object = $this->formRegistry->getType($class);
79-
8093
$helper = new DescriptorHelper();
8194
$options['format'] = $input->getOption('format');
8295
$helper->describe($io, $object, $options);
@@ -92,13 +105,13 @@ private function getFqcnTypeClass(InputInterface $input, SymfonyStyle $io, $shor
92105
}
93106

94107
if (0 === $count = count($classes)) {
95-
throw new \InvalidArgumentException(sprintf("Could not find type \"%s\" into the following namespaces:\n %s", $shortClassName, implode("\n ", $this->namespaces)));
108+
throw new InvalidArgumentException(sprintf("Could not find type \"%s\" into the following namespaces:\n %s", $shortClassName, implode("\n ", $this->namespaces)));
96109
}
97110
if (1 === $count) {
98111
return $classes[0];
99112
}
100113
if (!$input->isInteractive()) {
101-
throw new \InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
114+
throw new InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
102115
}
103116

104117
return $io->choice(sprintf("The type \"%s\" is ambiguous.\n\n Select one of the following form types to display its information:", $shortClassName), $classes, $classes[0]);

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\Console\Descriptor\DescriptorInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\Console\Style\SymfonyStyle;
17+
use Symfony\Component\Form\Extension\Core\CoreExtension;
18+
use Symfony\Component\Form\FormTypeInterface;
1719
use Symfony\Component\Form\ResolvedFormTypeInterface;
1820
use Symfony\Component\Form\Util\OptionsResolverWrapper;
1921
use Symfony\Component\OptionsResolver\OptionsResolver;
@@ -46,6 +48,9 @@ public function describe(OutputInterface $output, $object, array $options = arra
4648
$this->output = $output;
4749

4850
switch (true) {
51+
case null === $object:
52+
$this->describeDefaults($options);
53+
break;
4954
case $object instanceof ResolvedFormTypeInterface:
5055
$this->describeResolvedFormType($object, $options);
5156
break;
@@ -54,8 +59,21 @@ public function describe(OutputInterface $output, $object, array $options = arra
5459
}
5560
}
5661

62+
abstract protected function describeDefaults(array $options = array());
63+
5764
abstract protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = array());
5865

66+
protected function getCoreTypes()
67+
{
68+
$coreExtension = new CoreExtension();
69+
$coreExtensionRefObject = new \ReflectionObject($coreExtension);
70+
$loadTypesRefMethod = $coreExtensionRefObject->getMethod('loadTypes');
71+
$loadTypesRefMethod->setAccessible(true);
72+
$coreTypes = $loadTypesRefMethod->invoke($coreExtension);
73+
74+
return array_map(function (FormTypeInterface $type) { return get_class($type); }, $coreTypes);
75+
}
76+
5977
protected function collectOptions(ResolvedFormTypeInterface $type)
6078
{
6179
$this->parents = array();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
*/
2121
class JsonDescriptor extends Descriptor
2222
{
23+
protected function describeDefaults(array $options = array())
24+
{
25+
$data['builtin_form_types'] = $this->getCoreTypes();
26+
$data['services_form_types'] = array_values(array_diff($options['types'], $data['builtin_form_types']));
27+
$data['type_extensions'] = $options['extensions'];
28+
$data['type_guessers'] = $options['guessers'];
29+
30+
$this->writeData($data, $options);
31+
}
32+
2333
protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = array())
2434
{
2535
$this->collectOptions($resolvedFormType);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@
2121
*/
2222
class TextDescriptor extends Descriptor
2323
{
24+
protected function describeDefaults(array $options = array())
25+
{
26+
$coreTypes = $this->getCoreTypes();
27+
28+
$this->output->section('Built-in form types');
29+
$this->output->listing($coreTypes);
30+
31+
$this->output->section('Services form types');
32+
$this->output->listing(array_diff($options['types'], $coreTypes));
33+
34+
$this->output->section('Type extensions');
35+
$this->output->listing($options['extensions']);
36+
37+
$this->output->section('Type guessers');
38+
$this->output->listing($options['guessers']);
39+
}
40+
2441
protected function describeResolvedFormType(ResolvedFormTypeInterface $resolvedFormType, array $options = array())
2542
{
2643
$this->collectOptions($resolvedFormType);

src/Symfony/Component/Form/DependencyInjection/FormPass.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private function processFormTypes(ContainerBuilder $container)
7777
if ($container->hasDefinition($this->formDebugCommandService)) {
7878
$commandDefinition = $container->getDefinition($this->formDebugCommandService);
7979
$commandDefinition->setArgument(1, array_keys($namespaces));
80+
$commandDefinition->setArgument(2, array_keys($servicesMap));
8081
}
8182

8283
return ServiceLocatorTagPass::register($container, $servicesMap);
@@ -85,6 +86,7 @@ private function processFormTypes(ContainerBuilder $container)
8586
private function processFormTypeExtensions(ContainerBuilder $container)
8687
{
8788
$typeExtensions = array();
89+
$typeExtensionsClasses = array();
8890
foreach ($this->findAndSortTaggedServices($this->formTypeExtensionTag, $container) as $reference) {
8991
$serviceId = (string) $reference;
9092
$serviceDefinition = $container->getDefinition($serviceId);
@@ -97,20 +99,35 @@ private function processFormTypeExtensions(ContainerBuilder $container)
9799
}
98100

99101
$typeExtensions[$extendedType][] = new Reference($serviceId);
102+
$typeExtensionsClasses[] = $serviceDefinition->getClass();
100103
}
101104

102105
foreach ($typeExtensions as $extendedType => $extensions) {
103106
$typeExtensions[$extendedType] = new IteratorArgument($extensions);
104107
}
105108

109+
if ($container->hasDefinition($this->formDebugCommandService)) {
110+
$commandDefinition = $container->getDefinition($this->formDebugCommandService);
111+
$commandDefinition->setArgument(3, $typeExtensionsClasses);
112+
}
113+
106114
return $typeExtensions;
107115
}
108116

109117
private function processFormTypeGuessers(ContainerBuilder $container)
110118
{
111119
$guessers = array();
120+
$guessersClasses = array();
112121
foreach ($container->findTaggedServiceIds($this->formTypeGuesserTag, true) as $serviceId => $tags) {
113122
$guessers[] = new Reference($serviceId);
123+
124+
$serviceDefinition = $container->getDefinition($serviceId);
125+
$guessersClasses[] = $serviceDefinition->getClass();
126+
}
127+
128+
if ($container->hasDefinition($this->formDebugCommandService)) {
129+
$commandDefinition = $container->getDefinition($this->formDebugCommandService);
130+
$commandDefinition->setArgument(4, $guessersClasses);
114131
}
115132

116133
return new IteratorArgument($guessers);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
class DebugCommandTest extends TestCase
2323
{
24+
public function testDebugDefaults()
25+
{
26+
$tester = $this->createCommandTester();
27+
$ret = $tester->execute(array(), array('decorated' => false));
28+
29+
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
30+
$this->assertContains('Built-in form types', $tester->getDisplay());
31+
}
32+
2433
public function testDebugSingleFormType()
2534
{
2635
$tester = $this->createCommandTester();

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424

2525
abstract class AbstractDescriptorTest extends TestCase
2626
{
27+
/** @dataProvider getDescribeDefaultsTestData */
28+
public function testDescribeDefaults($object, array $options, $fixtureName)
29+
{
30+
$expectedDescription = $this->getExpectedDescription($fixtureName);
31+
$describedObject = $this->getObjectDescription($object, $options);
32+
33+
if ('json' === $this->getFormat()) {
34+
$this->assertEquals(json_encode(json_decode($expectedDescription), JSON_PRETTY_PRINT), json_encode(json_decode($describedObject), JSON_PRETTY_PRINT));
35+
} else {
36+
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $describedObject)));
37+
}
38+
}
39+
2740
/** @dataProvider getDescribeResolvedFormTypeTestData */
2841
public function testDescribeResolvedFormType(ResolvedFormTypeInterface $type, array $options, $fixtureName)
2942
{
@@ -37,6 +50,15 @@ public function testDescribeResolvedFormType(ResolvedFormTypeInterface $type, ar
3750
}
3851
}
3952

53+
public function getDescribeDefaultsTestData()
54+
{
55+
$options['types'] = array('Symfony\Bridge\Doctrine\Form\Type\EntityType');
56+
$options['extensions'] = array('Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension');
57+
$options['guessers'] = array('Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser');
58+
59+
yield array(null, $options, 'defaults_1');
60+
}
61+
4062
public function getDescribeResolvedFormTypeTestData()
4163
{
4264
$typeExtensions = array(
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"builtin_form_types": [
3+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType",
4+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\BirthdayType",
5+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\CheckboxType",
6+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\ChoiceType",
7+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\CollectionType",
8+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\CountryType",
9+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\DateIntervalType",
10+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\DateType",
11+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\DateTimeType",
12+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\EmailType",
13+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\HiddenType",
14+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\IntegerType",
15+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\LanguageType",
16+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\LocaleType",
17+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\MoneyType",
18+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\NumberType",
19+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\PasswordType",
20+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\PercentType",
21+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\RadioType",
22+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\RangeType",
23+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\RepeatedType",
24+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\SearchType",
25+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\TextareaType",
26+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType",
27+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\TimeType",
28+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\TimezoneType",
29+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\UrlType",
30+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\FileType",
31+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\ButtonType",
32+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\SubmitType",
33+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\ResetType",
34+
"Symfony\\Component\\Form\\Extension\\Core\\Type\\CurrencyType"
35+
],
36+
"services_form_types": [
37+
"Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType"
38+
],
39+
"type_extensions": [
40+
"Symfony\\Component\\Form\\Extension\\Csrf\\Type\\FormTypeCsrfExtension"
41+
],
42+
"type_guessers": [
43+
"Symfony\\Component\\Form\\Extension\\Validator\\ValidatorTypeGuesser"
44+
]
45+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
Built-in form types
3+
-------------------
4+
5+
* Symfony\Component\Form\Extension\Core\Type\FormType
6+
* Symfony\Component\Form\Extension\Core\Type\BirthdayType
7+
* Symfony\Component\Form\Extension\Core\Type\CheckboxType
8+
* Symfony\Component\Form\Extension\Core\Type\ChoiceType
9+
* Symfony\Component\Form\Extension\Core\Type\CollectionType
10+
* Symfony\Component\Form\Extension\Core\Type\CountryType
11+
* Symfony\Component\Form\Extension\Core\Type\DateIntervalType
12+
* Symfony\Component\Form\Extension\Core\Type\DateType
13+
* Symfony\Component\Form\Extension\Core\Type\DateTimeType
14+
* Symfony\Component\Form\Extension\Core\Type\EmailType
15+
* Symfony\Component\Form\Extension\Core\Type\HiddenType
16+
* Symfony\Component\Form\Extension\Core\Type\IntegerType
17+
* Symfony\Component\Form\Extension\Core\Type\LanguageType
18+
* Symfony\Component\Form\Extension\Core\Type\LocaleType
19+
* Symfony\Component\Form\Extension\Core\Type\MoneyType
20+
* Symfony\Component\Form\Extension\Core\Type\NumberType
21+
* Symfony\Component\Form\Extension\Core\Type\PasswordType
22+
* Symfony\Component\Form\Extension\Core\Type\PercentType
23+
* Symfony\Component\Form\Extension\Core\Type\RadioType
24+
* Symfony\Component\Form\Extension\Core\Type\RangeType
25+
* Symfony\Component\Form\Extension\Core\Type\RepeatedType
26+
* Symfony\Component\Form\Extension\Core\Type\SearchType
27+
* Symfony\Component\Form\Extension\Core\Type\TextareaType
28+
* Symfony\Component\Form\Extension\Core\Type\TextType
29+
* Symfony\Component\Form\Extension\Core\Type\TimeType
30+
* Symfony\Component\Form\Extension\Core\Type\TimezoneType
31+
* Symfony\Component\Form\Extension\Core\Type\UrlType
32+
* Symfony\Component\Form\Extension\Core\Type\FileType
33+
* Symfony\Component\Form\Extension\Core\Type\ButtonType
34+
* Symfony\Component\Form\Extension\Core\Type\SubmitType
35+
* Symfony\Component\Form\Extension\Core\Type\ResetType
36+
* Symfony\Component\Form\Extension\Core\Type\CurrencyType
37+
38+
Services form types
39+
-------------------
40+
41+
* Symfony\Bridge\Doctrine\Form\Type\EntityType
42+
43+
Type extensions
44+
---------------
45+
46+
* Symfony\Component\Form\Extension\Csrf\Type\FormTypeCsrfExtension
47+
48+
Type guessers
49+
-------------
50+
51+
* Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser
52+

0 commit comments

Comments
 (0)