|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the jade/jade package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Slince <[email protected]> |
|
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 Jade\Twig; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
|
|
16
|
|
|
class Twig implements \ArrayAccess |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Twig loader |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Twig\Loader\LoaderInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $loader; |
|
24
|
|
|
/** |
|
25
|
|
|
* Twig environment |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Twig\Environment |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $environment; |
|
30
|
|
|
/** |
|
31
|
|
|
* Default view variables |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $defaultVariables = []; |
|
36
|
|
|
/******************************************************************************** |
|
37
|
|
|
* Constructors and service provider registration |
|
38
|
|
|
*******************************************************************************/ |
|
39
|
|
|
/** |
|
40
|
|
|
* Create new Twig view |
|
41
|
|
|
* |
|
42
|
|
|
* @param string|array $path Path(s) to templates directory |
|
43
|
|
|
* @param array $settings Twig environment settings |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct($path, $settings = []) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->loader = $this->createLoader(is_string($path) ? [$path] : $path); |
|
|
|
|
|
|
48
|
|
|
$this->environment = new \Twig\Environment($this->loader, $settings); |
|
49
|
|
|
} |
|
50
|
|
|
/******************************************************************************** |
|
51
|
|
|
* Methods |
|
52
|
|
|
*******************************************************************************/ |
|
53
|
|
|
/** |
|
54
|
|
|
* Proxy method to add an extension to the Twig environment |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Twig\Extension\ExtensionInterface $extension A single extension instance or an array of instances |
|
57
|
|
|
*/ |
|
58
|
|
|
public function addExtension(\Twig\Extension\ExtensionInterface $extension) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->environment->addExtension($extension); |
|
61
|
|
|
} |
|
62
|
|
|
/** |
|
63
|
|
|
* Fetch rendered template |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $template Template pathname relative to templates directory |
|
66
|
|
|
* @param array $data Associative array of template variables |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Twig\Error\LoaderError When the template cannot be found |
|
69
|
|
|
* @throws \Twig\Error\SyntaxError When an error occurred during compilation |
|
70
|
|
|
* @throws \Twig\Error\RuntimeError When an error occurred during rendering |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function fetch($template, $data = []) |
|
75
|
|
|
{ |
|
76
|
|
|
$data = array_merge($this->defaultVariables, $data); |
|
77
|
|
|
return $this->environment->render($template, $data); |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* Fetch rendered block |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $template Template pathname relative to templates directory |
|
83
|
|
|
* @param string $block Name of the block within the template |
|
84
|
|
|
* @param array $data Associative array of template variables |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function fetchBlock($template, $block, $data = []) |
|
89
|
|
|
{ |
|
90
|
|
|
$data = array_merge($this->defaultVariables, $data); |
|
91
|
|
|
return $this->environment->loadTemplate($template)->renderBlock($block, $data); |
|
92
|
|
|
} |
|
93
|
|
|
/** |
|
94
|
|
|
* Fetch rendered string |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $string String |
|
97
|
|
|
* @param array $data Associative array of template variables |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function fetchFromString($string ="", $data = []) |
|
102
|
|
|
{ |
|
103
|
|
|
$data = array_merge($this->defaultVariables, $data); |
|
104
|
|
|
return $this->environment->createTemplate($string)->render($data); |
|
105
|
|
|
} |
|
106
|
|
|
/** |
|
107
|
|
|
* Output rendered template |
|
108
|
|
|
* |
|
109
|
|
|
* @param ResponseInterface $response |
|
110
|
|
|
* @param string $template Template pathname relative to templates directory |
|
111
|
|
|
* @param array $data Associative array of template variables |
|
112
|
|
|
* @return ResponseInterface |
|
113
|
|
|
*/ |
|
114
|
|
|
public function render(ResponseInterface $response, $template, $data = []) |
|
115
|
|
|
{ |
|
116
|
|
|
$response->getBody()->write($this->fetch($template, $data)); |
|
117
|
|
|
return $response; |
|
118
|
|
|
} |
|
119
|
|
|
/** |
|
120
|
|
|
* Create a loader with the given path |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $paths |
|
123
|
|
|
* @return \Twig\Loader\FilesystemLoader |
|
124
|
|
|
*/ |
|
125
|
|
|
private function createLoader(array $paths) |
|
126
|
|
|
{ |
|
127
|
|
|
$loader = new \Twig\Loader\FilesystemLoader(); |
|
128
|
|
|
foreach ($paths as $namespace => $path) { |
|
129
|
|
|
if (is_string($namespace)) { |
|
130
|
|
|
$loader->setPaths($path, $namespace); |
|
131
|
|
|
} else { |
|
132
|
|
|
$loader->addPath($path); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
return $loader; |
|
136
|
|
|
} |
|
137
|
|
|
/******************************************************************************** |
|
138
|
|
|
* Accessors |
|
139
|
|
|
*******************************************************************************/ |
|
140
|
|
|
/** |
|
141
|
|
|
* Return Twig loader |
|
142
|
|
|
* |
|
143
|
|
|
* @return \Twig\Loader\LoaderInterface |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getLoader() |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->loader; |
|
148
|
|
|
} |
|
149
|
|
|
/** |
|
150
|
|
|
* Return Twig environment |
|
151
|
|
|
* |
|
152
|
|
|
* @return \Twig\Environment |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getEnvironment() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->environment; |
|
157
|
|
|
} |
|
158
|
|
|
/******************************************************************************** |
|
159
|
|
|
* ArrayAccess interface |
|
160
|
|
|
*******************************************************************************/ |
|
161
|
|
|
/** |
|
162
|
|
|
* Does this collection have a given key? |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $key The data key |
|
165
|
|
|
* |
|
166
|
|
|
* @return bool |
|
167
|
|
|
*/ |
|
168
|
|
|
public function offsetExists($key) |
|
169
|
|
|
{ |
|
170
|
|
|
return array_key_exists($key, $this->defaultVariables); |
|
171
|
|
|
} |
|
172
|
|
|
/** |
|
173
|
|
|
* Get collection item for key |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $key The data key |
|
176
|
|
|
* |
|
177
|
|
|
* @return mixed The key's value, or the default value |
|
178
|
|
|
*/ |
|
179
|
|
|
public function offsetGet($key) |
|
180
|
|
|
{ |
|
181
|
|
|
return $this->defaultVariables[$key]; |
|
182
|
|
|
} |
|
183
|
|
|
/** |
|
184
|
|
|
* Set collection item |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $key The data key |
|
187
|
|
|
* @param mixed $value The data value |
|
188
|
|
|
*/ |
|
189
|
|
|
public function offsetSet($key, $value) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->defaultVariables[$key] = $value; |
|
192
|
|
|
} |
|
193
|
|
|
/** |
|
194
|
|
|
* Remove item from collection |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $key The data key |
|
197
|
|
|
*/ |
|
198
|
|
|
public function offsetUnset($key) |
|
199
|
|
|
{ |
|
200
|
|
|
unset($this->defaultVariables[$key]); |
|
201
|
|
|
} |
|
202
|
|
|
/******************************************************************************** |
|
203
|
|
|
* Countable interface |
|
204
|
|
|
*******************************************************************************/ |
|
205
|
|
|
/** |
|
206
|
|
|
* Get number of items in collection |
|
207
|
|
|
* |
|
208
|
|
|
* @return int |
|
209
|
|
|
*/ |
|
210
|
|
|
public function count() |
|
211
|
|
|
{ |
|
212
|
|
|
return count($this->defaultVariables); |
|
213
|
|
|
} |
|
214
|
|
|
/******************************************************************************** |
|
215
|
|
|
* IteratorAggregate interface |
|
216
|
|
|
*******************************************************************************/ |
|
217
|
|
|
/** |
|
218
|
|
|
* Get collection iterator |
|
219
|
|
|
* |
|
220
|
|
|
* @return \ArrayIterator |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getIterator() |
|
223
|
|
|
{ |
|
224
|
|
|
return new \ArrayIterator($this->defaultVariables); |
|
225
|
|
|
} |
|
226
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..