mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 02:59:27 +03:00
Merge pull request #81 from docteurklein/fix-supports-formtype-parser
Fix supports formtype parser
This commit is contained in:
commit
bd625aebed
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Parser;
|
namespace Nelmio\ApiDocBundle\Parser;
|
||||||
|
|
||||||
|
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
|
||||||
|
use Symfony\Component\Form\FormRegistry;
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
use Symfony\Component\Form\Exception\FormException;
|
use Symfony\Component\Form\Exception\FormException;
|
||||||
|
|
||||||
@ -36,9 +38,10 @@ class FormTypeParser implements ParserInterface
|
|||||||
'country' => 'string',
|
'country' => 'string',
|
||||||
);
|
);
|
||||||
|
|
||||||
public function __construct(FormFactoryInterface $formFactory)
|
public function __construct(FormFactoryInterface $formFactory, FormRegistry $formRegistry)
|
||||||
{
|
{
|
||||||
$this->formFactory = $formFactory;
|
$this->formFactory = $formFactory;
|
||||||
|
$this->formRegistry = $formRegistry;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,16 +50,16 @@ class FormTypeParser implements ParserInterface
|
|||||||
public function supports($item)
|
public function supports($item)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (is_string($item) && class_exists($item)) {
|
if ($this->createForm($item)) {
|
||||||
$item = new $item();
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$form = $this->formFactory->create($item);
|
|
||||||
} catch (FormException $e) {
|
} catch (FormException $e) {
|
||||||
return false;
|
return false;
|
||||||
|
} catch (MissingOptionsException $e) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,8 +67,8 @@ class FormTypeParser implements ParserInterface
|
|||||||
*/
|
*/
|
||||||
public function parse($type)
|
public function parse($type)
|
||||||
{
|
{
|
||||||
if (is_string($type) && class_exists($type)) {
|
if ($this->implementsType($type)) {
|
||||||
$type = new $type();
|
$type = $this->getTypeInstance($type);
|
||||||
}
|
}
|
||||||
|
|
||||||
$form = $this->formFactory->create($type);
|
$form = $this->formFactory->create($type);
|
||||||
@ -111,4 +114,31 @@ class FormTypeParser implements ParserInterface
|
|||||||
|
|
||||||
return $parameters;
|
return $parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function implementsType($item)
|
||||||
|
{
|
||||||
|
if (!class_exists($item)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
$refl = new \ReflectionClass($item);
|
||||||
|
|
||||||
|
return $refl->implementsInterface('Symfony\Component\Form\FormTypeInterface');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTypeInstance($type)
|
||||||
|
{
|
||||||
|
return unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type));
|
||||||
|
}
|
||||||
|
|
||||||
|
private function createForm($item)
|
||||||
|
{
|
||||||
|
if ($this->implementsType($item)) {
|
||||||
|
$type = $this->getTypeInstance($item);
|
||||||
|
|
||||||
|
return $this->formFactory->create($type);
|
||||||
|
}
|
||||||
|
if ($this->formRegistry->hasType($item)) {
|
||||||
|
return $this->formFactory->create($item);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
<services>
|
<services>
|
||||||
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
||||||
<argument type="service" id="form.factory" />
|
<argument type="service" id="form.factory" />
|
||||||
|
<argument type="service" id="form.registry" />
|
||||||
<tag name="nelmio_api_doc.extractor.parser" />
|
<tag name="nelmio_api_doc.extractor.parser" />
|
||||||
</service>
|
</service>
|
||||||
<service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" />
|
<service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user