HttpFoundationRequestHandler.php 3.9 KB

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