-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwigTemplateRenderer.php
153 lines (142 loc) · 5.13 KB
/
TwigTemplateRenderer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace App\Service\Templating;
use App\Utils\Traits\TwigHelperTrait;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Yaml\Yaml;
use Twig\Environment;
/**
* Class TwigTemplateRenderer.
*
* Render a Twig template or block.
*/
final class TwigTemplateRenderer implements TemplateRendererInterface, TemplateBlockRendererInterface
{
use TwigHelperTrait;
/**
* @var array
*/
private $templates;
/**
* @var Environment
*/
private $templateRenderer;
/**
* TwigTemplateRenderer constructor.
*
* @param ParameterBagInterface $parameterBag
* @param Environment $twig
*
*/
public function __construct(ParameterBagInterface $parameterBag, Environment $twig)
{
$this->templateRenderer = $twig;
// Get template list data in particular .yaml file
$yamlFilePath = $parameterBag->get('app_template_list_yaml_dir');
$this->templates = Yaml::parseFile( $yamlFilePath . 'template_list.yaml');
}
/**
* Render a Twig engine template.
*
* @param string $template
* @param array $data
* @param bool $isEmail
*
* @return string
*
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function renderTemplate(string $template, array $data, bool $isEmail = false): string
{
return $this->templateRenderer->render($template, $data);
}
/**
* Retrieve the Twig template name to show.
*
* @param string $actionClassName a fully qualified name based on action class
* @param bool $isEmail an indicator to distinct email templates
*
* @return string
*
* @see https://stackoverflow.com/questions/22407370/how-to-check-if-class-exists-within-a-namespace
* @see https://stackoverflow.com/questions/15839292/is-there-a-namespace-aware-alternative-to-phps-class-exists?noredirect=1&lq=1
*/
public function getTemplate(string $actionClassName, bool $isEmail = false): string
{
if (!class_exists($actionClassName)) {
throw new \InvalidArgumentException('Template cannot be rendered: mandatory class does not exist!');
}
$data = '';
$i = 0;
// Check template type to find (email or page/block template)
$templates = !$isEmail ? $this->templates['page_templates'] : $this->templates['email_templates'];
foreach ($templates as $template) {
++$i;
$hasBlock = !isset($template['block']) ? false : true;
$isMatched = $actionClassName !== $template['class'] ? false : true;
if ($i === \count($templates) && false === $isMatched) {
throw new \RuntimeException('No template name was found: try to use another rendering method!');
}
if (true === $hasBlock || false === $isMatched) {
continue;
}
$data = $template['name'];
break;
}
return $data;
}
/**
* Render a Twig template block.
*
* @param string $template a template name
* @param string $block a template block name
* @param array $data some data to pass to template
*
* @return string
*
* @throws \Throwable
*/
public function renderTemplateBlock(string $template, string $block, array $data): string
{
return $this->renderBlock($this->templateRenderer, $template, $block, $data);
}
/**
* Retrieve the Twig template block name based on fully qualified class name
* where template block rendering is called (e.g. Controller, Responder, or "Presenter" class name...)
*
* For instance, use an array to enable matching
* between Responder (or other class) fully qualified class name (key) and template block name (value).
*
* @param string $actionClassName a fully qualified class name base on action class
*
* @return array an associative array which contains template name and template block name
*/
public function getTemplateBlock(string $actionClassName): array
{
if (!class_exists($actionClassName)) {
throw new \InvalidArgumentException('Template block cannot be rendered: mandatory class does not exist!');
}
$data = [];
$i = 0;
$templates = $this->templates['page_templates'];
foreach ($templates as $template) {
++$i;
$hasBlock = !isset($template['block']) ? false : true;
$isMatched = $actionClassName !== $template['class'] ? false : true;
if ($i === \count($templates) && false === $isMatched) {
throw new \RuntimeException('No template block name was found: try to use another rendering method!');
}
if (false === $hasBlock || false === $isMatched) {
continue;
}
$data = [
'template' => $template['name'],
'block' => $template['block']
];
break;
}
return $data;
}
}