From 3404d3669ce60048e42c503d17724ae101bbfd22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 30 Nov 2021 18:35:05 +0100 Subject: [PATCH] create contextmanager and refactor docgeneratorTemplateType --- .../ChillDocGeneratorBundle.php | 10 ++++ .../Context/ContextManager.php | 21 ++++++++ .../Context/DocGeneratorContextInterface.php | 5 ++ .../Exception/UnexpectedTypeException.php | 11 +++++ .../HouseholdMemberSelectionContext.php | 10 ++++ .../Form/DocGeneratorTemplateType.php | 20 +++++++- .../config/services.yaml | 9 ++++ .../AccompanyingPeriodContext.php | 48 +++++++++++++++++++ 8 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php create mode 100644 src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php diff --git a/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php b/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php index d52f0f4b1..d85b1f7ee 100644 --- a/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php +++ b/src/Bundle/ChillDocGeneratorBundle/ChillDocGeneratorBundle.php @@ -11,8 +11,18 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; +use Chill\MainBundle\Routing\LocalMenuBuilderInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; class ChillDocGeneratorBundle extends Bundle { + public function build(ContainerBuilder $container) + { + parent::build($container); + + $container->registerForAutoconfiguration(DocGeneratorContextInterface::class) + ->addTag('chill_docgen.context'); + } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php new file mode 100644 index 000000000..ec065732c --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/ContextManager.php @@ -0,0 +1,21 @@ +contexts = $contexts; + } + + public function getContext(): array + { + return iterator_to_array($this->contexts); + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index 7493f7000..df8c55042 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -16,6 +16,11 @@ namespace Chill\DocGeneratorBundle\Context; */ interface DocGeneratorContextInterface { + + public static function getKey(): string; + + public function getName(): string; + /** * Get the data that will be injected to the generated document. * diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php new file mode 100644 index 000000000..727c12050 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/Exception/UnexpectedTypeException.php @@ -0,0 +1,11 @@ +contextManager = $contextManager; + } + public function buildForm(FormBuilderInterface $builder, array $options) { + $contexts = \array_flip(\array_map(function (DocGeneratorContextInterface $c) { + return $c->getName(); + }, $this->contextManager->getContext())); + $builder ->add('name', TranslatableStringFormType::class, [ 'label' => 'Nom', ]) - ->add('context') + ->add('context', ChoiceType::class, [ + 'required' => true, + 'label' => 'Context', + 'choices' => $contexts + ]) ->add('entities', ChoiceType::class, [ 'multiple' => true, 'choices' => [ diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 6e66115fc..25ed037f7 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -20,3 +20,12 @@ services: resource: "../Controller" autowire: true autoconfigure: true + + Chill\DocGeneratorBundle\Form\: + resource: "../Form/" + autowire: true + autoconfigure: true + + Chill\DocGeneratorBundle\Context\ContextManager: + arguments: + $contexts: !tagged_iterator { tag: chill_docgen.context, default_index_method: getKey } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php new file mode 100644 index 000000000..2f286351e --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -0,0 +1,48 @@ +normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class]); + } + + public function getForm($entity) + { + // TODO: Implement getForm() method. + } + + public function hasForm(): bool + { + return false; + } + + public function supports(string $entityClass): bool + { + return $entityClass === AccompanyingPeriod::class; + } +}