vendor/symfony/form/Extension/DataCollector/Proxy/ResolvedTypeDataCollectorProxy.php line 81

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\DataCollector\Proxy;
  11. use Symfony\Component\Form\Extension\DataCollector\FormDataCollectorInterface;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormTypeInterface;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\Form\ResolvedFormTypeInterface;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. /**
  20.  * Proxy that invokes a data collector when creating a form and its view.
  21.  *
  22.  * @author Bernhard Schussek <bschussek@gmail.com>
  23.  */
  24. class ResolvedTypeDataCollectorProxy implements ResolvedFormTypeInterface
  25. {
  26.     private ResolvedFormTypeInterface $proxiedType;
  27.     private FormDataCollectorInterface $dataCollector;
  28.     public function __construct(ResolvedFormTypeInterface $proxiedTypeFormDataCollectorInterface $dataCollector)
  29.     {
  30.         $this->proxiedType $proxiedType;
  31.         $this->dataCollector $dataCollector;
  32.     }
  33.     public function getBlockPrefix(): string
  34.     {
  35.         return $this->proxiedType->getBlockPrefix();
  36.     }
  37.     public function getParent(): ?ResolvedFormTypeInterface
  38.     {
  39.         return $this->proxiedType->getParent();
  40.     }
  41.     public function getInnerType(): FormTypeInterface
  42.     {
  43.         return $this->proxiedType->getInnerType();
  44.     }
  45.     public function getTypeExtensions(): array
  46.     {
  47.         return $this->proxiedType->getTypeExtensions();
  48.     }
  49.     public function createBuilder(FormFactoryInterface $factorystring $name, array $options = []): FormBuilderInterface
  50.     {
  51.         $builder $this->proxiedType->createBuilder($factory$name$options);
  52.         $builder->setAttribute('data_collector/passed_options'$options);
  53.         $builder->setType($this);
  54.         return $builder;
  55.     }
  56.     public function createView(FormInterface $formFormView $parent null): FormView
  57.     {
  58.         return $this->proxiedType->createView($form$parent);
  59.     }
  60.     public function buildForm(FormBuilderInterface $builder, array $options)
  61.     {
  62.         $this->proxiedType->buildForm($builder$options);
  63.     }
  64.     public function buildView(FormView $viewFormInterface $form, array $options)
  65.     {
  66.         $this->proxiedType->buildView($view$form$options);
  67.     }
  68.     public function finishView(FormView $viewFormInterface $form, array $options)
  69.     {
  70.         $this->proxiedType->finishView($view$form$options);
  71.         // Remember which view belongs to which form instance, so that we can
  72.         // get the collected data for a view when its form instance is not
  73.         // available (e.g. CSRF token)
  74.         $this->dataCollector->associateFormWithView($form$view);
  75.         // Since the CSRF token is only present in the FormView tree, we also
  76.         // need to check the FormView tree instead of calling isRoot() on the
  77.         // FormInterface tree
  78.         if (null === $view->parent) {
  79.             $this->dataCollector->collectViewVariables($view);
  80.             // Re-assemble data, in case FormView instances were added, for
  81.             // which no FormInterface instances were present (e.g. CSRF token).
  82.             // Since finishView() is called after finishing the views of all
  83.             // children, we can safely assume that information has been
  84.             // collected about the complete form tree.
  85.             $this->dataCollector->buildFinalFormTree($form$view);
  86.         }
  87.     }
  88.     public function getOptionsResolver(): OptionsResolver
  89.     {
  90.         return $this->proxiedType->getOptionsResolver();
  91.     }
  92. }