]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Extension / HttpFoundation / EventListener / BindRequestListener.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Extension\HttpFoundation\EventListener;
13
14 use Symfony\Component\Form\FormEvents;
15 use Symfony\Component\Form\FormEvent;
16 use Symfony\Component\Form\Exception\LogicException;
17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18 use Symfony\Component\HttpFoundation\Request;
19
20 /**
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 *
23 * @deprecated Deprecated since version 2.3, to be removed in 3.0. Pass the
24 * Request instance to {@link Form::process()} instead.
25 */
26 class BindRequestListener implements EventSubscriberInterface
27 {
28 public static function getSubscribedEvents()
29 {
30 // High priority in order to supersede other listeners
31 return array(FormEvents::PRE_BIND => array('preBind', 128));
32 }
33
34 public function preBind(FormEvent $event)
35 {
36 $form = $event->getForm();
37
38 /* @var Request $request */
39 $request = $event->getData();
40
41 // Only proceed if we actually deal with a Request
42 if (!$request instanceof Request) {
43 return;
44 }
45
46 // Uncomment this as soon as the deprecation note should be shown
47 // trigger_error('Passing a Request instance to Form::submit() is deprecated since version 2.3 and will be disabled in 3.0. Call Form::process($request) instead.', E_USER_DEPRECATED);
48
49 $name = $form->getConfig()->getName();
50 $default = $form->getConfig()->getCompound() ? array() : null;
51
52 // Store the bound data in case of a post request
53 switch ($request->getMethod()) {
54 case 'POST':
55 case 'PUT':
56 case 'DELETE':
57 case 'PATCH':
58 if ('' === $name) {
59 // Form bound without name
60 $params = $request->request->all();
61 $files = $request->files->all();
62 } else {
63 $params = $request->request->get($name, $default);
64 $files = $request->files->get($name, $default);
65 }
66
67 if (is_array($params) && is_array($files)) {
68 $data = array_replace_recursive($params, $files);
69 } else {
70 $data = $params ?: $files;
71 }
72
73 break;
74
75 case 'GET':
76 $data = '' === $name
77 ? $request->query->all()
78 : $request->query->get($name, $default);
79
80 break;
81
82 default:
83 throw new LogicException(sprintf(
84 'The request method "%s" is not supported',
85 $request->getMethod()
86 ));
87 }
88
89 $event->setData($data);
90 }
91 }