vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php line 110

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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\HttpFoundation;
  11. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  12. use Symfony\Component\Form\FormError;
  13. use Symfony\Component\Form\FormInterface;
  14. use Symfony\Component\Form\RequestHandlerInterface;
  15. use Symfony\Component\Form\Util\FormUtil;
  16. use Symfony\Component\Form\Util\ServerParams;
  17. use Symfony\Component\HttpFoundation\File\File;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\HttpFoundation\Request;
  20. /**
  21. * A request processor using the {@link Request} class of the HttpFoundation
  22. * component.
  23. *
  24. * @author Bernhard Schussek <[email protected]>
  25. */
  26. class HttpFoundationRequestHandler implements RequestHandlerInterface
  27. {
  28. private $serverParams;
  29. public function __construct(?ServerParams $serverParams = null)
  30. {
  31. $this->serverParams = $serverParams ?? new ServerParams();
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function handleRequest(FormInterface $form, $request = null)
  37. {
  38. if (!$request instanceof Request) {
  39. throw new UnexpectedTypeException($request, 'Symfony\Component\HttpFoundation\Request');
  40. }
  41. $name = $form->getName();
  42. $method = $form->getConfig()->getMethod();
  43. if ($method !== $request->getMethod()) {
  44. return;
  45. }
  46. // For request methods that must not have a request body we fetch data
  47. // from the query string. Otherwise we look for data in the request body.
  48. if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
  49. if ('' === $name) {
  50. $data = $request->query->all();
  51. } else {
  52. // Don't submit GET requests if the form's name does not exist
  53. // in the request
  54. if (!$request->query->has($name)) {
  55. return;
  56. }
  57. $data = $request->query->all()[$name];
  58. }
  59. } else {
  60. // Mark the form with an error if the uploaded size was too large
  61. // This is done here and not in FormValidator because $_POST is
  62. // empty when that error occurs. Hence the form is never submitted.
  63. if ($this->serverParams->hasPostMaxSizeBeenExceeded()) {
  64. // Submit the form, but don't clear the default values
  65. $form->submit(null, false);
  66. $form->addError(new FormError(
  67. $form->getConfig()->getOption('upload_max_size_message')(),
  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. $default = $form->getConfig()->getCompound() ? [] : null;
  78. $params = $request->request->all()[$name] ?? $default;
  79. $files = $request->files->get($name, $default);
  80. } else {
  81. // Don't submit the form if it is not present in the request
  82. return;
  83. }
  84. if (\is_array($params) && \is_array($files)) {
  85. $data = FormUtil::mergeParamsAndFiles($params, $files);
  86. } else {
  87. $data = $params ?: $files;
  88. }
  89. }
  90. // Don't auto-submit the form unless at least one field is present.
  91. if ('' === $name && \count(array_intersect_key($data, $form->all())) <= 0) {
  92. return;
  93. }
  94. $form->submit($data, 'PATCH' !== $method);
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function isFileUpload($data)
  100. {
  101. return $data instanceof File;
  102. }
  103. /**
  104. * @return int|null
  105. */
  106. public function getUploadFileError($data)
  107. {
  108. if (!$data instanceof UploadedFile || $data->isValid()) {
  109. return null;
  110. }
  111. return $data->getError();
  112. }
  113. }