-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormRenderer.php
105 lines (85 loc) · 2.25 KB
/
FormRenderer.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
declare(strict_types=1);
namespace Matt9mg\Concrete5\Symfony\Form;
use Concrete\Core\Localization\Localization;
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
use Symfony\Component\Templating\Helper\HelperInterface;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Form\FormRenderer as SymfonyFormRenderer;
class FormRenderer
{
/**
* @var PhpEngine
*/
private $engine;
/**
* @var FormHelper
*/
private $formHelper;
/**
* @var SymfonyFormRenderer
*/
private $renderer;
/**
* @var HelperInterface[]
*/
private $helpers = [];
private $templatePaths = [
__DIR__ . '/src/Resources/views/Form'
];
public function __construct()
{
$loader = new FilesystemLoader(array());
$this->engine = new PhpEngine(new TemplateNameParser(), $loader);
}
/**
* Add a template path where you form input template resides
*
* @param string $path
*
* @return FormRenderer
*/
public function addTemplatePath(string $path): FormRenderer
{
$this->templatePaths[] = $path;
return $this;
}
/**
* Add helpers used within the PHP engine
*
* @param HelperInterface $helper
* @return FormRenderer
*/
public function addHelpers(HelperInterface $helper): FormRenderer
{
$this->helpers[] = $helper;
return $this;
}
/**
* @return $this
*/
final public function build(): FormRenderer
{
$rendererEngine = new TemplatingRendererEngine($this->engine, $this->templatePaths);
$this->renderer = new SymfonyFormRenderer($rendererEngine);
$this->formHelper = new FormHelper($this->renderer);
$translatorHelper = new TranslatorHelper();
$helpers = array_merge($this->helpers, [$this->formHelper, $translatorHelper]);
$this->engine->addHelpers($helpers);
return $this;
}
/**
* @return FormHelper
*/
public function getFormHelper(): FormHelper
{
return $this->formHelper;
}
/**
* @return SymfonyFormRenderer
*/
public function getFormRenderer(): SymfonyFormRenderer
{
return $this->renderer;
}
}