Completed
Pull Request — master (#320)
by Markus
168:32 queued 166:19
created

TemplateHelper::dump()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 29
ccs 0
cts 23
cp 0
rs 8.8571
cc 3
eloc 23
nc 3
nop 1
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\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
            $cloner = new VarCloner();
75
            if ('cli' === PHP_SAPI) {
76
                $dumper = new CliDumper();
77
            } else {
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
            return $dumper->dump($cloner->cloneVar($value));
97
        }
98
        return print_r($value, true);
99
    }
100
101
    /**
102
     * Convert a string to a slug version of itself
103
     *
104
     * @param  string $original
105
     * @return string
106
     */
107 1
    public function slug($original)
108
    {
109 1
        $slug = str_replace(" ", "-", $original);
110 1
        $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
111 1
        return strtolower($slug);
112
    }
113
114
    /**
115
     * Given a template path, render it within its own scope. This
116
     * method also accepts an array of additional variables to be
117
     * passed to the template.
118
     *
119
     * @param string $template
120
     * @param array  $additionalVariables
121
     */
122 1
    public function render($template, array $additionalVariables = null)
123
    {
124 1
        $variables = $this->getVariables();
125
126
        // Pass the helper to the template:
127 1
        $variables["tpl"] = $this;
128
129 1
        if ($additionalVariables !== null) {
130 1
            $variables = array_replace($variables, $additionalVariables);
131 1
        }
132
133 1
        call_user_func(function () {
134 1
            extract(func_get_arg(1));
135 1
            require func_get_arg(0);
136 1
        }, $template, $variables);
137 1
    }
138
139
    /**
140
     * Sets the variables to be passed to all templates rendered
141
     * by this template helper.
142
     *
143
     * @param array $variables
144
     */
145 1
    public function setVariables(array $variables)
146
    {
147 1
        $this->variables = $variables;
148 1
    }
149
150
    /**
151
     * Sets a single template variable, by its name:
152
     *
153
     * @param string $variableName
154
     * @param mixd   $variableValue
155
     */
156 1
    public function setVariable($variableName, $variableValue)
157
    {
158 1
        $this->variables[$variableName] = $variableValue;
159 1
    }
160
161
    /**
162
     * Gets a single template variable, by its name, or
163
     * $defaultValue if the variable does not exist
164
     *
165
     * @param  string $variableName
166
     * @param  mixed  $defaultValue
167
     * @return mixed
168
     */
169 1
    public function getVariable($variableName, $defaultValue = null)
170
    {
171 1
        return isset($this->variables[$variableName]) ?
172 1
            $this->variables[$variableName] : $defaultValue;
173
    }
174
175
    /**
176
     * Unsets a single template variable, by its name
177
     *
178
     * @param string $variableName
179
     */
180 1
    public function delVariable($variableName)
181
    {
182 1
        unset($this->variables[$variableName]);
183 1
    }
184
185
    /**
186
     * Returns all variables for this helper
187
     *
188
     * @return array
189
     */
190 1
    public function getVariables()
191
    {
192 1
        return $this->variables;
193
    }
194
}
195