-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormFactory.php
82 lines (68 loc) · 2.44 KB
/
FormFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Matt9mg\Concrete5\Symfony\Form;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\FormExtensionInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
use Symfony\Component\Validator\Validation;
/**
* Class Form
* @package Matt9mg\Concrete5\Symfony\Form
*/
class FormFactory
{
/**
* @var SessionInterface
*/
private $session;
/**
* FormFactory constructor.
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* @var FormExtensionInterface[]
*/
private $formExtensions = [];
/**
* Add form extensions used within the createFormFactoryBuilder
*
* @param FormExtensionInterface $formExtension
* @return FormFactory
*/
public function addFormExtension(FormExtensionInterface $formExtension): FormFactory
{
$this->formExtensions[] = $formExtension;
return $this;
}
public function createFormFactory(): FormFactoryInterface
{
$csrfGenerator = new UriSafeTokenGenerator();
$csrfStorage = new SessionTokenStorage($this->session);
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
// need to update the below to use C5 translations
$translator = new \Symfony\Component\Translation\Translator('en_US');
$translatorHelper = new TranslatorHelper($translator);
$forms = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension())
->addExtension(new CsrfExtension($csrfManager))
->addExtension(new ValidatorExtension($validator));
foreach ($this->formExtensions as $formExtension) {
$forms->addExtension($formExtension);
}
return $forms->getFormFactory();
}
}