Completed
Push — separate-soap-handler ( aa8a6b...a6aaf5 )
by Denis
62:14 queued 01:08
created

TemplateHelper::dump()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 8.8571
cc 3
eloc 22
nc 3
nop 1
crap 3
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 2
     *
27
     * @param  string $raw
28 2
     * @return string
29
     */
30
    public function escape($raw)
31
    {
32 2
        $flags = ENT_QUOTES;
33 2
34 2
        // HHVM has all constants defined, but only ENT_IGNORE
35
        // works at the moment
36
        if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
37
            $flags |= ENT_SUBSTITUTE;
38
        } 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 2
            // we do not blacklist anything anywhere.
43
            $flags |= ENT_IGNORE;
44
        }
45
46
        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 1
     *
53
     * @param  string $raw
54 1
     * @return string
55 1
     */
56 1
    public function escapeButPreserveUris($raw)
57 1
    {
58 1
        $escaped = $this->escape($raw);
59
        return preg_replace(
60
            "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
61
            "<a href=\"$1\" target=\"_blank\">$1</a>", $escaped
62
        );
63
    }
64
65
    /**
66
     * Format the given value into a human readable string.
67 1
     *
68
     * @param  mixed $value
69 1
     * @return string
70 1
     */
71 1
    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 1
                    'num' => '',
83
                    'const' => '',
84 1
                    'str' => '',
85
                    'note' => '',
86
                    'ref' => '',
87 1
                    'public' => '',
88
                    'protected' => '',
89 1
                    'private' => '',
90 1
                    'meta' => '',
91 1
                    'key' => '',
92
                    'index' => '',
93 1
                );
94 1
                $dumper->setStyles($styles);
95 1
            }
96 1
97 1
            $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 1
     *
106
     * @param  string $original
107 1
     * @return string
108 1
     */
109
    public function slug($original)
110
    {
111
        $slug = str_replace(" ", "-", $original);
112
        $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
113
        return strtolower($slug);
114
    }
115
116 1
    /**
117
     * Given a template path, render it within its own scope. This
118 1
     * method also accepts an array of additional variables to be
119 1
     * passed to the template.
120
     *
121
     * @param string $template
122
     * @param array  $additionalVariables
123
     */
124
    public function render($template, array $additionalVariables = null)
125
    {
126
        $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
        }
134
135
        call_user_func(function () {
136
            extract(func_get_arg(1));
137
            require func_get_arg(0);
138
        }, $template, $variables);
139
    }
140 1
141
    /**
142 1
     * Sets the variables to be passed to all templates rendered
143 1
     * by this template helper.
144
     *
145
     * @param array $variables
146
     */
147
    public function setVariables(array $variables)
148
    {
149
        $this->variables = $variables;
150 1
    }
151
152 1
    /**
153
     * Sets a single template variable, by its name:
154
     *
155
     * @param string $variableName
156
     * @param mixd   $variableValue
157
     */
158
    public function setVariable($variableName, $variableValue)
159
    {
160
        $this->variables[$variableName] = $variableValue;
161
    }
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
    public function getVariable($variableName, $defaultValue = null)
172
    {
173
        return isset($this->variables[$variableName]) ?
174
            $this->variables[$variableName] : $defaultValue;
175
    }
176
177
    /**
178
     * Unsets a single template variable, by its name
179
     *
180
     * @param string $variableName
181
     */
182
    public function delVariable($variableName)
183
    {
184
        unset($this->variables[$variableName]);
185
    }
186
187
    /**
188
     * Returns all variables for this helper
189
     *
190
     * @return array
191
     */
192
    public function getVariables()
193
    {
194
        return $this->variables;
195
    }
196
}
197