vendor/symfony/form/Extension/DataCollector/FormDataCollector.php line 240

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;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Form\FormView;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\DataCollector\DataCollector;
  16. use Symfony\Component\Validator\ConstraintViolationInterface;
  17. use Symfony\Component\VarDumper\Caster\Caster;
  18. use Symfony\Component\VarDumper\Caster\ClassStub;
  19. use Symfony\Component\VarDumper\Caster\StubCaster;
  20. use Symfony\Component\VarDumper\Cloner\Stub;
  21. /**
  22.  * Data collector for {@link FormInterface} instances.
  23.  *
  24.  * @author Robert Schönthal <robert.schoenthal@gmail.com>
  25.  * @author Bernhard Schussek <bschussek@gmail.com>
  26.  */
  27. class FormDataCollector extends DataCollector implements FormDataCollectorInterface
  28. {
  29.     private $dataExtractor;
  30.     /**
  31.      * Stores the collected data per {@link FormInterface} instance.
  32.      *
  33.      * Uses the hashes of the forms as keys. This is preferable over using
  34.      * {@link \SplObjectStorage}, because in this way no references are kept
  35.      * to the {@link FormInterface} instances.
  36.      *
  37.      * @var array
  38.      */
  39.     private $dataByForm;
  40.     /**
  41.      * Stores the collected data per {@link FormView} instance.
  42.      *
  43.      * Uses the hashes of the views as keys. This is preferable over using
  44.      * {@link \SplObjectStorage}, because in this way no references are kept
  45.      * to the {@link FormView} instances.
  46.      *
  47.      * @var array
  48.      */
  49.     private $dataByView;
  50.     /**
  51.      * Connects {@link FormView} with {@link FormInterface} instances.
  52.      *
  53.      * Uses the hashes of the views as keys and the hashes of the forms as
  54.      * values. This is preferable over storing the objects directly, because
  55.      * this way they can safely be discarded by the GC.
  56.      *
  57.      * @var array
  58.      */
  59.     private $formsByView;
  60.     public function __construct(FormDataExtractorInterface $dataExtractor)
  61.     {
  62.         if (!class_exists(ClassStub::class)) {
  63.             throw new \LogicException(sprintf('The VarDumper component is needed for using the %s class. Install symfony/var-dumper version 3.4 or above.'__CLASS__));
  64.         }
  65.         $this->dataExtractor $dataExtractor;
  66.         $this->reset();
  67.     }
  68.     /**
  69.      * Does nothing. The data is collected during the form event listeners.
  70.      */
  71.     public function collect(Request $requestResponse $response, \Exception $exception null)
  72.     {
  73.     }
  74.     public function reset()
  75.     {
  76.         $this->data = [
  77.             'forms' => [],
  78.             'forms_by_hash' => [],
  79.             'nb_errors' => 0,
  80.         ];
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function associateFormWithView(FormInterface $formFormView $view)
  86.     {
  87.         $this->formsByView[spl_object_hash($view)] = spl_object_hash($form);
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function collectConfiguration(FormInterface $form)
  93.     {
  94.         $hash spl_object_hash($form);
  95.         if (!isset($this->dataByForm[$hash])) {
  96.             $this->dataByForm[$hash] = [];
  97.         }
  98.         $this->dataByForm[$hash] = array_replace(
  99.             $this->dataByForm[$hash],
  100.             $this->dataExtractor->extractConfiguration($form)
  101.         );
  102.         foreach ($form as $child) {
  103.             $this->collectConfiguration($child);
  104.         }
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function collectDefaultData(FormInterface $form)
  110.     {
  111.         $hash spl_object_hash($form);
  112.         if (!isset($this->dataByForm[$hash])) {
  113.             $this->dataByForm[$hash] = [];
  114.         }
  115.         $this->dataByForm[$hash] = array_replace(
  116.             $this->dataByForm[$hash],
  117.             $this->dataExtractor->extractDefaultData($form)
  118.         );
  119.         foreach ($form as $child) {
  120.             $this->collectDefaultData($child);
  121.         }
  122.     }
  123.     /**
  124.      * {@inheritdoc}
  125.      */
  126.     public function collectSubmittedData(FormInterface $form)
  127.     {
  128.         $hash spl_object_hash($form);
  129.         if (!isset($this->dataByForm[$hash])) {
  130.             // field was created by form event
  131.             $this->collectConfiguration($form);
  132.             $this->collectDefaultData($form);
  133.         }
  134.         $this->dataByForm[$hash] = array_replace(
  135.             $this->dataByForm[$hash],
  136.             $this->dataExtractor->extractSubmittedData($form)
  137.         );
  138.         // Count errors
  139.         if (isset($this->dataByForm[$hash]['errors'])) {
  140.             $this->data['nb_errors'] += \count($this->dataByForm[$hash]['errors']);
  141.         }
  142.         foreach ($form as $child) {
  143.             $this->collectSubmittedData($child);
  144.             // Expand current form if there are children with errors
  145.             if (empty($this->dataByForm[$hash]['has_children_error'])) {
  146.                 $childData $this->dataByForm[spl_object_hash($child)];
  147.                 $this->dataByForm[$hash]['has_children_error'] = !empty($childData['has_children_error']) || !empty($childData['errors']);
  148.             }
  149.         }
  150.     }
  151.     /**
  152.      * {@inheritdoc}
  153.      */
  154.     public function collectViewVariables(FormView $view)
  155.     {
  156.         $hash spl_object_hash($view);
  157.         if (!isset($this->dataByView[$hash])) {
  158.             $this->dataByView[$hash] = [];
  159.         }
  160.         $this->dataByView[$hash] = array_replace(
  161.             $this->dataByView[$hash],
  162.             $this->dataExtractor->extractViewVariables($view)
  163.         );
  164.         foreach ($view->children as $child) {
  165.             $this->collectViewVariables($child);
  166.         }
  167.     }
  168.     /**
  169.      * {@inheritdoc}
  170.      */
  171.     public function buildPreliminaryFormTree(FormInterface $form)
  172.     {
  173.         $this->data['forms'][$form->getName()] = &$this->recursiveBuildPreliminaryFormTree($form$this->data['forms_by_hash']);
  174.     }
  175.     /**
  176.      * {@inheritdoc}
  177.      */
  178.     public function buildFinalFormTree(FormInterface $formFormView $view)
  179.     {
  180.         $this->data['forms'][$form->getName()] = &$this->recursiveBuildFinalFormTree($form$view$this->data['forms_by_hash']);
  181.     }
  182.     /**
  183.      * {@inheritdoc}
  184.      */
  185.     public function getName()
  186.     {
  187.         return 'form';
  188.     }
  189.     /**
  190.      * {@inheritdoc}
  191.      */
  192.     public function getData()
  193.     {
  194.         return $this->data;
  195.     }
  196.     public function serialize()
  197.     {
  198.         foreach ($this->data['forms_by_hash'] as &$form) {
  199.             if (isset($form['type_class']) && !$form['type_class'] instanceof ClassStub) {
  200.                 $form['type_class'] = new ClassStub($form['type_class']);
  201.             }
  202.         }
  203.         return serialize($this->cloneVar($this->data));
  204.     }
  205.     /**
  206.      * {@inheritdoc}
  207.      */
  208.     protected function getCasters()
  209.     {
  210.         return parent::getCasters() + [
  211.             \Exception::class => function (\Exception $e, array $aStub $s) {
  212.                 foreach (["\0Exception\0previous""\0Exception\0trace"] as $k) {
  213.                     if (isset($a[$k])) {
  214.                         unset($a[$k]);
  215.                         ++$s->cut;
  216.                     }
  217.                 }
  218.                 return $a;
  219.             },
  220.             FormInterface::class => function (FormInterface $f, array $a) {
  221.                 return [
  222.                     Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
  223.                     Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(\get_class($f->getConfig()->getType()->getInnerType())),
  224.                 ];
  225.             },
  226.             FormView::class => [StubCaster::class, 'cutInternals'],
  227.             ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
  228.                 return [
  229.                     Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
  230.                     Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
  231.                     Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
  232.                 ];
  233.             },
  234.         ];
  235.     }
  236.     private function &recursiveBuildPreliminaryFormTree(FormInterface $form, array &$outputByHash)
  237.     {
  238.         $hash spl_object_hash($form);
  239.         $output = &$outputByHash[$hash];
  240.         $output = isset($this->dataByForm[$hash])
  241.             ? $this->dataByForm[$hash]
  242.             : [];
  243.         $output['children'] = [];
  244.         foreach ($form as $name => $child) {
  245.             $output['children'][$name] = &$this->recursiveBuildPreliminaryFormTree($child$outputByHash);
  246.         }
  247.         return $output;
  248.     }
  249.     private function &recursiveBuildFinalFormTree(FormInterface $form nullFormView $view, array &$outputByHash)
  250.     {
  251.         $viewHash spl_object_hash($view);
  252.         $formHash null;
  253.         if (null !== $form) {
  254.             $formHash spl_object_hash($form);
  255.         } elseif (isset($this->formsByView[$viewHash])) {
  256.             // The FormInterface instance of the CSRF token is never contained in
  257.             // the FormInterface tree of the form, so we need to get the
  258.             // corresponding FormInterface instance for its view in a different way
  259.             $formHash $this->formsByView[$viewHash];
  260.         }
  261.         if (null !== $formHash) {
  262.             $output = &$outputByHash[$formHash];
  263.         }
  264.         $output = isset($this->dataByView[$viewHash])
  265.             ? $this->dataByView[$viewHash]
  266.             : [];
  267.         if (null !== $formHash) {
  268.             $output array_replace(
  269.                 $output,
  270.                 isset($this->dataByForm[$formHash])
  271.                     ? $this->dataByForm[$formHash]
  272.                     : []
  273.             );
  274.         }
  275.         $output['children'] = [];
  276.         foreach ($view->children as $name => $childView) {
  277.             // The CSRF token, for example, is never added to the form tree.
  278.             // It is only present in the view.
  279.             $childForm null !== $form && $form->has($name)
  280.                 ? $form->get($name)
  281.                 : null;
  282.             $output['children'][$name] = &$this->recursiveBuildFinalFormTree($childForm$childView$outputByHash);
  283.         }
  284.         return $output;
  285.     }
  286. }