From b92711dfedd789b707b7d9f21dc34c446e22f9b1 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Wed, 14 Feb 2024 08:33:40 +0100 Subject: [PATCH 1/3] use tagged iterator --- src/Repository/OutputRepository.php | 8 ++++++-- src/Resources/config/services.yml | 4 ---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Repository/OutputRepository.php b/src/Repository/OutputRepository.php index de36cdf..45ca787 100644 --- a/src/Repository/OutputRepository.php +++ b/src/Repository/OutputRepository.php @@ -4,6 +4,8 @@ namespace Valantic\PimcoreFormsBundle\Repository; +use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; +use Valantic\PimcoreFormsBundle\DependencyInjection\ValanticPimcoreFormsExtension; use Valantic\PimcoreFormsBundle\Exception\DuplicateOutputException; use Valantic\PimcoreFormsBundle\Exception\UnknownOutputException; use Valantic\PimcoreFormsBundle\Form\Output\OutputInterface; @@ -18,8 +20,10 @@ class OutputRepository /** * @param iterable $outputs */ - public function __construct(iterable $outputs) - { + public function __construct( + #[TaggedIterator(ValanticPimcoreFormsExtension::TAG_OUTPUT)] + iterable $outputs + ) { $this->outputs = $this->iterableToArray($outputs); } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index c90a4f9..5c5b348 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -21,10 +21,6 @@ services: Valantic\PimcoreFormsBundle\Service\: resource: '../../Service/*' - Valantic\PimcoreFormsBundle\Repository\OutputRepository: - arguments: - - !tagged_iterator valantic.pimcore_forms.output - Valantic\PimcoreFormsBundle\Repository\InputHandlerRepository: arguments: - !tagged_iterator valantic.pimcore_forms.input_handler From 4c47deaf7b75f9d57e1d4aa64392360bfe2e125d Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Wed, 14 Feb 2024 08:34:08 +0100 Subject: [PATCH 2/3] introduce repo for choices --- .../ValanticPimcoreFormsExtension.php | 3 ++ .../ItemNotFoundInRepositoryException.php | 15 ++++++ src/Form/Builder.php | 10 ++-- src/Repository/AbstractRepository.php | 52 +++++++++++++++++++ src/Repository/ChoicesRepository.php | 16 ++++++ src/Resources/config/services.yml | 4 ++ 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/Exception/Repository/ItemNotFoundInRepositoryException.php create mode 100644 src/Repository/AbstractRepository.php create mode 100644 src/Repository/ChoicesRepository.php diff --git a/src/DependencyInjection/ValanticPimcoreFormsExtension.php b/src/DependencyInjection/ValanticPimcoreFormsExtension.php index 5dcd55c..9343db6 100644 --- a/src/DependencyInjection/ValanticPimcoreFormsExtension.php +++ b/src/DependencyInjection/ValanticPimcoreFormsExtension.php @@ -11,6 +11,7 @@ use Valantic\PimcoreFormsBundle\Form\InputHandler\InputHandlerInterface; use Valantic\PimcoreFormsBundle\Form\Output\OutputInterface; use Valantic\PimcoreFormsBundle\Form\RedirectHandler\RedirectHandlerInterface; +use Valantic\PimcoreFormsBundle\Form\Type\ChoicesInterface; use Valantic\PimcoreFormsBundle\Repository\ConfigurationRepository; /** @@ -23,6 +24,7 @@ class ValanticPimcoreFormsExtension extends Extension final public const TAG_OUTPUT = 'valantic.pimcore_forms.output'; final public const TAG_REDIRECT_HANDLER = 'valantic.pimcore_forms.redirect_handler'; final public const TAG_INPUT_HANDLER = 'valantic.pimcore_forms.input_handler'; + final public const TAG_CHOICES = 'valantic.pimcore_forms.choices'; /** * @param array $configs @@ -38,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container): void $container->registerForAutoconfiguration(OutputInterface::class)->addTag(self::TAG_OUTPUT); $container->registerForAutoconfiguration(RedirectHandlerInterface::class)->addTag(self::TAG_REDIRECT_HANDLER); $container->registerForAutoconfiguration(InputHandlerInterface::class)->addTag(self::TAG_INPUT_HANDLER); + $container->registerForAutoconfiguration(ChoicesInterface::class)->addTag(self::TAG_CHOICES); $ymlLoader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $ymlLoader->load('services.yml'); diff --git a/src/Exception/Repository/ItemNotFoundInRepositoryException.php b/src/Exception/Repository/ItemNotFoundInRepositoryException.php new file mode 100644 index 0000000..0c04fef --- /dev/null +++ b/src/Exception/Repository/ItemNotFoundInRepositoryException.php @@ -0,0 +1,15 @@ +container->get($definition['provider']); + $choices = $this->choicesRepository->get($definition['provider']); if ($choices instanceof ConfigAwareInterface) { $choices->setFormName($formName); $choices->setFieldConfig($formConfig); diff --git a/src/Repository/AbstractRepository.php b/src/Repository/AbstractRepository.php new file mode 100644 index 0000000..169b4f3 --- /dev/null +++ b/src/Repository/AbstractRepository.php @@ -0,0 +1,52 @@ + */ + protected iterable $iterables, + ) { + } + + /** @return TItem */ + public function get(string $key) + { + return $this->all()[$key] ?? throw new ItemNotFoundInRepositoryException($key); + } + + /** @return TItem[] */ + protected function all(): array + { + $this->items ??= $this->initializeItemsFromIterables(); + + return $this->items; + } + + /** + * @return array,TItem> + */ + protected function initializeItemsFromIterables(): array + { + $arr = []; + + foreach ($this->iterables as $iterable) { + $arr[$iterable::class] = $iterable; + } + + return $arr; + } +} diff --git a/src/Repository/ChoicesRepository.php b/src/Repository/ChoicesRepository.php new file mode 100644 index 0000000..0b7ca88 --- /dev/null +++ b/src/Repository/ChoicesRepository.php @@ -0,0 +1,16 @@ + + * + * @internal + */ +class ChoicesRepository extends AbstractRepository +{ +} diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 5c5b348..822fe1b 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -21,6 +21,10 @@ services: Valantic\PimcoreFormsBundle\Service\: resource: '../../Service/*' + Valantic\PimcoreFormsBundle\Repository\ChoicesRepository: + arguments: + - !tagged_iterator valantic.pimcore_forms.choices + Valantic\PimcoreFormsBundle\Repository\InputHandlerRepository: arguments: - !tagged_iterator valantic.pimcore_forms.input_handler From c0ee2e8957cddaa758f576c74df4eb2b21a9c2b4 Mon Sep 17 00:00:00 2001 From: Linus Metzler Date: Wed, 14 Feb 2024 08:34:16 +0100 Subject: [PATCH 3/3] refactor existing simple repos --- .../UnknownInputHandlerException.php | 13 ----- .../UnknownRedirectHandlerException.php | 13 ----- src/Repository/InputHandlerRepository.php | 54 +++---------------- src/Repository/RedirectHandlerRepository.php | 54 +++---------------- 4 files changed, 12 insertions(+), 122 deletions(-) delete mode 100644 src/Exception/UnknownInputHandlerException.php delete mode 100644 src/Exception/UnknownRedirectHandlerException.php diff --git a/src/Exception/UnknownInputHandlerException.php b/src/Exception/UnknownInputHandlerException.php deleted file mode 100644 index 78159cc..0000000 --- a/src/Exception/UnknownInputHandlerException.php +++ /dev/null @@ -1,13 +0,0 @@ - + * + * @internal + */ +class InputHandlerRepository extends AbstractRepository { - /** - * @var array - */ - protected array $inputHandlers; - - /** - * @param iterable $inputHandlers - */ - public function __construct(iterable $inputHandlers) - { - $this->inputHandlers = $this->iterableToArray($inputHandlers); - } - - public function get(string $key): InputHandlerInterface - { - if (str_starts_with($key, '\\')) { - $key = substr($key, 1); - } - - if (!array_key_exists($key, $this->inputHandlers)) { - throw new UnknownInputHandlerException($key); - } - - return clone $this->inputHandlers[$key]; - } - - /** - * @param iterable $iterables - * - * @return array - */ - public function iterableToArray(iterable $iterables): array - { - $arr = []; - - foreach ($iterables as $iterable) { - if (!($iterable instanceof InputHandlerInterface)) { - continue; - } - - $name = $iterable::class; - $arr[$name] = $iterable; - } - - return $arr; - } } diff --git a/src/Repository/RedirectHandlerRepository.php b/src/Repository/RedirectHandlerRepository.php index 4ab5a6a..6b9a6b2 100644 --- a/src/Repository/RedirectHandlerRepository.php +++ b/src/Repository/RedirectHandlerRepository.php @@ -4,55 +4,13 @@ namespace Valantic\PimcoreFormsBundle\Repository; -use Valantic\PimcoreFormsBundle\Exception\UnknownRedirectHandlerException; use Valantic\PimcoreFormsBundle\Form\RedirectHandler\RedirectHandlerInterface; -class RedirectHandlerRepository +/** + * @extends AbstractRepository + * + * @internal + */ +class RedirectHandlerRepository extends AbstractRepository { - /** - * @var array - */ - protected array $redirectHandlers; - - /** - * @param iterable $redirectHandlers - */ - public function __construct(iterable $redirectHandlers) - { - $this->redirectHandlers = $this->iterableToArray($redirectHandlers); - } - - public function get(string $key): RedirectHandlerInterface - { - if (str_starts_with($key, '\\')) { - $key = substr($key, 1); - } - - if (!array_key_exists($key, $this->redirectHandlers)) { - throw new UnknownRedirectHandlerException($key); - } - - return clone $this->redirectHandlers[$key]; - } - - /** - * @param iterable $iterables - * - * @return array - */ - public function iterableToArray(iterable $iterables): array - { - $arr = []; - - foreach ($iterables as $iterable) { - if (!($iterable instanceof RedirectHandlerInterface)) { - continue; - } - - $name = $iterable::class; - $arr[$name] = $iterable; - } - - return $arr; - } }