HttpFoundationRequestHandler.php 4.2 KB

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