create contextmanager and refactor docgeneratorTemplateType

This commit is contained in:
Julien Fastré 2021-11-30 18:35:05 +01:00
parent 0710d6572b
commit 3404d3669c
8 changed files with 132 additions and 2 deletions

View File

@ -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');
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Chill\DocGeneratorBundle\Context;
class ContextManager
{
private iterable $contexts;
/**
* @param iterable $contexts
*/
public function __construct(iterable $contexts)
{
$this->contexts = $contexts;
}
public function getContext(): array
{
return iterator_to_array($this->contexts);
}
}

View File

@ -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.
*

View File

@ -0,0 +1,11 @@
<?php
namespace Chill\DocGeneratorBundle\Context\Exception;
class UnexpectedTypeException extends \LogicException
{
public function __construct($value, string $expectedType)
{
parent::__construct(sprintf('Expected argument of type "%s", "%s" given', $expectedType, \is_object($value) ? \get_class($value) : \gettype($value)));
}
}

View File

@ -23,6 +23,16 @@ use function get_class;
*/
class HouseholdMemberSelectionContext implements DocGeneratorContextInterface
{
public function getName(): string
{
return 'household member';
}
public static function getKey(): string
{
return self::class;
}
/**
* Get the data that will be injected to the generated document.
*

View File

@ -11,6 +11,8 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Form;
use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
use Symfony\Component\Form\AbstractType;
@ -19,17 +21,31 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\MainBundle\Form\Type\TranslatableStringFormType;
class DocGeneratorTemplateType extends AbstractType
{
private ContextManager $contextManager;
public function __construct(ContextManager $contextManager)
{
$this->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' => [

View File

@ -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 }

View File

@ -0,0 +1,48 @@
<?php
namespace Chill\PersonBundle\Service\DocGenerator;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
class AccompanyingPeriodContext implements DocGeneratorContextInterface
{
public NormalizerInterface $normalizer;
public function getName(): string
{
return 'Accompanying Period';
}
public static function getKey(): string
{
return self::class;
}
public function getData($entity): array
{
if (!$entity instanceof AccompanyingPeriod) {
throw new UnexpectedTypeException($entity, AccompanyingPeriod::class);
}
return $this->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;
}
}