Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4                                                        |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2006 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 3.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available through the world-wide-web at the following url:           |
11
// | http://www.php.net/license/3_0.txt.                                  |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Authors: Frederic Poeydomenge <fpoeydomenge@free.fr>                 |
17
// +----------------------------------------------------------------------+
18
 
19
/**
20
 * Wrapper for the var_dump function.
21
 *
22
 * " The var_dump function displays structured information about expressions
23
 * that includes its type and value. Arrays are explored recursively
24
 * with values indented to show structure. "
25
 *
26
 * The Var_Dump class captures the output of the var_dump function,
27
 * by using output control functions, and then uses external renderer
28
 * classes for displaying the result in various graphical ways :
29
 * simple text, HTML/XHTML text, HTML/XHTML table, XML, ...
30
 *
31
 * @category  PHP
32
 * @package   Var_Dump
33
 * @author    Frederic Poeydomenge <fpoeydomenge@free.fr>
34
 * @copyright 1997-2006 The PHP Group
35
 * @license   http://www.php.net/license/3_0.txt PHP License 3.0
36
 * @version   CVS: $Id: Text.php 233111 2007-04-02 09:38:10Z fredericpoeydome $
37
 * @link      http://pear.php.net/package/Var_Dump
38
 */
39
 
40
/**
41
 * Include the base class for all renderers
42
 */
43
 
44
require_once 'Var_Dump/Renderer/Common.php';
45
 
46
/**
47
 * A concrete renderer for Var_Dump
48
 *
49
 * Returns a text-only representation of a variable
50
 *
51
 * @category  PHP
52
 * @package   Var_Dump
53
 * @author    Frederic Poeydomenge <fpoeydomenge@free.fr>
54
 * @copyright 1997-2006 The PHP Group
55
 * @license   http://www.php.net/license/3_0.txt PHP License 3.0
56
 * @version   CVS: $Id: Text.php 233111 2007-04-02 09:38:10Z fredericpoeydome $
57
 * @link      http://pear.php.net/package/Var_Dump
58
 */
59
 
60
class Var_Dump_Renderer_Text extends Var_Dump_Renderer_Common
61
{
62
 
63
    /**
64
     * Default configuration options.
65
     *
66
     * Valid configuration options are :
67
     *     show_container  : bool,    Show the root Element or not
68
     *     show_eol        : string,  String to insert before a newline, or false
69
     *     mode            : string,  Can be one of the following displaying modes
70
     *       'compact' = no keys alignment
71
     *       'normal'  = keys alignment, proportional spacing
72
     *       'wide'    = keys alignment, wider spacing
73
     *     offset          : integer, Offset between the start of a group and the content
74
     *     opening         : string,  Opening character
75
     *     closing         : string,  Closing character
76
     *     operator        : string,  Operator symbol
77
     *     is_html         : bool,    Do we need to htmlspecialchars() the texts
78
     *     before_text     : string,  Text to insert before the text
79
     *     after_text      : string,  Text to insert after the text
80
     *     before_num_key  : string,  Text to insert before a numerical key
81
     *     after_num_key   : string,  Text to insert after a numerical key
82
     *     before_str_key  : string,  Text to insert before a string key
83
     *     after_str_key   : string,  Text to insert after a string key
84
     *     before_operator : string,  Text to insert before the operator
85
     *     after_operator  : string,  Text to insert after the operator
86
     *     before_type     : string,  Text to insert before a type
87
     *     after_type      : string,  Text to insert after a type
88
     *     before_value    : string,  Text to insert before a value
89
     *     after_value     : string,  Text to insert after a value
90
     *
91
     * @var    array
92
     * @access public
93
     */
94
    var $defaultOptions = array(
95
        'show_container'  => TRUE,
96
        'show_eol'        => FALSE,
97
        'mode'            => 'compact',
98
        'offset'          => 2,
99
        'opening'         => '{',
100
        'closing'         => '}',
101
        'operator'        => ' => ',
102
        'is_html'         => FALSE,
103
        'before_text'     => '',
104
        'after_text'      => '',
105
        'before_num_key'  => '',
106
        'after_num_key'   => '',
107
        'before_str_key'  => '',
108
        'after_str_key'   => '',
109
        'before_operator' => '',
110
        'after_operator'  => '',
111
        'before_type'     => '',
112
        'after_type'      => '',
113
        'before_value'    => '',
114
        'after_value'     => ''
115
    );
116
 
117
    /**
118
     * Class constructor.
119
     *
120
     * @param array $options Parameters for the rendering.
121
     * @access public
122
     */
123
    function Var_Dump_Renderer_Text($options = array())
124
    {
125
        $this->setOptions($options);
126
    }
127
 
128
    /**
129
     * Returns the string representation of a variable.
130
     *
131
     * @return string The string representation of the variable.
132
     * @access public
133
     */
134
    function toString()
135
    {
136
        $parent = array();
137
        $stackOffset = array(0);
138
        $offset = 0;
139
        $txt = $this->options['before_text'];
140
        $counter = count($this->family);
141
        for ($c = 0 ; $c < $counter ; $c++) {
142
            switch ($this->family[$c]) {
143
                case VAR_DUMP_START_GROUP :
144
                    if (! empty($parent)) {
145
                        $offset = end($stackOffset)
146
                            + $this->keyLen[end($parent)]
147
                            + $this->_len($this->options['operator']);
148
                        array_push($stackOffset, $offset);
149
                    }
150
                    array_push($parent, $c);
151
                    if ($this->options['show_container'] or $this->depth[$c] > 0) {
152
                        if ($this->options['is_html']) {
153
                            $txt .= htmlspecialchars($this->value[$c]);
154
                        } else {
155
                            $txt .= $this->value[$c];
156
                        }
157
                        $txt .= ' ' . $this->options['opening'] . "\n";
158
                    }
159
                    break;
160
                case VAR_DUMP_FINISH_GROUP :
161
                    if ($this->depth[$c] > 0) {
162
                        $offset = $this->depth[$c] * $this->options['offset'];
163
                        if ($this->options['mode'] == 'wide') {
164
                            $offset += end($stackOffset);
165
                        }
166
                        if (!$this->options['show_container']) {
167
                            $offset -= $this->options['offset'];
168
                        }
169
                        $txt .= str_repeat(' ', $offset);
170
                    }
171
                    if ($this->options['show_container'] or $this->depth[$c] > 0) {
172
                        $txt .= $this->options['closing'] . "\n";
173
                    }
174
                    array_pop($parent);
175
                    array_pop($stackOffset);
176
                    break;
177
                case VAR_DUMP_START_ELEMENT_NUM :
178
                case VAR_DUMP_START_ELEMENT_STR :
179
                    if ($this->depth[$c] > 0) {
180
                        $offset = $this->depth[$c] * $this->options['offset'];
181
                        if ($this->options['mode'] == 'wide') {
182
                            $offset += end($stackOffset);
183
                        }
184
                        if (! $this->options['show_container']) {
185
                            $offset -= $this->options['offset'];
186
                        }
187
                        $txt .= str_repeat(' ', $offset);
188
                    }
189
                    if ($this->options['mode'] == 'compact') {
190
                        $txt .= $this->_getStartElement($c);
191
                        $offset += $this->_len($this->value[$c]);
192
                    } else {
193
                        $txt .= sprintf(
194
                            '%-' . $this->keyLen[end($parent)] . 's',
195
                            $this->_getStartElement($c)
196
                        );
197
                        $offset += $this->keyLen[end($parent)];
198
                    }
199
                    $txt .= $this->_getOperator();
200
                    if ($this->family[$c]==VAR_DUMP_START_ELEMENT_NUM) {
201
                        $offset +=
202
                            $this->_len($this->options['before_num_key']) +
203
                            $this->_len($this->options['after_num_key']);
204
                    }
205
                    if ($this->family[$c]==VAR_DUMP_START_ELEMENT_STR) {
206
                        $offset +=
207
                            $this->_len($this->options['before_str_key']) +
208
                            $this->_len($this->options['after_str_key']);
209
                    }
210
                    $offset +=
211
                        $this->_len($this->options['before_operator']) +
212
                        $this->_len($this->options['operator']) +
213
                        $this->_len($this->options['after_operator']) +
214
                        $this->_len($this->options['before_type']) +
215
                        $this->_len($this->options['after_type']);
216
                    break;
217
                case VAR_DUMP_FINISH_ELEMENT :
218
                    $txt .= $this->_getFinishElement($c) . "\n";
219
                    break;
220
                case VAR_DUMP_FINISH_STRING :
221
                    // offset is the value set during the previous pass
222
                    // in VAR_DUMP_START_ELEMENT_*
223
                    $txt .= preg_replace(
224
                        '/(?<=\n)^/m',
225
                        $this->options['after_value'] .
226
                            str_repeat(' ', $offset + $this->_len($this->type[$c]) + 1) .
227
                            $this->options['before_value'],
228
                        $this->_getFinishElement($c)
229
                    ) . "\n";
230
                    break;
231
            }
232
        }
233
        $txt .= $this->options['after_text'];
234
        return rtrim($txt);
235
    }
236
 
237
    /**
238
     * Returns the lenght of the shift (string without tags).
239
     *
240
     * @param string $string The string.
241
     * @return integer Length of the shift.
242
     * @access private
243
     */
244
    function _len($string)
245
    {
246
        if ($this->options['is_html']) {
247
            return strlen(strip_tags($string));
248
        } else {
249
            return strlen($string);
250
        }
251
    }
252
 
253
    /**
254
     * Returns the operator symbol.
255
     *
256
     * @return string The operator symbol.
257
     * @access private
258
     */
259
    function _getOperator()
260
    {
261
        $txt = $this->options['before_operator'];
262
        if ($this->options['is_html']) {
263
            $txt .= htmlspecialchars($this->options['operator']);
264
        } else {
265
            $txt .= $this->options['operator'];
266
        }
267
        $txt .= $this->options['after_operator'];
268
        return $txt;
269
    }
270
 
271
    /**
272
     * Returns the key of the element.
273
     *
274
     * @param integer $c Index of the element.
275
     * @return string The key of the element.
276
     * @access private
277
     */
278
    function _getStartElement($c)
279
    {
280
        $comp = ($this->family[$c] == VAR_DUMP_START_ELEMENT_NUM) ? 'num' : 'str';
281
        $txt = $this->options['before_' . $comp . '_key'];
282
        if ($this->options['is_html']) {
283
            $txt .= htmlspecialchars($this->value[$c]);
284
        } else {
285
            $txt .= $this->value[$c];
286
        }
287
        $txt .= $this->options['after_' . $comp . '_key'];
288
        return $txt;
289
    }
290
 
291
    /**
292
     * Returns the value of the element.
293
     *
294
     * @param integer $c Index of the element.
295
     * @return string The value of the element.
296
     * @access private
297
     */
298
    function _getFinishElement($c)
299
    {
300
        $txt = $this->options['before_type'];
301
        if ($this->options['is_html']) {
302
            $txt .= htmlspecialchars($this->type[$c]);
303
        } else {
304
            $txt .= $this->type[$c];
305
        }
306
        $txt .= $this->options['after_type'];
307
        if (! is_null($this->value[$c])) {
308
            $txt .= ' ' . $this->options['before_value'];
309
            if ($this->options['is_html']) {
310
                $string = htmlspecialchars($this->value[$c]);
311
            } else {
312
                $string = $this->value[$c];
313
            }
314
            if ($this->options['show_eol'] !== FALSE) {
315
                $string = str_replace(
316
                    "\n",
317
                    $this->options['show_eol'] . "\n",
318
                    $string
319
                );
320
            }
321
            $txt .= $string . $this->options['after_value'];
322
        }
323
        return $txt;
324
    }
325
 
326
}
327
 
328
?>