CachingFactoryDecorator example


class CoreExtension extends AbstractExtension
{
    private PropertyAccessorInterface $propertyAccessor;
    private ChoiceListFactoryInterface $choiceListFactory;
    private ?TranslatorInterface $translator;

    public function __construct(PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null)
    {
        $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
        $this->choiceListFactory = $choiceListFactory ?? new CachingFactoryDecorator(new PropertyAccessDecorator(new DefaultChoiceListFactory()$this->propertyAccessor));
        $this->translator = $translator;
    }

    protected function loadTypes(): array
    {
        return [
            new Type\FormType($this->propertyAccessor),
            new Type\BirthdayType(),
            new Type\CheckboxType(),
            new Type\ChoiceType($this->choiceListFactory, $this->translator),
            new Type\CollectionType(),
            
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader;

/** * @author Bernhard Schussek <bschussek@gmail.com> */
class CachingFactoryDecoratorTest extends TestCase
{
    private CachingFactoryDecorator $factory;

    protected function setUp(): void
    {
        $this->factory = new CachingFactoryDecorator(new DefaultChoiceListFactory());
    }

    public function testCreateFromChoicesEmpty()
    {
        $list1 = $this->factory->createListFromChoices([]);
        $list2 = $this->factory->createListFromChoices([]);

        $this->assertSame($list1$list2);
        $this->assertEquals(new ArrayChoiceList([])$list1);
        $this->assertEquals(new ArrayChoiceList([])$list2);
    }

    
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Contracts\Translation\TranslatorInterface;

class ChoiceType extends AbstractType
{
    private ChoiceListFactoryInterface $choiceListFactory;
    private ?TranslatorInterface $translator;

    public function __construct(ChoiceListFactoryInterface $choiceListFactory = null, TranslatorInterface $translator = null)
    {
        $this->choiceListFactory = $choiceListFactory ?? new CachingFactoryDecorator(
            new PropertyAccessDecorator(
                new DefaultChoiceListFactory()
            )
        );
        $this->translator = $translator;
    }

    /** * @return void */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
Home | Imprint | This part of the site doesn't use cookies.