4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form
;
14 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
15 use Symfony\Component\Form\FormInterface
;
16 use Symfony\Component\Form\RequestHandlerInterface
;
19 * A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
21 * @author Bernhard Schussek <bschussek@gmail.com>
23 class NativeRequestHandler
implements RequestHandlerInterface
26 * The allowed keys of the $_FILES array.
30 private static $fileKeys = array(
41 public function handleRequest(FormInterface
$form, $request = null)
43 if (null !== $request) {
44 throw new UnexpectedTypeException($request, 'null');
47 $name = $form->getName();
48 $method = $form->getConfig()->getMethod();
50 if ($method !== self
::getRequestMethod()) {
54 if ('GET' === $method) {
58 // Don't submit GET requests if the form's name does not exist
60 if (!isset($_GET[$name])) {
67 $fixedFiles = array();
68 foreach ($_FILES as $name => $file) {
69 $fixedFiles[$name] = self
::stripEmptyFiles(self
::fixPhpFilesArray($file));
76 $default = $form->getConfig()->getCompound() ? array() : null;
77 $params = isset($_POST[$name]) ? $_POST[$name] : $default;
78 $files = isset($fixedFiles[$name]) ? $fixedFiles[$name] : $default;
81 if (is_array($params) && is_array($files)) {
82 $data = array_replace_recursive($params, $files);
84 $data = $params ?: $files;
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) {
93 $form->submit($data, 'PATCH' !== $method);
97 * Returns the method used to submit the request to the server.
99 * @return string The request method.
101 private static function getRequestMethod()
103 $method = isset($_SERVER['REQUEST_METHOD'])
104 ? strtoupper($_SERVER['REQUEST_METHOD'])
107 if ('POST' === $method && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
108 $method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
115 * Fixes a malformed PHP $_FILES array.
117 * PHP has a bug that the format of the $_FILES array differs, depending on
118 * whether the uploaded file fields had normal field names or array-like
119 * field names ("normal" vs. "parent[child]").
121 * This method fixes the array to look like the "normal" $_FILES array.
123 * It's safe to pass an already converted array, in which case this method
124 * just returns the original array unmodified.
126 * This method is identical to {@link Symfony\Component\HttpFoundation\FileBag::fixPhpFilesArray}
127 * and should be kept as such in order to port fixes quickly and easily.
133 private static function fixPhpFilesArray($data)
135 if (!is_array($data)) {
139 $keys = array_keys($data);
142 if (self
::$fileKeys !== $keys || !isset($data['name']) || !is_array($data['name'])) {
147 foreach (self
::$fileKeys as $k) {
151 foreach (array_keys($data['name']) as $key) {
152 $files[$key] = self
::fixPhpFilesArray(array(
153 'error' => $data['error'][$key],
154 'name' => $data['name'][$key],
155 'type' => $data['type'][$key],
156 'tmp_name' => $data['tmp_name'][$key],
157 'size' => $data['size'][$key]
165 * Sets empty uploaded files to NULL in the given uploaded files array.
167 * @param mixed $data The file upload data.
169 * @return array|null Returns the stripped upload data.
171 private static function stripEmptyFiles($data)
173 if (!is_array($data)) {
177 $keys = array_keys($data);
180 if (self
::$fileKeys === $keys) {
181 if (UPLOAD_ERR_NO_FILE
=== $data['error']) {
188 foreach ($data as $key => $value) {
189 $data[$key] = self
::stripEmptyFiles($value);