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

Open in your IDE?
  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 $proxiedType;
  27.     private $dataCollector;
  28.     public function __construct(ResolvedFormTypeInterface $proxiedTypeFormDataCollectorInterface $dataCollector)
  29.     {
  30.         $this->proxiedType $proxiedType;
  31.         $this->dataCollector $dataCollector;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function getBlockPrefix(): string
  37.     {
  38.         return $this->proxiedType->getBlockPrefix();
  39.     }
  40.     /**
  41.      * {@inheritdoc}
  42.      */
  43.     public function getParent(): ?ResolvedFormTypeInterface
  44.     {
  45.         return $this->proxiedType->getParent();
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function getInnerType(): FormTypeInterface
  51.     {
  52.         return $this->proxiedType->getInnerType();
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function getTypeExtensions(): array
  58.     {
  59.         return $this->proxiedType->getTypeExtensions();
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function createBuilder(FormFactoryInterface $factorystring $name, array $options = []): FormBuilderInterface
  65.     {
  66.         $builder $this->proxiedType->createBuilder($factory$name$options);
  67.         $builder->setAttribute('data_collector/passed_options'$options);
  68.         $builder->setType($this);
  69.         return $builder;
  70.     }
  71.     /**
  72.      * {@inheritdoc}
  73.      */
  74.     public function createView(FormInterface $formFormView $parent null): FormView
  75.     {
  76.         return $this->proxiedType->createView($form$parent);
  77.     }
  78.     /**
  79.      * {@inheritdoc}
  80.      */
  81.     public function buildForm(FormBuilderInterface $builder, array $options)
  82.     {
  83.         $this->proxiedType->buildForm($builder$options);
  84.     }
  85.     /**
  86.      * {@inheritdoc}
  87.      */
  88.     public function buildView(FormView $viewFormInterface $form, array $options)
  89.     {
  90.         $this->proxiedType->buildView($view$form$options);
  91.     }
  92.     /**
  93.      * {@inheritdoc}
  94.      */
  95.     public function finishView(FormView $viewFormInterface $form, array $options)
  96.     {
  97.         $this->proxiedType->finishView($view$form$options);
  98.         // Remember which view belongs to which form instance, so that we can
  99.         // get the collected data for a view when its form instance is not
  100.         // available (e.g. CSRF token)
  101.         $this->dataCollector->associateFormWithView($form$view);
  102.         // Since the CSRF token is only present in the FormView tree, we also
  103.         // need to check the FormView tree instead of calling isRoot() on the
  104.         // FormInterface tree
  105.         if (null === $view->parent) {
  106.             $this->dataCollector->collectViewVariables($view);
  107.             // Re-assemble data, in case FormView instances were added, for
  108.             // which no FormInterface instances were present (e.g. CSRF token).
  109.             // Since finishView() is called after finishing the views of all
  110.             // children, we can safely assume that information has been
  111.             // collected about the complete form tree.
  112.             $this->dataCollector->buildFinalFormTree($form$view);
  113.         }
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function getOptionsResolver(): OptionsResolver
  119.     {
  120.         return $this->proxiedType->getOptionsResolver();
  121.     }
  122. }