1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Whoops - php errors for cool kids |
4
|
|
|
* @author Filipe Dobreira <http://github.com/filp> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Whoops\Util; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
10
|
|
|
use Symfony\Component\VarDumper\Dumper\CliDumper; |
11
|
|
|
use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Exposes useful tools for working with/in templates |
15
|
|
|
*/ |
16
|
|
|
class TemplateHelper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* An array of variables to be passed to all templates |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $variables = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Escapes a string for output in an HTML document |
26
|
|
|
* |
27
|
|
|
* @param string $raw |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
2 |
|
public function escape($raw) |
31
|
|
|
{ |
32
|
2 |
|
$flags = ENT_QUOTES; |
33
|
|
|
|
34
|
|
|
// HHVM has all constants defined, but only ENT_IGNORE |
35
|
|
|
// works at the moment |
36
|
2 |
|
if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) { |
37
|
2 |
|
$flags |= ENT_SUBSTITUTE; |
38
|
2 |
|
} else { |
39
|
|
|
// This is for 5.3. |
40
|
|
|
// The documentation warns of a potential security issue, |
41
|
|
|
// but it seems it does not apply in our case, because |
42
|
|
|
// we do not blacklist anything anywhere. |
43
|
|
|
$flags |= ENT_IGNORE; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
return htmlspecialchars($raw, $flags, "UTF-8"); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Escapes a string for output in an HTML document, but preserves |
51
|
|
|
* URIs within it, and converts them to clickable anchor elements. |
52
|
|
|
* |
53
|
|
|
* @param string $raw |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
1 |
|
public function escapeButPreserveUris($raw) |
57
|
|
|
{ |
58
|
1 |
|
$escaped = $this->escape($raw); |
59
|
1 |
|
return preg_replace( |
60
|
1 |
|
"@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@", |
61
|
1 |
|
"<a href=\"$1\" target=\"_blank\">$1</a>", $escaped |
62
|
1 |
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Format the given value into a human readable string. |
67
|
|
|
* |
68
|
|
|
* @param mixed $value |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function dump($value) |
72
|
|
|
{ |
73
|
|
|
if (class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) { |
74
|
|
|
static $dumper = null; |
75
|
|
|
|
76
|
|
|
// re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump. |
77
|
|
|
if (!$dumper) { |
78
|
|
|
$dumper = new HtmlDumper(); |
79
|
|
|
|
80
|
|
|
$styles = array( |
81
|
|
|
'default' => '', |
82
|
|
|
'num' => '', |
83
|
|
|
'const' => '', |
84
|
|
|
'str' => '', |
85
|
|
|
'note' => '', |
86
|
|
|
'ref' => '', |
87
|
|
|
'public' => '', |
88
|
|
|
'protected' => '', |
89
|
|
|
'private' => '', |
90
|
|
|
'meta' => '', |
91
|
|
|
'key' => '', |
92
|
|
|
'index' => '', |
93
|
|
|
); |
94
|
|
|
$dumper->setStyles($styles); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$cloner = new VarCloner(); |
98
|
|
|
return $dumper->dump($cloner->cloneVar($value)); |
99
|
|
|
} |
100
|
|
|
return print_r($value, true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Convert a string to a slug version of itself |
105
|
|
|
* |
106
|
|
|
* @param string $original |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
1 |
|
public function slug($original) |
110
|
|
|
{ |
111
|
1 |
|
$slug = str_replace(" ", "-", $original); |
112
|
1 |
|
$slug = preg_replace('/[^\w\d\-\_]/i', '', $slug); |
113
|
1 |
|
return strtolower($slug); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Given a template path, render it within its own scope. This |
118
|
|
|
* method also accepts an array of additional variables to be |
119
|
|
|
* passed to the template. |
120
|
|
|
* |
121
|
|
|
* @param string $template |
122
|
|
|
* @param array $additionalVariables |
123
|
|
|
*/ |
124
|
1 |
|
public function render($template, array $additionalVariables = null) |
125
|
|
|
{ |
126
|
1 |
|
$variables = $this->getVariables(); |
127
|
|
|
|
128
|
|
|
// Pass the helper to the template: |
129
|
1 |
|
$variables["tpl"] = $this; |
130
|
|
|
|
131
|
1 |
|
if ($additionalVariables !== null) { |
132
|
1 |
|
$variables = array_replace($variables, $additionalVariables); |
133
|
1 |
|
} |
134
|
|
|
|
135
|
1 |
|
call_user_func(function () { |
136
|
1 |
|
extract(func_get_arg(1)); |
137
|
1 |
|
require func_get_arg(0); |
138
|
1 |
|
}, $template, $variables); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Sets the variables to be passed to all templates rendered |
143
|
|
|
* by this template helper. |
144
|
|
|
* |
145
|
|
|
* @param array $variables |
146
|
|
|
*/ |
147
|
1 |
|
public function setVariables(array $variables) |
148
|
|
|
{ |
149
|
1 |
|
$this->variables = $variables; |
150
|
1 |
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets a single template variable, by its name: |
154
|
|
|
* |
155
|
|
|
* @param string $variableName |
156
|
|
|
* @param mixd $variableValue |
157
|
|
|
*/ |
158
|
1 |
|
public function setVariable($variableName, $variableValue) |
159
|
|
|
{ |
160
|
1 |
|
$this->variables[$variableName] = $variableValue; |
161
|
1 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Gets a single template variable, by its name, or |
165
|
|
|
* $defaultValue if the variable does not exist |
166
|
|
|
* |
167
|
|
|
* @param string $variableName |
168
|
|
|
* @param mixed $defaultValue |
169
|
|
|
* @return mixed |
170
|
|
|
*/ |
171
|
1 |
|
public function getVariable($variableName, $defaultValue = null) |
172
|
|
|
{ |
173
|
1 |
|
return isset($this->variables[$variableName]) ? |
174
|
1 |
|
$this->variables[$variableName] : $defaultValue; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Unsets a single template variable, by its name |
179
|
|
|
* |
180
|
|
|
* @param string $variableName |
181
|
|
|
*/ |
182
|
1 |
|
public function delVariable($variableName) |
183
|
|
|
{ |
184
|
1 |
|
unset($this->variables[$variableName]); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Returns all variables for this helper |
189
|
|
|
* |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
1 |
|
public function getVariables() |
193
|
|
|
{ |
194
|
1 |
|
return $this->variables; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|