8000 Search type class into registered form namespaces too · symfony/symfony@4fc79b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fc79b9

Browse files
committed
Search type class into registered form namespaces too
1 parent a4938b6 commit 4fc79b9

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
*/
3030
class FormDebugCommand extends Command
3131
{
32-
/** @var OptionsResolver */
32+
private $formRegistry;
3333
private $optionsResolver;
3434
private $parents = array();
3535
private $extensions = array();
36-
private $formRegistry;
36+
private $typesFound = array();
3737

3838
public function __construct(FormRegistryInterface $formRegistry)
3939
{
@@ -62,8 +62,8 @@ protected function configure()
6262
protected function execute(InputInterface $input, OutputInterface $output)
6363
{
6464
$class = $input->getArgument('class');
65-
if (!class_exists($class)) {
66-
$class = $this->getBuiltinFormTypeClass($class);
65+
if (!class_exists($class) && false === mb_strpos($class, '\\')) {
66+
$class = $this->findFormTypeClass($class);
6767
}
6868

6969
$type = $this->formRegistry->getType($class);
@@ -83,6 +83,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
8383
$io->section('Type extensions');
8484
$io->listing(array_keys($this->extensions));
8585
}
86+
87+
if (count($this->typesFound) > 1) {
88+
$io->block('Similar form types found:', null, 'info');
89+
$io->listing(array_slice($this->typesFound, 1));
90+
$io->comment('To search for a specific class, re-run this command with the fully-qualified class name.');
91+
$io->comment(sprintf('e.g. <comment>debug:form \'%s\'</comment>', $this->typesFound[1]));
92+
}
8693
}
8794

8895
private function getOptionsGroup(ResolvedFormTypeInterface $type)
@@ -145,20 +152,48 @@ private function getParentOptionResolver(ResolvedFormTypeInterface $type)
145152
return $optionsResolver;
146153
}
147154

148-
private function getBuiltinFormTypeClass($class)
155+
private function findFormTypeClass($shortClassName)
149156
{
150-
$namespaces = array(
151-
'Symfony\\Component\\Form\\Extension\\Core\\Type',
152-
'Symfony\\Bridge\\Doctrine\\Form\\Type',
153-
);
157+
$namespaces = $this->getRegisteredFormNamespaces();
154158

155159
foreach ($namespaces as $namespace) {
156-
if (class_exists($fqcn = $namespace.'\\'.$class)) {
157-
return $fqcn;
160+
if (class_exists($fqcn = $namespace.'\\'.$shortClassName)) {
161+
$this->typesFound[] = $fqcn;
158162
}
159163
}
160164

161-
throw new \InvalidArgumentException(sprintf('Could not find built-in type "%s" into the following namespaces: %s.', $class, implode(', ', $namespaces)));
165+
if (0 === count($this->typesFound)) {
166+
throw new \InvalidArgumentException(sprintf('Could not find type "%s" into the following namespaces: %s.', $shortClassName, implode(', ', $namespaces)));
167+
}
168+
169+
return $this->typesFound[0];
170+
}
171+
172+
private function getRegisteredFormNamespaces()
173+
{
174+
// Registered form namespaces
175+
$types = array();
176+
foreach ($this->formRegistry->getExtensions() as $extension) {
177+
$refTypeContainer = (new \ReflectionObject($extension))->getProperty('typeContainer');
178+
$refTypeContainer->setAccessible(true);
179+
$typeContainer = $refTypeContainer->getValue($extension);
180+
181+
$refFactories = (new \ReflectionObject($typeContainer))->getProperty('factories');
182+
$refFactories->setAccessible(true);
183+
184+
$types = array_merge($types, array_keys($refFactories->getValue($typeContainer)));
185+
}
186+
$registeredNamespaces = array_map(function ($class) {
187+
return substr($class, 0, strrpos($class, '\\'));
188+
}, $types);
189+
190+
// Core namespaces
191+
$coreNamespaces = array(
192+
'Symfony\\Bridge\\Doctrine\\Form\\Type',
193+
'Symfony\\Component\\Form\\Extension\\Core\\Type',
194+
);
195+
196+
return array_unique(array_merge($registeredNamespaces, $coreNamespaces));
162197
}
163198

164199
private function buildTableContent($options)

0 commit comments

Comments
 (0)
0