mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
FormTypeParser: FormType constructor should be called when possible
This commit is contained in:
parent
4fa50ebe53
commit
e2c2d00075
@ -279,7 +279,15 @@ class FormTypeParser implements ParserInterface
|
||||
|
||||
private function getTypeInstance($type)
|
||||
{
|
||||
return unserialize(sprintf('O:%d:"%s":0:{}', strlen($type), $type));
|
||||
$refl = new \ReflectionClass($type);
|
||||
$constructor = $refl->getConstructor();
|
||||
|
||||
// this fallback may lead to runtime exception, but try hard to generate the docs
|
||||
if ($constructor && $constructor->getNumberOfRequiredParameters() > 0) {
|
||||
return $refl->newInstanceWithoutConstructor();
|
||||
}
|
||||
|
||||
return $refl->newInstance();
|
||||
}
|
||||
|
||||
private function createForm($item)
|
||||
|
57
Tests/Fixtures/Form/RequireConstructionType.php
Normal file
57
Tests/Fixtures/Form/RequireConstructionType.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the NelmioApiDocBundle.
|
||||
*
|
||||
* (c) Nelmio <hello@nelm.io>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Tests\Fixtures\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||
|
||||
class RequireConstructionType extends AbstractType
|
||||
{
|
||||
private $noThrow;
|
||||
|
||||
public function __construct($optionalArgs = null)
|
||||
{
|
||||
$this->noThrow = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
if ($this->noThrow !== true)
|
||||
throw new \RuntimeException(__CLASS__ . " require contruction");
|
||||
|
||||
$builder
|
||||
->add('a', null, array('description' => 'A nice description'))
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
|
||||
));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'require_construction_type';
|
||||
}
|
||||
}
|
@ -1080,17 +1080,26 @@ With multiple lines.',
|
||||
'description' => '',
|
||||
'sinceVersion' => null,
|
||||
'untilVersion' => null,
|
||||
'actualType' => DataTypes::MODEL,
|
||||
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
|
||||
'default' => null,
|
||||
'children' => array(
|
||||
'a' => array(
|
||||
'dataType' => 'string',
|
||||
'format' => '{length: min: foo}, {not blank}',
|
||||
'required' => true,
|
||||
'readonly' => null
|
||||
'readonly' => null,
|
||||
'actualType' => DataTypes::STRING,
|
||||
'subType' => null,
|
||||
'default' => 'nelmio',
|
||||
),
|
||||
'b' => array(
|
||||
'dataType' => 'DateTime',
|
||||
'required' => null,
|
||||
'readonly' => null
|
||||
'readonly' => null,
|
||||
'actualType' => DataTypes::DATETIME,
|
||||
'subType' => null,
|
||||
'default' => null,
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -1207,17 +1216,26 @@ With multiple lines.',
|
||||
'description' => '',
|
||||
'sinceVersion' => null,
|
||||
'untilVersion' => null,
|
||||
'actualType' => DataTypes::MODEL,
|
||||
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\Test',
|
||||
'default' => null,
|
||||
'children' => array(
|
||||
'a' => array(
|
||||
'dataType' => 'string',
|
||||
'format' => '{length: min: foo}, {not blank}',
|
||||
'required' => true,
|
||||
'readonly' => null
|
||||
'readonly' => null,
|
||||
'actualType' => DataTypes::STRING,
|
||||
'subType' => null,
|
||||
'default' => 'nelmio',
|
||||
),
|
||||
'b' => array(
|
||||
'dataType' => 'DateTime',
|
||||
'required' => null,
|
||||
'readonly' => null
|
||||
'readonly' => null,
|
||||
'actualType' => DataTypes::DATETIME,
|
||||
'subType' => null,
|
||||
'default' => null,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
@ -473,7 +473,57 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase
|
||||
'readonly' => false,
|
||||
),
|
||||
),
|
||||
)
|
||||
),
|
||||
array(
|
||||
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType'),
|
||||
array(
|
||||
'require_construction_type' => array(
|
||||
'dataType' => 'object (RequireConstructionType)',
|
||||
'required' => true,
|
||||
'description' => '',
|
||||
'readonly' => false,
|
||||
'default' => null,
|
||||
'actualType' => 'model',
|
||||
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\RequireConstructionType',
|
||||
'children' => array(
|
||||
'a' => array(
|
||||
'dataType' => 'string',
|
||||
'actualType' => 'string',
|
||||
'subType' => null,
|
||||
'default' => null,
|
||||
'required' => true,
|
||||
'description' => 'A nice description',
|
||||
'readonly' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType'),
|
||||
array(
|
||||
'dependency_type' => array(
|
||||
'dataType' => 'object (DependencyType)',
|
||||
'required' => true,
|
||||
'description' => '',
|
||||
'readonly' => false,
|
||||
'default' => null,
|
||||
'actualType' => 'model',
|
||||
'subType' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Form\DependencyType',
|
||||
'children' => array(
|
||||
'a' => array(
|
||||
'dataType' => 'string',
|
||||
'actualType' => 'string',
|
||||
'subType' => null,
|
||||
'default' => null,
|
||||
'required' => true,
|
||||
'description' => 'A nice description',
|
||||
'readonly' => false,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user