From 5c52352eb54da189cac63d34569f8093da810aea Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Wed, 3 Oct 2012 16:35:56 +0200 Subject: [PATCH 1/5] allow to pass classes wich have constructor arguments --- Parser/FormTypeParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 6a25257..5ae5752 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -48,7 +48,7 @@ class FormTypeParser implements ParserInterface { try { if (is_string($item) && class_exists($item)) { - $item = new $item(); + $item = unserialize(sprintf('O:%d:"%s":0:{}', strlen($item), $item)); } $form = $this->formFactory->create($item); From f9d4fb90cd70f12f0e883031de716394edd49cd2 Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Wed, 3 Oct 2012 16:39:07 +0200 Subject: [PATCH 2/5] allow to pass classes wich have constructor arguments --- Parser/FormTypeParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 5ae5752..43f815d 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -65,7 +65,7 @@ class FormTypeParser implements ParserInterface public function parse($type) { if (is_string($type) && class_exists($type)) { - $type = new $type(); + $item = unserialize(sprintf('O:%d:"%s":0:{}', strlen($item), $item)); } $form = $this->formFactory->create($type); From 734dd0da7e50d8dab771c9ad198c5c57eb2d5db3 Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Wed, 3 Oct 2012 17:06:56 +0200 Subject: [PATCH 3/5] fix bad variable name --- Parser/FormTypeParser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 43f815d..ce2d879 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -65,7 +65,7 @@ class FormTypeParser implements ParserInterface public function parse($type) { if (is_string($type) && class_exists($type)) { - $item = unserialize(sprintf('O:%d:"%s":0:{}', strlen($item), $item)); + $type = unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type)); } $form = $this->formFactory->create($type); From 011c59b4d7b8659b1ebed49c01e8e44d625696f0 Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Thu, 4 Oct 2012 10:37:12 +0200 Subject: [PATCH 4/5] do not support form types with required options --- Parser/FormTypeParser.php | 54 +++++++++++++++++++++++++++------ Resources/config/formatters.xml | 1 + 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index ce2d879..de746d4 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -11,6 +11,10 @@ namespace Nelmio\ApiDocBundle\Parser; +use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; + +use Symfony\Component\Form\FormRegistry; + use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\Exception\FormException; @@ -36,9 +40,10 @@ class FormTypeParser implements ParserInterface '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 +52,18 @@ class FormTypeParser implements ParserInterface public function supports($item) { try { - if (is_string($item) && class_exists($item)) { - $item = unserialize(sprintf('O:%d:"%s":0:{}', strlen($item), $item)); + if ($this->createForm($item)) { + return true; } - - $form = $this->formFactory->create($item); - } catch (FormException $e) { + } + catch (FormException $e) { + return false; + } + catch (MissingOptionsException $e) { return false; } - return true; + return false; } /** @@ -64,8 +71,8 @@ class FormTypeParser implements ParserInterface */ public function parse($type) { - if (is_string($type) && class_exists($type)) { - $type = unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type)); + if ($this->implementsType($type)) { + $type = $this->getTypeInstance($type); } $form = $this->formFactory->create($type); @@ -111,4 +118,31 @@ class FormTypeParser implements ParserInterface 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); + } + } } diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index 42f189c..bc1dcbf 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -15,6 +15,7 @@ + From df29283a7f19bbb2cdf7977e6eec3fa6fd8dbb21 Mon Sep 17 00:00:00 2001 From: Klein Florian Date: Fri, 5 Oct 2012 09:36:43 +0200 Subject: [PATCH 5/5] fix cs --- Parser/FormTypeParser.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index de746d4..09719f4 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -12,9 +12,7 @@ namespace Nelmio\ApiDocBundle\Parser; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; - use Symfony\Component\Form\FormRegistry; - use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\Exception\FormException; @@ -55,11 +53,9 @@ class FormTypeParser implements ParserInterface if ($this->createForm($item)) { return true; } - } - catch (FormException $e) { + } catch (FormException $e) { return false; - } - catch (MissingOptionsException $e) { + } catch (MissingOptionsException $e) { return false; }