ability to change information for types of forms

This commit is contained in:
Alexander Kulinich 2020-07-02 22:57:05 +03:00
parent 99b9d4f486
commit d43396c930
6 changed files with 88 additions and 2 deletions

View File

@ -0,0 +1,24 @@
<?php
namespace Nelmio\ApiDocBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class FormInfoParserCompilerPass implements CompilerPassInterface
{
const TAG_NAME = 'nelmio_api_doc.extractor.form_info_parser';
public function process(ContainerBuilder $container)
{
if (!$container->has('nelmio_api_doc.parser.form_type_parser')) {
return;
}
$formParser = $container->findDefinition('nelmio_api_doc.parser.form_type_parser');
foreach ($container->findTaggedServiceIds(self::TAG_NAME) as $id => $tags) {
$formParser->addMethodCall('addFormInfoParser', [new Reference($id)]);
}
}
}

View File

@ -11,6 +11,7 @@
namespace Nelmio\ApiDocBundle\DependencyInjection;
use Nelmio\ApiDocBundle\Parser\FormInfoParser;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@ -43,6 +44,11 @@ class NelmioApiDocExtension extends Extension
$container->setParameter('nelmio_api_doc.sandbox.request_format.formats', $config['sandbox']['request_format']['formats']);
$container->setParameter('nelmio_api_doc.sandbox.entity_to_choice', $config['sandbox']['entity_to_choice']);
if (method_exists($container, 'registerForAutoconfiguration')) {
$container->registerForAutoconfiguration(FormInfoParser::class)
->addTag(FormInfoParserCompilerPass::TAG_NAME);
}
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('formatters.xml');
$loader->load('services.xml');

View File

@ -3,6 +3,7 @@
namespace Nelmio\ApiDocBundle;
use Nelmio\ApiDocBundle\DependencyInjection\AnnotationsProviderCompilerPass;
use Nelmio\ApiDocBundle\DependencyInjection\FormInfoParserCompilerPass;
use Nelmio\ApiDocBundle\DependencyInjection\SwaggerConfigCompilerPass;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
@ -21,5 +22,6 @@ class NelmioApiDocBundle extends Bundle
$container->addCompilerPass(new ExtractorHandlerCompilerPass());
$container->addCompilerPass(new AnnotationsProviderCompilerPass());
$container->addCompilerPass(new SwaggerConfigCompilerPass());
$container->addCompilerPass(new FormInfoParserCompilerPass());
}
}

12
Parser/FormInfoParser.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace Nelmio\ApiDocBundle\Parser;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormTypeInterface;
interface FormInfoParser
{
public function parseFormType(FormTypeInterface $type, FormConfigInterface $config): ?array;
}

View File

@ -17,9 +17,10 @@ use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\Translation\TranslatorInterface;
@ -46,6 +47,11 @@ class FormTypeParser implements ParserInterface
*/
protected $entityToChoice;
/**
* @var array|FormInfoParser[]
*/
protected $formInfoParsers = [];
/**
* @var array
*
@ -197,6 +203,28 @@ class FormTypeParser implements ParserInterface
);
}
public function addFormInfoParser(FormInfoParser $formInfoParser)
{
$class = get_class($formInfoParser);
if (isset($this->formInfoParsers[$class])) {
throw new \InvalidArgumentException($class . ' already added');
}
$this->formInfoParsers[$class] = $formInfoParser;
}
private function parseFormType(FormTypeInterface $type, FormConfigInterface $config): ?array
{
foreach ($this->formInfoParsers as $parser) {
$customInfo = $parser->parseFormType($type, $config);
if ($customInfo) {
return $customInfo;
}
}
return null;
}
private function getDataType($type)
{
foreach ($this->extendedMapTypes as $data => $types) {
@ -223,6 +251,20 @@ class FormTypeParser implements ParserInterface
$type instanceof FormInterface || $type instanceof ResolvedFormTypeInterface;
$type = $type->getParent()
) {
$customInfo = $this->parseFormType($type->getInnerType(), $config);
if ($customInfo) {
$parameters[$name] = array_merge([
'dataType' => 'string',
'actualType' => 'string',
'default' => $config->getData(),
'required' => $config->getRequired(),
'description' => $this->getFormDescription($config, $domain),
'readonly' => $config->getDisabled(),
], $customInfo);
continue 2;
}
$typeName = method_exists($type, 'getBlockPrefix') ?
$type->getBlockPrefix() : $type->getName();

View File

@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=7.1",
"symfony/twig-bundle": "~2.3|~3.0|~4.0",
"symfony/framework-bundle": "~2.3|~3.0|~4.0",
"symfony/console": "~2.3|~3.0|~4.0",