Completed
Push — master ( 2cc253...afab26 )
by Denis
02:41
created

TemplateHelper::getCloner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
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 = array();
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
    private function getDumper()
83
    {
84
        if (!$this->htmlDumper && class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) {
85
            $this->htmlDumperOutput = new HtmlDumperOutput();
86
            // re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump.
87
            $this->htmlDumper = new HtmlDumper($this->htmlDumperOutput);
88
89
            $styles = array(
90
                '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',
91
                'num' => 'color:#BCD42A',
92
                'const' => 'color: #4bb1b1;',
93
                'str' => 'color:#BCD42A',
94
                'note' => 'color:#ef7c61',
95
                'ref' => 'color:#A0A0A0',
96
                'public' => 'color:#FFFFFF',
97
                'protected' => 'color:#FFFFFF',
98
                'private' => 'color:#FFFFFF',
99
                'meta' => 'color:#FFFFFF',
100
                'key' => 'color:#BCD42A',
101
                'index' => 'color:#ef7c61',
102
            );
103
            $this->htmlDumper->setStyles($styles);
104
        }
105
106
        return $this->htmlDumper;
107
    }
108
109
    /**
110
     * Format the given value into a human readable string.
111
     *
112
     * @param  mixed $value
113
     * @return string
114
     */
115
    public function dump($value)
116
    {
117
        $dumper = $this->getDumper();
118
119
        if ($dumper) {
120
            // re-use the same DumpOutput instance, so it won't re-render the global styles/scripts on each dump.
121
            // exclude verbose information (e.g. exception stack traces)
122
            $dumper->dump(
123
                $this->getCloner()->cloneVar($value, Caster::EXCLUDE_VERBOSE),
124
                $this->htmlDumperOutput
125
            );
126
127
            $output = $this->htmlDumperOutput->getOutput();
128
            $this->htmlDumperOutput->clear();
129
130
            return $output;
131
        }
132
133
        return print_r($value, true);
134
    }
135
136
    /**
137
     * Format the args of the given Frame as a human readable html string
138
     *
139
     * @param  Frame $frame
140
     * @return string the rendered html
141
     */
142
    public function dumpArgs(Frame $frame)
143
    {
144
        // we support frame args only when the optional dumper is available
145
        if (!$this->getDumper()) {
146
            return '';
147
        }
148
149
        $html = '';
150
        $numFrames = count($frame->getArgs());
151
152
        if ($numFrames > 0) {
153
            $html = '<ol class="linenums">';
154
            foreach($frame->getArgs() as $j => $frameArg) {
155
                $html .= '<li>'. $this->dump($frameArg) .'</li>';
156
            }
157
            $html .= '</ol>';
158
        }
159
160
        return $html;
161
    }
162
163
    /**
164
     * Convert a string to a slug version of itself
165
     *
166
     * @param  string $original
167
     * @return string
168
     */
169 1
    public function slug($original)
170
    {
171 1
        $slug = str_replace(" ", "-", $original);
172 1
        $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
173 1
        return strtolower($slug);
174
    }
175
176
    /**
177
     * Given a template path, render it within its own scope. This
178
     * method also accepts an array of additional variables to be
179
     * passed to the template.
180
     *
181
     * @param string $template
182
     * @param array  $additionalVariables
183
     */
184 1
    public function render($template, array $additionalVariables = null)
185
    {
186 1
        $variables = $this->getVariables();
187
188
        // Pass the helper to the template:
189 1
        $variables["tpl"] = $this;
190
191 1
        if ($additionalVariables !== null) {
192 1
            $variables = array_replace($variables, $additionalVariables);
193 1
        }
194
195 1
        call_user_func(function () {
196 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...
197 1
            require func_get_arg(0);
198 1
        }, $template, $variables);
199 1
    }
200
201
    /**
202
     * Sets the variables to be passed to all templates rendered
203
     * by this template helper.
204
     *
205
     * @param array $variables
206
     */
207 1
    public function setVariables(array $variables)
208
    {
209 1
        $this->variables = $variables;
210 1
    }
211
212
    /**
213
     * Sets a single template variable, by its name:
214
     *
215
     * @param string $variableName
216
     * @param mixd   $variableValue
217
     */
218 1
    public function setVariable($variableName, $variableValue)
219
    {
220 1
        $this->variables[$variableName] = $variableValue;
221 1
    }
222
223
    /**
224
     * Gets a single template variable, by its name, or
225
     * $defaultValue if the variable does not exist
226
     *
227
     * @param  string $variableName
228
     * @param  mixed  $defaultValue
229
     * @return mixed
230
     */
231 1
    public function getVariable($variableName, $defaultValue = null)
232
    {
233 1
        return isset($this->variables[$variableName]) ?
234 1
            $this->variables[$variableName] : $defaultValue;
235
    }
236
237
    /**
238
     * Unsets a single template variable, by its name
239
     *
240
     * @param string $variableName
241
     */
242 1
    public function delVariable($variableName)
243
    {
244 1
        unset($this->variables[$variableName]);
245 1
    }
246
247
    /**
248
     * Returns all variables for this helper
249
     *
250
     * @return array
251
     */
252 1
    public function getVariables()
253
    {
254 1
        return $this->variables;
255
    }
256
257
    /**
258
     * Set the cloner used for dumping variables.
259
     *
260
     * @param AbstractCloner $cloner
261
     */
262
    public function setCloner($cloner)
263
    {
264
        $this->cloner = $cloner;
265
    }
266
267
    /**
268
     * Get the cloner used for dumping variables.
269
     *
270
     * @return AbstractCloner
271
     */
272
    public function getCloner()
273
    {
274
        if (!$this->cloner) {
275
            $this->cloner = new VarCloner();
276
        }
277
        return $this->cloner;
278
    }
279
}
280