8000 Choose the proper type class when it is ambiguous · symfony/symfony@1437e85 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1437e85

Browse files
committed
Choose the proper type class when it is ambiguous
1 parent 399117b commit 1437e85

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class FormDebugCommand extends Command
3333
private $optionsResolver;
3434
private $parents = array();
3535
private $extensions = array();
36-
private $typesFound = array();
3736
private $namespaces = array(
3837
'Symfony\\Bridge\\Doctrine\\Form\\Type',
3938
'Symfony\\Component\\Form\\Extension\\Core\\Type',
@@ -66,15 +65,15 @@ protected function configure()
6665
*/
6766
protected function execute(InputInterface $input, OutputInterface $output)
6867
{
69-
$class = $input->getArgument('class');
70-
if (!class_exists($class) && false === mb_strpos($class, '\\')) {
71-
$class = $this->findFormTypeClass($class);
68+
$io = new SymfonyStyle($input, $output);
69+
70+
if (!class_exists($class = $input->getArgument('class'))) {
71+
$class = $this->findProperTypeClass($input, $io, $class);
7272
}
7373

7474
$type = $this->formRegistry->getType($class);
7575

76-
$io = new SymfonyStyle($input, $output);
77-
$io->section(sprintf('%s (Block prefix: "%s")', get_class($type->getInnerType()), $type->getInnerType()->getBlockPrefix()));
76+
$io->title(sprintf('%s (Block prefix: "%s")', get_class($type->getInnerType()), $type->getInnerType()->getBlockPrefix()));
7877

7978
list($headers, $rows) = $this->buildTableContent($this->getOptionsGroup($type));
8079
$io->table($headers, $rows);
@@ -88,13 +87,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
8887
$io->section('Type extensions');
8988
$io->listing(array_keys($this->extensions));
9089
}
91-
92-
if (count($this->typesFound) > 1) {
93-
$io->block('Similar form types found:', null, 'info');
94-
$io->listing(array_slice($this->typesFound, 1));
95-
$io->comment('To search for a specific class, re-run this command with the fully-qualified class name.');
96-
$io->comment(sprintf('e.g. <comment>debug:form \'%s\'</comment>', $this->typesFound[1]));
97-
}
9890
}
9991

10092
private function getOptionsGroup(ResolvedFormTypeInterface $type)
@@ -157,19 +149,26 @@ private function getParentOptionResolver(ResolvedFormTypeInterface $type)
157149
return $optionsResolver;
158150
}
159151

160-
private function findFormTypeClass($shortClassName)
152+
private function findProperTypeClass(InputInterface $input, SymfonyStyle $io, $shortClassName)
161153
{
154+
$classes = array();
162155
foreach ($this->namespaces as $namespace) {
163156
if (class_exists($fqcn = $namespace.'\\'.$shortClassName)) {
164-
$this->typesFound[] = $fqcn;
157+
$classes[] = $fqcn;
165158
}
166159
}
167160

168-
if (0 === count($this->typesFound)) {
169-
throw new \InvalidArgumentException(sprintf('Could not find type "%s" into the following namespaces: %s.', $shortClassName, implode(', ', $this->namespaces)));
161+
if (0 === $count = count($classes)) {
162+
throw new \InvalidArgumentException(sprintf("Could not find type \"%s\" into the following namespaces:\n %s", $shortClassName, implode("\n ", $this->namespaces)));
163+
}
164+
if (1 === $count) {
165+
return $classes[0];
166+
}
167+
if (!$input->isInteractive()) {
168+
throw new \InvalidArgumentException(sprintf("The type \"%s\" is ambiguous.\nDid you mean one of these?\n %s", $shortClassName, implode("\n ", $classes)));
170169
}
171170

172-
return $this->typesFound[0];
171+
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]);
173172
}
174173

175174
private function buildTableContent($options)

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ private function processFormTypes(ContainerBuilder $container, Definition $defin
7070
$servicesMap[$serviceDefinition->getClass()] = new Reference($serviceId);
7171
}
7272

73-
// Builds an array of namespaces from registered form types
74-
$namespaces = array_map(function ($class) {
75-
return substr($class, 0, strrpos($class, '\\'));
76-
}, array_keys($servicesMap));
77-
$commandDefinition = $container->getDefinition('form.command.debug');
78-
$commandDefinition->setArgument(1, $namespaces);
73+
if ($container->hasDefinition('form.command.debug')) {
74+
// Builds an array of namespaces from registered form types
75+
$namespaces = array_map(function ($class) {
76+
return substr($class, 0, strrpos($class, '\\'));
77+
}, array_keys($servicesMap));
78+
$commandDefinition = $container->getDefinition('form.command.debug');
79+
$commandDefinition->setArgument(1, $namespaces);
80+
}
7981

8082
return ServiceLocatorTagPass::register($container, $servicesMap);
8183
}

0 commit comments

Comments
 (0)
0