@@ -9,286 +9,286 @@ |
||
9 | 9 | |
10 | 10 | final class PHPTemplate implements \ArrayAccess { |
11 | 11 | |
12 | - /** |
|
13 | - * @var View data |
|
14 | - */ |
|
15 | - private $vars; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var string Template file |
|
19 | - */ |
|
20 | - private $template_file; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var array Allowed url protocols |
|
24 | - */ |
|
25 | - private const ALLOWED_URL_PROTOCOLS = [ |
|
26 | - 'http', |
|
27 | - 'https', |
|
28 | - 'ftp' |
|
29 | - ]; |
|
30 | - |
|
31 | - /** |
|
32 | - * Constructor. |
|
33 | - * |
|
34 | - * @param string|null $template_file The template file |
|
35 | - * @param array $vars The view data |
|
36 | - */ |
|
37 | - public function __construct(string $template_file = null, array $vars = []) { |
|
38 | - $this->template_file = $template_file; |
|
39 | - $this->set($vars); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Set view data. |
|
44 | - * |
|
45 | - * @param array $vars The view data |
|
46 | - * @return void |
|
47 | - */ |
|
48 | - public function set(array $vars = []): void { |
|
49 | - $this->vars = $vars; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Render template. |
|
54 | - * |
|
55 | - * @param array|null $vars The view data |
|
56 | - * @return string Rendered template |
|
57 | - * @throws Exception |
|
58 | - */ |
|
59 | - public function render(array $vars = null): string { |
|
60 | - if (empty($this->template_file)) { |
|
61 | - throw new BadMethodCallException('Calls to render() method is illegal when a template path is not set.'); |
|
62 | - } |
|
63 | - if (!file_exists($this->template_file)) { |
|
64 | - throw new TemplateNotFound(sprintf('Template %s could not be loaded.', $this->template_file)); |
|
65 | - } |
|
66 | - |
|
67 | - if (null !== $vars) { |
|
68 | - $this->set($vars); |
|
69 | - } |
|
70 | - |
|
71 | - return trim(self::renderTemplate($this->template_file, $this)); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Render template. |
|
76 | - * |
|
77 | - * @param string $path The path |
|
78 | - * @param PHPTemplate $v The PHPTemplate instance |
|
79 | - * @return string The rendered content |
|
80 | - * @throws Exception |
|
81 | - */ |
|
82 | - private static function renderTemplate($path, PHPTemplate $v): string { |
|
83 | - ob_start(); |
|
84 | - try { |
|
85 | - /** @noinspection PhpIncludeInspection */ |
|
86 | - include $path; |
|
87 | - } catch (Exception $exception) { |
|
88 | - ob_end_clean(); |
|
89 | - throw $exception; |
|
90 | - } |
|
91 | - |
|
92 | - return ob_get_clean(); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get view data by key name. |
|
97 | - * |
|
98 | - * @param string $key The key |
|
99 | - * @return mixed|null The value |
|
100 | - */ |
|
101 | - public function get(string $key) { |
|
102 | - if (isset($this->vars[$key])) { |
|
103 | - return $this->vars[$key]; |
|
104 | - } |
|
105 | - |
|
106 | - return null; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Whether an offset exists. |
|
111 | - * |
|
112 | - * @param mixed $offset An offset to check for |
|
113 | - * @return bool Returns true on success or false on failure. |
|
114 | - */ |
|
115 | - public function offsetExists($offset): bool { |
|
116 | - return isset($this->vars[$offset]); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Offset to retrieve. |
|
121 | - * |
|
122 | - * @param mixed $offset The offset to retrieve. |
|
123 | - * @return mixed Returns the value at specified offset. |
|
124 | - */ |
|
125 | - public function offsetGet($offset) { |
|
126 | - $first_char = $offset{0}; |
|
127 | - |
|
128 | - switch ($first_char) { |
|
129 | - case ':': |
|
130 | - return $this->url($this->getVar(substr($offset, 1))); |
|
131 | - |
|
132 | - case '!': |
|
133 | - return $this->getVar(substr($offset, 1)); |
|
134 | - default: |
|
135 | - return $this->escape($this->getVar($offset)); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Assign a value to the specified offset. |
|
141 | - * |
|
142 | - * @param mixed $offset The offset to assign the value to |
|
143 | - * @param mixed $value The value to set |
|
144 | - * @return void |
|
145 | - */ |
|
146 | - public function offsetSet($offset, $value): void { |
|
147 | - $this->vars[$offset] = $value; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Unset an offset. |
|
152 | - * |
|
153 | - * @param mixed $offset The offset to unset. |
|
154 | - * @return void |
|
155 | - */ |
|
156 | - public function offsetUnset($offset): void { |
|
157 | - unset($this->vars[$offset]); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * View data value to retrieve. |
|
162 | - * |
|
163 | - * @param string $offset The offset to retrieve |
|
164 | - * @return string Returns the value at specified offset |
|
165 | - */ |
|
166 | - private function getVar(string $offset): string { |
|
167 | - if (isset($this->vars[$offset]) && \is_scalar($this->vars[$offset]) && !\is_bool($this->vars[$offset])) { |
|
168 | - return $this->vars[$offset]; |
|
169 | - } |
|
170 | - return ''; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Html encoding. |
|
175 | - * |
|
176 | - * Escapes the provided literal string to make sure it does not contain anything that would be interpreted as HTML. |
|
177 | - * You can also use this escape method inside HTML attributes as it would also convert single and double |
|
178 | - * quotes to HTML entities. |
|
179 | - * |
|
180 | - * @param string $input The value to encode |
|
181 | - * @return string The html encoded value |
|
182 | - */ |
|
183 | - public function escape(string $input): string { |
|
184 | - return \htmlspecialchars($input, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8'); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * URL encoding. |
|
189 | - * |
|
190 | - * Sanitizes the provided URL making sure it only contains relative paths, or URIs whose protocol |
|
191 | - * is http://, https://, or ftp://. Any other protocols such as javascript: will be removed. |
|
192 | - * |
|
193 | - * @param string $uri The url |
|
194 | - * @return string The url encoded string |
|
195 | - */ |
|
196 | - public function url(string $uri): string { |
|
197 | - $allowed_protocols = array_flip(static::ALLOWED_URL_PROTOCOLS); |
|
198 | - |
|
199 | - // Iteratively remove any invalid protocol found. |
|
200 | - // Borrowed from Drupal. |
|
201 | - do { |
|
202 | - $before = $uri; |
|
203 | - $colon_position = strpos($uri, ':'); |
|
204 | - if ($colon_position > 0) { |
|
205 | - |
|
206 | - // We found a colon, possibly a protocol. Verify. |
|
207 | - $protocol = substr($uri, 0, $colon_position); |
|
208 | - |
|
209 | - // If a colon is preceded by a slash, question mark or hash, it cannot |
|
210 | - // possibly be part of the URL scheme. This must be a relative URL, which |
|
211 | - // inherits the (safe) protocol of the base document. |
|
212 | - if (preg_match('![/?#]!', $protocol)) { |
|
213 | - break; |
|
214 | - } |
|
215 | - |
|
216 | - // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3 |
|
217 | - // (URI Comparison) scheme comparison must be case-insensitive. |
|
218 | - if (!isset($allowed_protocols[strtolower($protocol)])) { |
|
219 | - $uri = substr($uri, $colon_position + 1); |
|
220 | - } |
|
221 | - } |
|
222 | - } while ($before !== $uri); |
|
223 | - return $this->escape($uri); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Expands the provided array into HTML attributes. |
|
228 | - * |
|
229 | - * @param array $attributes The attributes |
|
230 | - * |
|
231 | - * @return string |
|
232 | - */ |
|
233 | - public function attributes(array $attributes): string { |
|
234 | - foreach ($attributes as $attribute => &$data) { |
|
235 | - $data = implode(' ', (array)$data); |
|
236 | - $data = $attribute . '="' . $this->escape($data) . '"'; |
|
237 | - } |
|
238 | - |
|
239 | - return $attributes ? ' ' . implode(' ', $attributes) : ''; |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * This is a shortcut to throw an error of type TemplateError that the parent caller can catch. |
|
244 | - * You will probably never need to use this method, but this is an easy way to throw an error if your |
|
245 | - * template cannot proceed and you want to terminate and report it to the parent callers. |
|
246 | - * |
|
247 | - * @param string $error_message The error nessage |
|
248 | - * @param int $error_code The error code |
|
249 | - * |
|
250 | - * @throws TemplateError |
|
251 | - */ |
|
252 | - public function error(string $error_message, int $error_code = 0): void { |
|
253 | - throw new TemplateError($error_message, $error_code); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Cast to string. |
|
258 | - * |
|
259 | - * @return string Blank string |
|
260 | - */ |
|
261 | - public function __toString(): string { |
|
262 | - return ''; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Set. |
|
267 | - * |
|
268 | - * @param mixed $name The name |
|
269 | - * @param mixed $value The value |
|
270 | - * @return void |
|
271 | - */ |
|
272 | - public function __set($name, $value): void { |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Isset. |
|
277 | - * |
|
278 | - * @param mixed $name The name |
|
279 | - * @return bool Status |
|
280 | - */ |
|
281 | - public function __isset($name) { |
|
282 | - return false; |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Get. |
|
287 | - * |
|
288 | - * @param mixed $name The name |
|
289 | - * @return string Blank string |
|
290 | - */ |
|
291 | - public function __get($name) { |
|
292 | - return ''; |
|
293 | - } |
|
12 | + /** |
|
13 | + * @var View data |
|
14 | + */ |
|
15 | + private $vars; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var string Template file |
|
19 | + */ |
|
20 | + private $template_file; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var array Allowed url protocols |
|
24 | + */ |
|
25 | + private const ALLOWED_URL_PROTOCOLS = [ |
|
26 | + 'http', |
|
27 | + 'https', |
|
28 | + 'ftp' |
|
29 | + ]; |
|
30 | + |
|
31 | + /** |
|
32 | + * Constructor. |
|
33 | + * |
|
34 | + * @param string|null $template_file The template file |
|
35 | + * @param array $vars The view data |
|
36 | + */ |
|
37 | + public function __construct(string $template_file = null, array $vars = []) { |
|
38 | + $this->template_file = $template_file; |
|
39 | + $this->set($vars); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Set view data. |
|
44 | + * |
|
45 | + * @param array $vars The view data |
|
46 | + * @return void |
|
47 | + */ |
|
48 | + public function set(array $vars = []): void { |
|
49 | + $this->vars = $vars; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Render template. |
|
54 | + * |
|
55 | + * @param array|null $vars The view data |
|
56 | + * @return string Rendered template |
|
57 | + * @throws Exception |
|
58 | + */ |
|
59 | + public function render(array $vars = null): string { |
|
60 | + if (empty($this->template_file)) { |
|
61 | + throw new BadMethodCallException('Calls to render() method is illegal when a template path is not set.'); |
|
62 | + } |
|
63 | + if (!file_exists($this->template_file)) { |
|
64 | + throw new TemplateNotFound(sprintf('Template %s could not be loaded.', $this->template_file)); |
|
65 | + } |
|
66 | + |
|
67 | + if (null !== $vars) { |
|
68 | + $this->set($vars); |
|
69 | + } |
|
70 | + |
|
71 | + return trim(self::renderTemplate($this->template_file, $this)); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Render template. |
|
76 | + * |
|
77 | + * @param string $path The path |
|
78 | + * @param PHPTemplate $v The PHPTemplate instance |
|
79 | + * @return string The rendered content |
|
80 | + * @throws Exception |
|
81 | + */ |
|
82 | + private static function renderTemplate($path, PHPTemplate $v): string { |
|
83 | + ob_start(); |
|
84 | + try { |
|
85 | + /** @noinspection PhpIncludeInspection */ |
|
86 | + include $path; |
|
87 | + } catch (Exception $exception) { |
|
88 | + ob_end_clean(); |
|
89 | + throw $exception; |
|
90 | + } |
|
91 | + |
|
92 | + return ob_get_clean(); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get view data by key name. |
|
97 | + * |
|
98 | + * @param string $key The key |
|
99 | + * @return mixed|null The value |
|
100 | + */ |
|
101 | + public function get(string $key) { |
|
102 | + if (isset($this->vars[$key])) { |
|
103 | + return $this->vars[$key]; |
|
104 | + } |
|
105 | + |
|
106 | + return null; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Whether an offset exists. |
|
111 | + * |
|
112 | + * @param mixed $offset An offset to check for |
|
113 | + * @return bool Returns true on success or false on failure. |
|
114 | + */ |
|
115 | + public function offsetExists($offset): bool { |
|
116 | + return isset($this->vars[$offset]); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Offset to retrieve. |
|
121 | + * |
|
122 | + * @param mixed $offset The offset to retrieve. |
|
123 | + * @return mixed Returns the value at specified offset. |
|
124 | + */ |
|
125 | + public function offsetGet($offset) { |
|
126 | + $first_char = $offset{0}; |
|
127 | + |
|
128 | + switch ($first_char) { |
|
129 | + case ':': |
|
130 | + return $this->url($this->getVar(substr($offset, 1))); |
|
131 | + |
|
132 | + case '!': |
|
133 | + return $this->getVar(substr($offset, 1)); |
|
134 | + default: |
|
135 | + return $this->escape($this->getVar($offset)); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Assign a value to the specified offset. |
|
141 | + * |
|
142 | + * @param mixed $offset The offset to assign the value to |
|
143 | + * @param mixed $value The value to set |
|
144 | + * @return void |
|
145 | + */ |
|
146 | + public function offsetSet($offset, $value): void { |
|
147 | + $this->vars[$offset] = $value; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Unset an offset. |
|
152 | + * |
|
153 | + * @param mixed $offset The offset to unset. |
|
154 | + * @return void |
|
155 | + */ |
|
156 | + public function offsetUnset($offset): void { |
|
157 | + unset($this->vars[$offset]); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * View data value to retrieve. |
|
162 | + * |
|
163 | + * @param string $offset The offset to retrieve |
|
164 | + * @return string Returns the value at specified offset |
|
165 | + */ |
|
166 | + private function getVar(string $offset): string { |
|
167 | + if (isset($this->vars[$offset]) && \is_scalar($this->vars[$offset]) && !\is_bool($this->vars[$offset])) { |
|
168 | + return $this->vars[$offset]; |
|
169 | + } |
|
170 | + return ''; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Html encoding. |
|
175 | + * |
|
176 | + * Escapes the provided literal string to make sure it does not contain anything that would be interpreted as HTML. |
|
177 | + * You can also use this escape method inside HTML attributes as it would also convert single and double |
|
178 | + * quotes to HTML entities. |
|
179 | + * |
|
180 | + * @param string $input The value to encode |
|
181 | + * @return string The html encoded value |
|
182 | + */ |
|
183 | + public function escape(string $input): string { |
|
184 | + return \htmlspecialchars($input, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8'); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * URL encoding. |
|
189 | + * |
|
190 | + * Sanitizes the provided URL making sure it only contains relative paths, or URIs whose protocol |
|
191 | + * is http://, https://, or ftp://. Any other protocols such as javascript: will be removed. |
|
192 | + * |
|
193 | + * @param string $uri The url |
|
194 | + * @return string The url encoded string |
|
195 | + */ |
|
196 | + public function url(string $uri): string { |
|
197 | + $allowed_protocols = array_flip(static::ALLOWED_URL_PROTOCOLS); |
|
198 | + |
|
199 | + // Iteratively remove any invalid protocol found. |
|
200 | + // Borrowed from Drupal. |
|
201 | + do { |
|
202 | + $before = $uri; |
|
203 | + $colon_position = strpos($uri, ':'); |
|
204 | + if ($colon_position > 0) { |
|
205 | + |
|
206 | + // We found a colon, possibly a protocol. Verify. |
|
207 | + $protocol = substr($uri, 0, $colon_position); |
|
208 | + |
|
209 | + // If a colon is preceded by a slash, question mark or hash, it cannot |
|
210 | + // possibly be part of the URL scheme. This must be a relative URL, which |
|
211 | + // inherits the (safe) protocol of the base document. |
|
212 | + if (preg_match('![/?#]!', $protocol)) { |
|
213 | + break; |
|
214 | + } |
|
215 | + |
|
216 | + // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3 |
|
217 | + // (URI Comparison) scheme comparison must be case-insensitive. |
|
218 | + if (!isset($allowed_protocols[strtolower($protocol)])) { |
|
219 | + $uri = substr($uri, $colon_position + 1); |
|
220 | + } |
|
221 | + } |
|
222 | + } while ($before !== $uri); |
|
223 | + return $this->escape($uri); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Expands the provided array into HTML attributes. |
|
228 | + * |
|
229 | + * @param array $attributes The attributes |
|
230 | + * |
|
231 | + * @return string |
|
232 | + */ |
|
233 | + public function attributes(array $attributes): string { |
|
234 | + foreach ($attributes as $attribute => &$data) { |
|
235 | + $data = implode(' ', (array)$data); |
|
236 | + $data = $attribute . '="' . $this->escape($data) . '"'; |
|
237 | + } |
|
238 | + |
|
239 | + return $attributes ? ' ' . implode(' ', $attributes) : ''; |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * This is a shortcut to throw an error of type TemplateError that the parent caller can catch. |
|
244 | + * You will probably never need to use this method, but this is an easy way to throw an error if your |
|
245 | + * template cannot proceed and you want to terminate and report it to the parent callers. |
|
246 | + * |
|
247 | + * @param string $error_message The error nessage |
|
248 | + * @param int $error_code The error code |
|
249 | + * |
|
250 | + * @throws TemplateError |
|
251 | + */ |
|
252 | + public function error(string $error_message, int $error_code = 0): void { |
|
253 | + throw new TemplateError($error_message, $error_code); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Cast to string. |
|
258 | + * |
|
259 | + * @return string Blank string |
|
260 | + */ |
|
261 | + public function __toString(): string { |
|
262 | + return ''; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Set. |
|
267 | + * |
|
268 | + * @param mixed $name The name |
|
269 | + * @param mixed $value The value |
|
270 | + * @return void |
|
271 | + */ |
|
272 | + public function __set($name, $value): void { |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Isset. |
|
277 | + * |
|
278 | + * @param mixed $name The name |
|
279 | + * @return bool Status |
|
280 | + */ |
|
281 | + public function __isset($name) { |
|
282 | + return false; |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Get. |
|
287 | + * |
|
288 | + * @param mixed $name The name |
|
289 | + * @return string Blank string |
|
290 | + */ |
|
291 | + public function __get($name) { |
|
292 | + return ''; |
|
293 | + } |
|
294 | 294 | } |
@@ -232,7 +232,7 @@ |
||
232 | 232 | */ |
233 | 233 | public function attributes(array $attributes): string { |
234 | 234 | foreach ($attributes as $attribute => &$data) { |
235 | - $data = implode(' ', (array)$data); |
|
235 | + $data = implode(' ', (array) $data); |
|
236 | 236 | $data = $attribute . '="' . $this->escape($data) . '"'; |
237 | 237 | } |
238 | 238 |