From d43396c9308d7cd5c430442438283d0c5ee74c7e Mon Sep 17 00:00:00 2001 From: Alexander Kulinich Date: Thu, 2 Jul 2020 22:57:05 +0300 Subject: [PATCH] ability to change information for types of forms --- .../FormInfoParserCompilerPass.php | 24 ++++++++++ DependencyInjection/NelmioApiDocExtension.php | 6 +++ NelmioApiDocBundle.php | 2 + Parser/FormInfoParser.php | 12 +++++ Parser/FormTypeParser.php | 44 ++++++++++++++++++- composer.json | 2 +- 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 DependencyInjection/FormInfoParserCompilerPass.php create mode 100644 Parser/FormInfoParser.php diff --git a/DependencyInjection/FormInfoParserCompilerPass.php b/DependencyInjection/FormInfoParserCompilerPass.php new file mode 100644 index 0000000..3a8b576 --- /dev/null +++ b/DependencyInjection/FormInfoParserCompilerPass.php @@ -0,0 +1,24 @@ +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)]); + } + } +} diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 5af3f32..9896c25 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -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'); diff --git a/NelmioApiDocBundle.php b/NelmioApiDocBundle.php index 4b42b20..3b8dbf9 100644 --- a/NelmioApiDocBundle.php +++ b/NelmioApiDocBundle.php @@ -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()); } } diff --git a/Parser/FormInfoParser.php b/Parser/FormInfoParser.php new file mode 100644 index 0000000..07bb495 --- /dev/null +++ b/Parser/FormInfoParser.php @@ -0,0 +1,12 @@ +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(); diff --git a/composer.json b/composer.json index 78ddc05..5330426 100644 --- a/composer.json +++ b/composer.json @@ -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",