1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Ntentan Framework |
5
|
|
|
* Copyright (c) 2010-2015 James Ekow Abaka Ainooson |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
8
|
|
|
* a copy of this software and associated documentation files (the |
9
|
|
|
* "Software"), to deal in the Software without restriction, including |
10
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
11
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
12
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
13
|
|
|
* the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be |
16
|
|
|
* included in all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
20
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
22
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
23
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
24
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace ntentan\honam\engines; |
28
|
|
|
|
29
|
|
|
use ntentan\honam\engines\php\HelpersLoader; |
30
|
|
|
use ntentan\honam\engines\php\Janitor; |
31
|
|
|
use ntentan\honam\TemplateEngine; |
32
|
|
|
use ntentan\honam\engines\php\Variable; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The PHP engine is a template engine built into honam which uses raw PHP as |
36
|
|
|
* the template language. By virtue of this, the PHP engine can boast of high |
37
|
|
|
* performance. Since this engine uses PHP, it has access to all the language's |
38
|
|
|
* features. This could sometimes create a problem since some of these features |
39
|
|
|
* are not intended for templating use. |
40
|
|
|
*/ |
41
|
|
|
class PhpEngine extends AbstractEngine |
42
|
|
|
{ |
43
|
|
|
private $helpersLoader; |
44
|
|
|
private $janitor; |
45
|
|
|
|
46
|
|
|
public function __construct(HelpersLoader $helpersLoader, Janitor $janitor) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
$this->helpersLoader = $helpersLoader; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function partial($template, $templateData = array()) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
return $this->templateRenderer->render($template, $templateData); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function unescape($item) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if($item instanceof ntentan\honam\template_engines\php\Variable) { |
|
|
|
|
59
|
|
|
return $item->unescape(); |
60
|
|
|
} else { |
61
|
|
|
return $item; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Passes the data to be rendered to the template engine instance. |
67
|
|
|
*/ |
68
|
|
|
public function renderFromFileTemplate(string $filePath, array $data) : string |
69
|
|
|
{ |
70
|
|
|
// Escape each variable by passing it through the variable class. |
71
|
|
|
// Users would have to unescape them by calling the escape method directly |
72
|
|
|
// on the variable. |
73
|
|
|
foreach ($data as $_key => $_value) { |
74
|
|
|
$$_key = Variable::initialize($_value); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Expose helpers |
78
|
|
|
$helpers = $this->helpersLoader; |
79
|
|
|
|
80
|
|
|
// Start trapping the output buffer and include the PHP template for |
81
|
|
|
// execution. |
82
|
|
|
ob_start(); |
83
|
|
|
try { |
84
|
|
|
include $filePath; |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
ob_get_flush(); |
87
|
|
|
throw $e; |
88
|
|
|
} |
89
|
|
|
return ob_get_clean(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Passes a template string and data to be rendered to the template engine |
94
|
|
|
* instance. |
95
|
|
|
*/ |
96
|
|
|
public function renderFromStringTemplate(string $string, array $data) : string |
97
|
|
|
{ |
98
|
|
|
\ntentan\utils\StringStream::register(); |
99
|
|
|
file_put_contents('string://template', $string); |
100
|
|
|
return $this->renderFromFileTemplate('string://template', $data); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// public function generate($templateVariables) |
104
|
|
|
// { |
105
|
|
|
// // Escape each variable by passing it through the variable class. |
106
|
|
|
// // Users would have to unescape them by calling the escape method directly |
107
|
|
|
// // on the variable. |
108
|
|
|
// foreach ($templateVariables as $_key => $_value) { |
109
|
|
|
// $$_key = php\Variable::initialize($_value); |
110
|
|
|
// } |
111
|
|
|
// |
112
|
|
|
// // Expose helpers |
113
|
|
|
// $helpers = $this->getHelpersLoader(); |
114
|
|
|
// |
115
|
|
|
// // Start trapping the output buffer and include the PHP template for |
116
|
|
|
// // execution. |
117
|
|
|
// ob_start(); |
118
|
|
|
// try { |
119
|
|
|
// include $this->template; |
120
|
|
|
// } catch (\Exception $e) { |
121
|
|
|
// ob_get_flush(); |
122
|
|
|
// throw $e; |
123
|
|
|
// } |
124
|
|
|
// return ob_get_clean(); |
125
|
|
|
// } |
126
|
|
|
// |
127
|
|
|
// /** |
128
|
|
|
// * A utility function to strip the text of all HTML code. This function |
129
|
|
|
// * removes all HTML tags instead of escaping them. |
130
|
|
|
// * |
131
|
|
|
// * @param string $text |
132
|
|
|
// * @return string |
133
|
|
|
// */ |
134
|
|
|
// public function strip($text) { |
135
|
|
|
// return php\Janitor::cleanHtml($text, true); |
136
|
|
|
// } |
137
|
|
|
// |
138
|
|
|
// /** |
139
|
|
|
// * A utility function to cut long pieces of text into meaningful short |
140
|
|
|
// * chunks. The function is very good in cases where you want to show just |
141
|
|
|
// * a short preview snippet of a long text. The function cuts the long string |
142
|
|
|
// * without cutting through words and appends some sort of ellipsis |
143
|
|
|
// * terminator to the text. |
144
|
|
|
// * |
145
|
|
|
// * @param string $text The text to be truncated. |
146
|
|
|
// * @param string $length The maximum lenght of the truncated string. Might |
147
|
|
|
// * return a shorter string if the lenght ends in the middle of a word. |
148
|
|
|
// * @param string $terminator The ellipsis terminator to use for the text. |
149
|
|
|
// * @return string |
150
|
|
|
// */ |
151
|
|
|
// public function truncate($text, $length, $terminator = ' ...') { |
152
|
|
|
// while (mb_substr($text, $length, 1) != ' ' && $length > 0) { |
153
|
|
|
// $length--; |
154
|
|
|
// } |
155
|
|
|
// return mb_substr($text, 0, $length) . $terminator; |
156
|
|
|
// } |
157
|
|
|
// |
158
|
|
|
// protected function generateFromString($string, $data) { |
159
|
|
|
// \ntentan\utils\StringStream::register(); |
160
|
|
|
// file_put_contents('string://template', $string); |
161
|
|
|
// $this->template = 'string://template'; |
162
|
|
|
// return $this->generate($data); |
163
|
|
|
// } |
164
|
|
|
// |
165
|
|
|
// public function __destruct() { |
166
|
|
|
// \ntentan\utils\StringStream::unregister(); |
167
|
|
|
// } |
168
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.