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 Exception; |
30
|
|
|
use ntentan\honam\engines\php\HelperVariable; |
31
|
|
|
use ntentan\honam\engines\php\Janitor; |
32
|
|
|
use ntentan\honam\engines\php\Variable; |
33
|
|
|
use ntentan\honam\TemplateRenderer; |
34
|
|
|
use ntentan\utils\StringStream; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The PHP engine is a template engine built into honam which uses raw PHP as |
38
|
|
|
* the template language. By virtue of this, the PHP engine can boast of high |
39
|
|
|
* performance. Since this engine uses PHP, it has access to all the language's |
40
|
|
|
* features. This could sometimes create a problem since some of these features |
41
|
|
|
* are not intended for templating use. |
42
|
|
|
*/ |
43
|
|
|
class PhpEngine extends AbstractEngine |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var HelperVariable |
47
|
|
|
*/ |
48
|
|
|
private $helperVariable; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var Janitor |
52
|
|
|
*/ |
53
|
|
|
private $janitor; |
54
|
|
|
private $templateRenderer; |
55
|
|
|
|
56
|
27 |
|
public function __construct(TemplateRenderer $templateRenderer, HelperVariable $helperVariable, Janitor $janitor) |
57
|
|
|
{ |
58
|
27 |
|
$this->helperVariable = $helperVariable; |
59
|
27 |
|
$this->janitor = $janitor; |
60
|
27 |
|
$this->templateRenderer = $templateRenderer; |
61
|
27 |
|
} |
62
|
|
|
|
63
|
16 |
|
public function partial($template, $templateData = array()) |
64
|
|
|
{ |
65
|
16 |
|
return $this->templateRenderer->render($template, $templateData); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param mixed $item |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
9 |
|
public function unescape($item) |
73
|
|
|
{ |
74
|
9 |
|
if($item instanceof Variable) { |
75
|
9 |
|
return $item->unescape(); |
76
|
|
|
} else { |
77
|
|
|
return $item; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Passes the data to be rendered to the template engine instance. |
83
|
|
|
*/ |
84
|
27 |
|
public function renderFromFileTemplate(string $filePath, array $data) : string |
85
|
|
|
{ |
86
|
|
|
// Escape each variable by passing it through the variable class. |
87
|
|
|
// Users would have to unescape them by calling the escape method directly |
88
|
|
|
// on the variable. |
89
|
27 |
|
foreach ($data as $_key => $_value) { |
90
|
24 |
|
$$_key = Variable::initialize($_value, $this->janitor); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Expose helpers |
94
|
27 |
|
$helpers = $this->helperVariable; |
95
|
|
|
|
96
|
|
|
// Start trapping the output buffer and include the PHP template for |
97
|
|
|
// execution. |
98
|
27 |
|
ob_start(); |
99
|
|
|
try { |
100
|
27 |
|
include $filePath; |
101
|
|
|
} catch (Exception $e) { |
102
|
|
|
ob_get_flush(); |
103
|
|
|
throw $e; |
104
|
|
|
} |
105
|
27 |
|
return ob_get_clean(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Passes a template string and data to be rendered to the template engine |
110
|
|
|
* instance. |
111
|
|
|
*/ |
112
|
|
|
public function renderFromStringTemplate(string $string, array $data) : string |
113
|
|
|
{ |
114
|
|
|
StringStream::register(); |
115
|
|
|
file_put_contents('string://template', $string); |
116
|
|
|
return $this->renderFromFileTemplate('string://template', $data); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* A utility function to strip the text of all HTML code. This function |
121
|
|
|
* removes all HTML tags instead of escaping them. |
122
|
|
|
* |
123
|
|
|
* @param string $text |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
1 |
|
public function strip($text) |
127
|
|
|
{ |
128
|
1 |
|
return $this->janitor->cleanHtml($text, true); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* A utility function to cut long pieces of text into meaningful short |
133
|
|
|
* chunks. The function is very good in cases where you want to show just |
134
|
|
|
* a short preview snippet of a long text. The function cuts the long string |
135
|
|
|
* without cutting through words and appends some sort of ellipsis |
136
|
|
|
* terminator to the text. |
137
|
|
|
* |
138
|
|
|
* @param string $text The text to be truncated. |
139
|
|
|
* @param integer $length The maximum lenght of the truncated string. Might |
140
|
|
|
* return a shorter string if the lenght ends in the middle of a word. |
141
|
|
|
* @param string $terminator The ellipsis terminator to use for the text. |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
1 |
|
public function truncate($text, $length, $terminator = ' ...') |
145
|
|
|
{ |
146
|
1 |
|
while (mb_substr($text, $length, 1) != ' ' && $length > 0) { |
147
|
1 |
|
$length--; |
148
|
|
|
} |
149
|
1 |
|
return mb_substr($text, 0, $length) . $terminator; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function __destruct() |
153
|
|
|
{ |
154
|
|
|
StringStream::unregister(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|