Skip to content

Commit

Permalink
introduce repo for choices
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Feb 15, 2024
1 parent b92711d commit 4c47dea
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/DependencyInjection/ValanticPimcoreFormsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<mixed> $configs
Expand All @@ -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');
Expand Down
15 changes: 15 additions & 0 deletions src/Exception/Repository/ItemNotFoundInRepositoryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Exception\Repository;

use Valantic\PimcoreFormsBundle\Exception\BaseException;

class ItemNotFoundInRepositoryException extends BaseException
{
public function __construct(string $key)
{
parent::__construct(sprintf('Item %s not found in repository', $key));
}
}
10 changes: 4 additions & 6 deletions src/Form/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Valantic\PimcoreFormsBundle\Form;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
Expand All @@ -15,16 +14,16 @@
use Symfony\Component\Validator\Constraints\Choice;
use Symfony\Contracts\Translation\TranslatorInterface;
use Valantic\PimcoreFormsBundle\DependencyInjection\Configuration;
use Valantic\PimcoreFormsBundle\Form\Type\ChoicesInterface;
use Valantic\PimcoreFormsBundle\Form\Type\ConfigAwareInterface;
use Valantic\PimcoreFormsBundle\Repository\ChoicesRepository;

class Builder
{
public function __construct(
protected readonly ParameterBagInterface $container,
protected readonly UrlGeneratorInterface $urlGenerator,
protected readonly TranslatorInterface $translator,
protected readonly FormFactoryInterface $formFactory
protected readonly FormFactoryInterface $formFactory,
protected readonly ChoicesRepository $choicesRepository,
) {
}

Expand Down Expand Up @@ -122,8 +121,7 @@ protected function getOptions(string $formName, array $definition, array $formCo
}
}
if (!empty($definition['provider']) && is_string($definition['provider'])) {
/** @var ChoicesInterface $choices */
$choices = $this->container->get($definition['provider']);
$choices = $this->choicesRepository->get($definition['provider']);
if ($choices instanceof ConfigAwareInterface) {
$choices->setFormName($formName);
$choices->setFieldConfig($formConfig);
Expand Down
52 changes: 52 additions & 0 deletions src/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Repository;

use Valantic\PimcoreFormsBundle\Exception\Repository\ItemNotFoundInRepositoryException;

/**
* @template TItem
*
* @internal
*/
abstract class AbstractRepository
{
/** @var TItem[] */
protected array $items;

public function __construct(
/** @var \Iterator<TItem> */
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<class-string<TItem>,TItem>
*/
protected function initializeItemsFromIterables(): array
{
$arr = [];

foreach ($this->iterables as $iterable) {
$arr[$iterable::class] = $iterable;
}

return $arr;
}
}
16 changes: 16 additions & 0 deletions src/Repository/ChoicesRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Repository;

use Valantic\PimcoreFormsBundle\Form\Type\ChoicesInterface;

/**
* @extends AbstractRepository<ChoicesInterface>
*
* @internal
*/
class ChoicesRepository extends AbstractRepository
{
}
4 changes: 4 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c47dea

Please sign in to comment.