Completed
Pull Request — v1 (#450)
by Denis
02:54
created

TemplateHelper::getDumper()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 20
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 2
nop 0
crap 12
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\Caster\Caster;
10
use Symfony\Component\VarDumper\Cloner\AbstractCloner;
11
use Symfony\Component\VarDumper\Cloner\VarCloner;
12
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
13
use Whoops\Exception\Frame;
14
15
/**
16
 * Exposes useful tools for working with/in templates
17
 */
18
class TemplateHelper
19
{
20
    /**
21
     * An array of variables to be passed to all templates
22
     * @var array
23
     */
24
    private $variables = [];
25
26
    /**
27
     * @var HtmlDumper
28
     */
29
    private $htmlDumper;
30
31
    /**
32
     * @var HtmlDumperOutput
33
     */
34
    private $htmlDumperOutput;
35
36
    /**
37
     * @var AbstractCloner
38
     */
39
    private $cloner;
40
41
    /**
42
     * Escapes a string for output in an HTML document
43
     *
44
     * @param  string $raw
45
     * @return string
46
     */
47 2
    public function escape($raw)
48
    {
49 2
        $flags = ENT_QUOTES;
50
51
        // HHVM has all constants defined, but only ENT_IGNORE
52
        // works at the moment
53 2
        if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
54 2
            $flags |= ENT_SUBSTITUTE;
55 2
        } else {
56
            // This is for 5.3.
57
            // The documentation warns of a potential security issue,
58
            // but it seems it does not apply in our case, because
59
            // we do not blacklist anything anywhere.
60
            $flags |= ENT_IGNORE;
61
        }
62
63 2
        return htmlspecialchars($raw, $flags, "UTF-8");
64
    }
65
66
    /**
67
     * Escapes a string for output in an HTML document, but preserves
68
     * URIs within it, and converts them to clickable anchor elements.
69
     *
70
     * @param  string $raw
71
     * @return string
72
     */
73 1
    public function escapeButPreserveUris($raw)
74
    {
75 1
        $escaped = $this->escape($raw);
76 1
        return preg_replace(
77 1
            "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
78 1
            "<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
79 1
        );
80
    }
81
82
    /**
83
     * Makes sure that the given string breaks on the delimiter.
84
     *
85
     * @param  string $delimiter
86
     * @param  string $s
87
     * @return string
88
     */
89
    public function breakOnDelimiter($delimiter, $s)
90
    {
91
        $parts = explode($delimiter, $s);
92
        foreach ($parts as &$part) {
93
            $part = '<div class="delimiter">' . $part . '</div>';
94
        }
95
96
        return implode($delimiter, $parts);
97
    }
98
99
    /**
100
     * Replace the part of the path that all files have in common.
101
     *
102
     * @param  string $path
103
     * @return string
104
     */
105
    public function shorten($path)
106
    {
107
        $dirname = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
108
        if ($dirname != "/") {
109
            $path = str_replace($dirname, '&hellip;', $path);
110
        }
111
112
        return $path;
113
    }
114
115
    private function getDumper()
116
    {
117
        if (!$this->htmlDumper && class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) {
118
            $this->htmlDumperOutput = new HtmlDumperOutput();
119
            // re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump.
120
            $this->htmlDumper = new HtmlDumper($this->htmlDumperOutput);
121
122
            $styles = [
123
                'default' => 'color:#FFFFFF; line-height:normal; font:12px "Inconsolata", "Fira Mono", "Source Code Pro", Monaco, Consolas, "Lucida Console", monospace !important; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: normal',
124
                'num' => 'color:#BCD42A',
125
                'const' => 'color: #4bb1b1;',
126
                'str' => 'color:#BCD42A',
127
                'note' => 'color:#ef7c61',
128
                'ref' => 'color:#A0A0A0',
129
                'public' => 'color:#FFFFFF',
130
                'protected' => 'color:#FFFFFF',
131
                'private' => 'color:#FFFFFF',
132
                'meta' => 'color:#FFFFFF',
133
                'key' => 'color:#BCD42A',
134
                'index' => 'color:#ef7c61',
135
            ];
136
            $this->htmlDumper->setStyles($styles);
137
        }
138
139
        return $this->htmlDumper;
140
    }
141
142
    /**
143
     * Format the given value into a human readable string.
144
     *
145
     * @param  mixed $value
146
     * @return string
147
     */
148
    public function dump($value)
149
    {
150
        $dumper = $this->getDumper();
151
152
        if ($dumper) {
153
            // re-use the same DumpOutput instance, so it won't re-render the global styles/scripts on each dump.
154
            // exclude verbose information (e.g. exception stack traces)
155
            if (class_exists('Symfony\Component\VarDumper\Caster\Caster')) {
156
                $cloneVar = $this->getCloner()->cloneVar($value, Caster::EXCLUDE_VERBOSE);
157
            // Symfony VarDumper 2.6 Caster class dont exist.
158
            } else {
159
                $cloneVar = $this->getCloner()->cloneVar($value);
160
            }
161
162
            $dumper->dump(
163
                $cloneVar,
164
                $this->htmlDumperOutput
165
            );
166
167
            $output = $this->htmlDumperOutput->getOutput();
168
            $this->htmlDumperOutput->clear();
169
170
            return $output;
171
        }
172
173
        return print_r($value, true);
174
    }
175
176
    /**
177
     * Format the args of the given Frame as a human readable html string
178
     *
179
     * @param  Frame $frame
180
     * @return string the rendered html
181
     */
182
    public function dumpArgs(Frame $frame)
183
    {
184
        // we support frame args only when the optional dumper is available
185
        if (!$this->getDumper()) {
186
            return '';
187
        }
188
189
        $html = '';
190
        $numFrames = count($frame->getArgs());
191
192
        if ($numFrames > 0) {
193
            $html = '<ol class="linenums">';
194
            foreach($frame->getArgs() as $j => $frameArg) {
195
                $html .= '<li>'. $this->dump($frameArg) .'</li>';
196
            }
197
            $html .= '</ol>';
198
        }
199
200
        return $html;
201
    }
202
203
    /**
204
     * Convert a string to a slug version of itself
205
     *
206
     * @param  string $original
207
     * @return string
208
     */
209 1
    public function slug($original)
210
    {
211 1
        $slug = str_replace(" ", "-", $original);
212 1
        $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
213 1
        return strtolower($slug);
214
    }
215
216
    /**
217
     * Given a template path, render it within its own scope. This
218
     * method also accepts an array of additional variables to be
219
     * passed to the template.
220
     *
221
     * @param string $template
222
     * @param array  $additionalVariables
223
     */
224 1
    public function render($template, array $additionalVariables = null)
225
    {
226 1
        $variables = $this->getVariables();
227
228
        // Pass the helper to the template:
229 1
        $variables["tpl"] = $this;
230
231 1
        if ($additionalVariables !== null) {
232 1
            $variables = array_replace($variables, $additionalVariables);
233 1
        }
234
235 1
        call_user_func(function () {
236 1
            extract(func_get_arg(1));
0 ignored issues
show
Bug introduced by
func_get_arg(1) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
237 1
            require func_get_arg(0);
238 1
        }, $template, $variables);
239 1
    }
240
241
    /**
242
     * Sets the variables to be passed to all templates rendered
243
     * by this template helper.
244
     *
245
     * @param array $variables
246
     */
247 1
    public function setVariables(array $variables)
248
    {
249 1
        $this->variables = $variables;
250 1
    }
251
252
    /**
253
     * Sets a single template variable, by its name:
254
     *
255
     * @param string $variableName
256
     * @param mixd   $variableValue
257
     */
258 1
    public function setVariable($variableName, $variableValue)
259
    {
260 1
        $this->variables[$variableName] = $variableValue;
261 1
    }
262
263
    /**
264
     * Gets a single template variable, by its name, or
265
     * $defaultValue if the variable does not exist
266
     *
267
     * @param  string $variableName
268
     * @param  mixed  $defaultValue
269
     * @return mixed
270
     */
271 1
    public function getVariable($variableName, $defaultValue = null)
272
    {
273 1
        return isset($this->variables[$variableName]) ?
274 1
            $this->variables[$variableName] : $defaultValue;
275
    }
276
277
    /**
278
     * Unsets a single template variable, by its name
279
     *
280
     * @param string $variableName
281
     */
282 1
    public function delVariable($variableName)
283
    {
284 1
        unset($this->variables[$variableName]);
285 1
    }
286
287
    /**
288
     * Returns all variables for this helper
289
     *
290
     * @return array
291
     */
292 1
    public function getVariables()
293
    {
294 1
        return $this->variables;
295
    }
296
297
    /**
298
     * Set the cloner used for dumping variables.
299
     *
300
     * @param AbstractCloner $cloner
301
     */
302
    public function setCloner($cloner)
303
    {
304
        $this->cloner = $cloner;
305
    }
306
307
    /**
308
     * Get the cloner used for dumping variables.
309
     *
310
     * @return AbstractCloner
311
     */
312
    public function getCloner()
313
    {
314
        if (!$this->cloner) {
315
            $this->cloner = new VarCloner();
316
        }
317
        return $this->cloner;
318
    }
319
}
320