mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
ability to change information for types of forms
This commit is contained in:
parent
99b9d4f486
commit
d43396c930
24
DependencyInjection/FormInfoParserCompilerPass.php
Normal file
24
DependencyInjection/FormInfoParserCompilerPass.php
Normal 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)]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\DependencyInjection;
|
namespace Nelmio\ApiDocBundle\DependencyInjection;
|
||||||
|
|
||||||
|
use Nelmio\ApiDocBundle\Parser\FormInfoParser;
|
||||||
use Symfony\Component\Config\FileLocator;
|
use Symfony\Component\Config\FileLocator;
|
||||||
use Symfony\Component\DependencyInjection\Definition;
|
use Symfony\Component\DependencyInjection\Definition;
|
||||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
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.request_format.formats', $config['sandbox']['request_format']['formats']);
|
||||||
$container->setParameter('nelmio_api_doc.sandbox.entity_to_choice', $config['sandbox']['entity_to_choice']);
|
$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 = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||||
$loader->load('formatters.xml');
|
$loader->load('formatters.xml');
|
||||||
$loader->load('services.xml');
|
$loader->load('services.xml');
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace Nelmio\ApiDocBundle;
|
namespace Nelmio\ApiDocBundle;
|
||||||
|
|
||||||
use Nelmio\ApiDocBundle\DependencyInjection\AnnotationsProviderCompilerPass;
|
use Nelmio\ApiDocBundle\DependencyInjection\AnnotationsProviderCompilerPass;
|
||||||
|
use Nelmio\ApiDocBundle\DependencyInjection\FormInfoParserCompilerPass;
|
||||||
use Nelmio\ApiDocBundle\DependencyInjection\SwaggerConfigCompilerPass;
|
use Nelmio\ApiDocBundle\DependencyInjection\SwaggerConfigCompilerPass;
|
||||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
@ -21,5 +22,6 @@ class NelmioApiDocBundle extends Bundle
|
|||||||
$container->addCompilerPass(new ExtractorHandlerCompilerPass());
|
$container->addCompilerPass(new ExtractorHandlerCompilerPass());
|
||||||
$container->addCompilerPass(new AnnotationsProviderCompilerPass());
|
$container->addCompilerPass(new AnnotationsProviderCompilerPass());
|
||||||
$container->addCompilerPass(new SwaggerConfigCompilerPass());
|
$container->addCompilerPass(new SwaggerConfigCompilerPass());
|
||||||
|
$container->addCompilerPass(new FormInfoParserCompilerPass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
Parser/FormInfoParser.php
Normal file
12
Parser/FormInfoParser.php
Normal 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;
|
||||||
|
}
|
@ -17,9 +17,10 @@ use Symfony\Component\Form\Exception\FormException;
|
|||||||
use Symfony\Component\Form\Exception\InvalidArgumentException;
|
use Symfony\Component\Form\Exception\InvalidArgumentException;
|
||||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||||
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
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\FormFactoryInterface;
|
||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
|
use Symfony\Component\Form\FormTypeInterface;
|
||||||
use Symfony\Component\Form\ResolvedFormTypeInterface;
|
use Symfony\Component\Form\ResolvedFormTypeInterface;
|
||||||
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
|
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
@ -46,6 +47,11 @@ class FormTypeParser implements ParserInterface
|
|||||||
*/
|
*/
|
||||||
protected $entityToChoice;
|
protected $entityToChoice;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array|FormInfoParser[]
|
||||||
|
*/
|
||||||
|
protected $formInfoParsers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @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)
|
private function getDataType($type)
|
||||||
{
|
{
|
||||||
foreach ($this->extendedMapTypes as $data => $types) {
|
foreach ($this->extendedMapTypes as $data => $types) {
|
||||||
@ -223,6 +251,20 @@ class FormTypeParser implements ParserInterface
|
|||||||
$type instanceof FormInterface || $type instanceof ResolvedFormTypeInterface;
|
$type instanceof FormInterface || $type instanceof ResolvedFormTypeInterface;
|
||||||
$type = $type->getParent()
|
$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') ?
|
$typeName = method_exists($type, 'getBlockPrefix') ?
|
||||||
$type->getBlockPrefix() : $type->getName();
|
$type->getBlockPrefix() : $type->getName();
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.4",
|
"php": ">=7.1",
|
||||||
"symfony/twig-bundle": "~2.3|~3.0|~4.0",
|
"symfony/twig-bundle": "~2.3|~3.0|~4.0",
|
||||||
"symfony/framework-bundle": "~2.3|~3.0|~4.0",
|
"symfony/framework-bundle": "~2.3|~3.0|~4.0",
|
||||||
"symfony/console": "~2.3|~3.0|~4.0",
|
"symfony/console": "~2.3|~3.0|~4.0",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user