1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resilient; |
4
|
|
|
|
5
|
|
|
use \Psr\Http\Message\ResponseInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* TwigRenderer class. |
9
|
|
|
* |
10
|
|
|
* Provide implementation for abstract renderer, specifically made with twig engine |
11
|
|
|
* |
12
|
|
|
* @extends AbstractRenderer |
13
|
|
|
*/ |
14
|
|
|
class TwigRenderer extends AbstractRenderer |
15
|
|
|
{ |
16
|
|
|
protected $loader; |
17
|
|
|
protected $environment; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* __construct function. |
21
|
|
|
* |
22
|
|
|
* @access public |
23
|
|
|
* @param mixed $path |
24
|
|
|
* @param array $options (default: []) |
25
|
|
|
*/ |
26
|
|
|
public function __construct($path, array $options = []) |
27
|
|
|
{ |
28
|
|
|
$this->loader = $this->createLoader(is_array($path) ? $path : [$path]); |
29
|
|
|
$this->environment = new \Twig_Environment($this->loader, $options); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* addExtension function. |
34
|
|
|
* |
35
|
|
|
* @access public |
36
|
|
|
* @param \Twig_ExtensionInterface $extension |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function addExtension(\Twig_ExtensionInterface $extension) |
40
|
|
|
{ |
41
|
|
|
$this->environment->addExtension($extension); |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* addFilter function. |
48
|
|
|
* |
49
|
|
|
* @access public |
50
|
|
|
* @param \Twig_Filter $filter |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function addFilter(\Twig_Filter $filter) |
54
|
|
|
{ |
55
|
|
|
$this->environment->addFilter($filter); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
protected function template($template) |
64
|
|
|
{ |
65
|
|
|
return $this->environment->load($template); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Twig renderBlock function. |
70
|
|
|
* |
71
|
|
|
* @access public |
72
|
|
|
* @param mixed $template |
73
|
|
|
* @param mixed $block_name |
74
|
|
|
* @param mixed $data (default: []) |
75
|
|
|
* @return string rendered block |
76
|
|
|
*/ |
77
|
|
|
public function renderBlock($template, $block_name, $data = []) |
78
|
|
|
{ |
79
|
|
|
return $this->template($template)->renderBlock($block_name, $this->merge_data($data)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function render(ResponseInterface $response, $template, $data = []) |
86
|
|
|
{ |
87
|
|
|
$response->getBody()->write($this->template($template)->render($this->merge_data($data))); |
88
|
|
|
|
89
|
|
|
return $response; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* getLoader function. |
94
|
|
|
* |
95
|
|
|
* @access public |
96
|
|
|
* @return Twig_Loader_Filesystem |
97
|
|
|
*/ |
98
|
|
|
public function getLoader() |
99
|
|
|
{ |
100
|
|
|
return $this->loader; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* getEnvironment function. |
105
|
|
|
* |
106
|
|
|
* @access public |
107
|
|
|
* @return Twig_Environment |
108
|
|
|
*/ |
109
|
|
|
public function getEnvironment() |
110
|
|
|
{ |
111
|
|
|
return $this->environment; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* createLoader function. |
116
|
|
|
* |
117
|
|
|
* @access protected |
118
|
|
|
* @param array $paths |
119
|
|
|
* @return Twig_Loader_Filesystem |
120
|
|
|
*/ |
121
|
|
|
protected function createLoader(array $paths) |
122
|
|
|
{ |
123
|
|
|
$loader = new \Twig_Loader_Filesystem(); |
124
|
|
|
|
125
|
|
|
foreach ($paths as $namespace => $path) |
126
|
|
|
{ |
127
|
|
|
if (is_string($namespace)) |
128
|
|
|
{ |
129
|
|
|
$loader->setPaths($path, $namespace); |
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$loader->addPath($path); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $loader; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|