vendor/sylius/resource-bundle/src/Bundle/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php line 115

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\Form\Extension\HttpFoundation;
  12. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  13. use Symfony\Component\Form\FormError;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\RequestHandlerInterface;
  16. use Symfony\Component\Form\Util\ServerParams;
  17. use Symfony\Component\HttpFoundation\File\File;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Webmozart\Assert\Assert;
  20. /**
  21.  * Does not compare the form's method with the request's method.
  22.  * Always submits the form, even if there are no fields sent.
  23.  *
  24.  * @internal
  25.  *
  26.  * @see \Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler
  27.  */
  28. final class HttpFoundationRequestHandler implements RequestHandlerInterface
  29. {
  30.     private ServerParams $serverParams;
  31.     public function __construct(ServerParams $serverParams null)
  32.     {
  33.         $this->serverParams $serverParams ?: new ServerParams();
  34.     }
  35.     public function handleRequest(FormInterface $formmixed $request null): void
  36.     {
  37.         if (!$request instanceof Request) {
  38.             throw new UnexpectedTypeException($request'Symfony\Component\HttpFoundation\Request');
  39.         }
  40.         $name $form->getName();
  41.         $method $request->getMethod();
  42.         // For request methods that must not have a request body we fetch data
  43.         // from the query string. Otherwise we look for data in the request body.
  44.         if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
  45.             if ('' === $name) {
  46.                 $data $request->query->all();
  47.             } else {
  48.                 // Don't submit GET requests if the form's name does not exist
  49.                 // in the request
  50.                 if (!$request->query->has($name)) {
  51.                     return;
  52.                 }
  53.                 $data $request->query->all($name);
  54.             }
  55.         } else {
  56.             // Mark the form with an error if the uploaded size was too large
  57.             // This is done here and not in FormValidator because $_POST is
  58.             // empty when that error occurs. Hence the form is never submitted.
  59.             if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
  60.                 // Submit the form, but don't clear the default values
  61.                 $form->submit(nullfalse);
  62.                 $uploadMaxSizeMessageCallable $form->getConfig()->getOption('upload_max_size_message');
  63.                 Assert::isCallable($uploadMaxSizeMessageCallable);
  64.                 $uploadMaxSizeMessage call_user_func($uploadMaxSizeMessageCallable);
  65.                 Assert::string($uploadMaxSizeMessage);
  66.                 $form->addError(new FormError(
  67.                     $uploadMaxSizeMessage,
  68.                     null,
  69.                     ['{{ max }}' => $this->serverParams->getNormalizedIniPostMaxSize()],
  70.                 ));
  71.                 return;
  72.             }
  73.             if ('' === $name) {
  74.                 $params $request->request->all();
  75.                 $files $request->files->all();
  76.             } elseif ($request->request->has($name) || $request->files->has($name)) {
  77.                 /** @psalm-var array|null $default */
  78.                 $default $form->getConfig()->getCompound() ? [] : null;
  79.                 if ($request->request->has($name)) {
  80.                     $params $request->request->all($name);
  81.                 } else {
  82.                     $params $default;
  83.                 }
  84.                 $files $request->files->get($name$default);
  85.             } else {
  86.                 // Don't submit the form if it is not present in the request
  87.                 return;
  88.             }
  89.             if (is_array($params) && is_array($files)) {
  90.                 $data array_replace_recursive($params$files);
  91.             } else {
  92.                 $data $params ?: $files;
  93.             }
  94.         }
  95.         $form->submit($data'PATCH' !== $method);
  96.     }
  97.     public function isFileUpload(mixed $data): bool
  98.     {
  99.         return $data instanceof File;
  100.     }
  101. }