Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Copyright (c) 2008-2009, Laurent Laville <pear@laurent-laville.org>
4
 *
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *     * Redistributions of source code must retain the above copyright
12
 *       notice, this list of conditions and the following disclaimer.
13
 *     * Redistributions in binary form must reproduce the above copyright
14
 *       notice, this list of conditions and the following disclaimer in the
15
 *       documentation and/or other materials provided with the distribution.
16
 *     * Neither the name of the authors nor the names of its contributors
17
 *       may be used to endorse or promote products derived from this software
18
 *       without specific prior written permission.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGE.
31
 *
32
 * PHP versions 4 and 5
33
 *
34
 * @category PHP
35
 * @package  PHP_CompatInfo
36
 * @author   Laurent Laville <pear@laurent-laville.org>
37
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
38
 * @version  CVS: $Id: Text.php,v 1.13 2009/01/02 10:18:47 farell Exp $
39
 * @link     http://pear.php.net/package/PHP_CompatInfo
40
 * @since    File available since Release 1.8.0b3
41
 */
42
 
43
require_once 'Console/Table.php';
44
 
45
/**
46
 * Text renderer for PHP_CompatInfo component.
47
 *
48
 * The PHP_CompatInfo_Renderer_Text class is a concrete implementation
49
 * of PHP_CompatInfo_Renderer abstract class. It simply display results
50
 * for the command line interface with help of PEAR::Console_Table
51
 *
52
 * @category PHP
53
 * @package  PHP_CompatInfo
54
 * @author   Laurent Laville <pear@laurent-laville.org>
55
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
56
 * @version  Release: 1.9.0
57
 * @link     http://pear.php.net/package/PHP_CompatInfo
58
 * @since    Class available since Release 1.8.0b3
59
 */
60
class PHP_CompatInfo_Renderer_Text extends PHP_CompatInfo_Renderer
61
{
62
    /**
63
     * Text Renderer Class constructor (ZE1) for PHP4
64
     *
65
     * @param object &$parser Instance of the parser (model of MVC pattern)
66
     * @param array  $conf    A hash containing any additional configuration
67
     *
68
     * @access public
69
     * @since  version 1.8.0b3 (2008-06-07)
70
     */
71
    function PHP_CompatInfo_Renderer_Text(&$parser, $conf)
72
    {
73
        $this->__construct($parser, $conf);
74
    }
75
 
76
    /**
77
     * Text Renderer Class constructor (ZE2) for PHP5+
78
     *
79
     * @param object &$parser Instance of the parser (model of MVC pattern)
80
     * @param array  $conf    A hash containing any additional configuration
81
     *
82
     * @access public
83
     * @since  version 1.8.0b3 (2008-06-07)
84
     */
85
    function __construct(&$parser, $conf)
86
    {
87
        $defaults = array('colwidth' => array('f' => 29, 'e' => 12, 'c' => 20));
88
        $conf     = array_merge($defaults, $conf);
89
 
90
        parent::PHP_CompatInfo_Renderer($parser, $conf);
91
    }
92
 
93
    /**
94
     * Display final results
95
     *
96
     * Display final results, when data source parsing is over.
97
     *
98
     * @access public
99
     * @return void
100
     * @since  version 1.8.0b3 (2008-06-07)
101
     */
102
    function display()
103
    {
104
        $o    = $this->args['output-level'];
105
        $info = $this->parseData;
106
        $hdr  = array();
107
        $src  = $this->_parser->dataSource;
108
 
109
        if (isset($this->args['dir'])) {
110
            $dir   = $this->args['dir'];
111
            $hdr[] = 'Files';
112
        } elseif (isset($this->args['file'])) {
113
            $file  = $this->args['file'];
114
            $hdr[] = 'File';
115
        } elseif (isset($this->args['string'])) {
116
            $string = $this->args['string'];
117
            $hdr[]  = 'Source code';
118
        } elseif ($src['dataType'] == 'directory') {
119
            $dir   = $src['dataSource'];
120
            $hdr[] = 'Files';
121
        } elseif ($src['dataType'] == 'file') {
122
            $file  = $src['dataSource'];
123
            $hdr[] = 'File';
124
        } else {
125
            $string = $src['dataSource'];
126
            $hdr[]  = 'Source code';
127
        }
128
 
129
        $table = new Console_Table();
130
        $f     = 0;
131
        if ($o & 16) {
132
            $hdr[] = 'Version';
133
            $f++;
134
        }
135
        if ($o & 1) {
136
            $hdr[] = 'C';
137
            $f++;
138
        }
139
        if ($o & 2) {
140
            $hdr[]   = 'Extensions';
141
            $filter2 = array(&$this, '_splitExtname');
142
            $table->addFilter($f+1, $filter2);
143
            $f++;
144
        }
145
        if ($o & 4) {
146
            if ($o & 8) {
147
                $hdr[] = 'Constants/Tokens';
148
            } else {
149
                $hdr[] = 'Constants';
150
            }
151
            $f++;
152
        } else {
153
            if ($o & 8) {
154
                $hdr[] = 'Tokens';
155
                $f++;
156
            }
157
        }
158
        $table->setHeaders($hdr);
159
        $filter0 = array(&$this, '_splitFilename');
160
        $table->addFilter(0, $filter0);
161
        if ($o > 3 && $o < 16 || $o > 19) {
162
            $filter3 = array(&$this, '_splitConstant');
163
            $table->addFilter($f, $filter3);
164
        }
165
 
166
        $ext   = implode("\r\n", $info['extensions']);
167
        $const = implode("\r\n", array_merge($info['constants'], $info['tokens']));
168
        if (isset($dir)) {
169
            $ds   = DIRECTORY_SEPARATOR;
170
            $dir  = str_replace(array('\\', '/'), $ds, $dir);
171
            $data = array($dir . $ds . '*');
172
        } elseif (isset($file)) {
173
            $data = array($file);
174
        } else {
175
            $data = array('<?php ... ?>');
176
        }
177
 
178
        if ($o & 16) {
179
            if (empty($info['max_version'])) {
180
                $data[] = $info['version'];
181
            } else {
182
                $data[] = implode("\r\n", array($info['version'],
183
                                                $info['max_version']));
184
            }
185
        }
186
        if ($o & 1) {
187
            $data[] = $info['cond_code'][0];
188
        }
189
        if ($o & 2) {
190
            $data[] = $ext;
191
        }
192
        if ($o & 4) {
193
            if ($o & 8) {
194
                $data[] = $const;
195
            } else {
196
                $data[] = implode("\r\n", $info['constants']);
197
            }
198
        } else {
199
            if ($o & 8) {
200
                $data[] = implode("\r\n", $info['tokens']);
201
            }
202
        }
203
 
204
        $table->addRow($data);
205
 
206
        unset($info['max_version']);
207
        unset($info['version']);
208
        unset($info['classes']);
209
        unset($info['functions']);
210
        unset($info['extensions']);
211
        unset($info['constants']);
212
        unset($info['tokens']);
213
        unset($info['cond_code']);
214
 
215
        $ignored       = $info['ignored_files'];
216
        $all_functions = array();
217
 
218
        unset($info['ignored_files']);
219
        unset($info['ignored_functions']);
220
        unset($info['ignored_extensions']);
221
        unset($info['ignored_constants']);
222
 
223
        // summarize : print only summary for directory without files details
224
        if ($this->args['summarize'] === false && isset($dir)) {
225
 
226
            foreach ($info as $file => $info) {
227
                if (is_numeric($file{0})) {
228
                    // extra information available only when debug mode is on
229
                    $all_functions[$file] = $info;
230
                    continue;
231
                }
232
                if ($info === false) {
233
                    continue;  // skip this (invalid) file
234
                }
235
                $ext   = implode("\r\n", $info['extensions']);
236
                $const = implode("\r\n", array_merge($info['constants'],
237
                                                     $info['tokens']));
238
 
239
                $file = str_replace(array('\\', '/'), $ds, $file);
240
                $table->addSeparator();
241
 
242
                $data = array($file);
243
                if ($o & 16) {
244
                    if (empty($info['max_version'])) {
245
                        $data[] = $info['version'];
246
                    } else {
247
                        $data[] = implode("\r\n", array($info['version'],
248
                                                        $info['max_version']));
249
                    }
250
                }
251
                if ($o & 1) {
252
                    $data[] = $info['cond_code'][0];
253
                }
254
                if ($o & 2) {
255
                    $data[] = $ext;
256
                }
257
                if ($o & 4) {
258
                    if ($o & 8) {
259
                        $data[] = $const;
260
                    } else {
261
                        $data[] = implode("\r\n", $info['constants']);
262
                    }
263
                } else {
264
                    if ($o & 8) {
265
                        $data[] = implode("\r\n", $info['tokens']);
266
                    }
267
                }
268
 
269
                $table->addRow($data);
270
            }
271
        } else {
272
            foreach ($info as $file => $info) {
273
                if (is_numeric($file{0})) {
274
                    // extra information available only when debug mode is on
275
                    $all_functions[$file] = $info;
276
                }
277
            }
278
        }
279
 
280
        $output = $table->getTable();
281
 
282
        // verbose level
283
        $v = isset($this->args['verbose']) ? $this->args['verbose'] : 0;
284
 
285
        // command line resume
286
        if ($v & 1) {
287
            $output .= "\nCommand Line resume :\n\n";
288
 
289
            $table = new Console_Table();
290
            $table->setHeaders(array('Option', 'Value'));
291
 
292
            $filter0 = array(&$this, '_splitOption');
293
            $table->addFilter(0, $filter0);
294
            $filter1 = array(&$this, '_splitValue');
295
            $table->addFilter(1, $filter1);
296
 
297
            if (is_array($this->args)) {
298
                foreach ($this->args as $key => $raw) {
299
                    if ($key == 'summarize') {
300
                        $raw = ($raw === true) ? 'TRUE' : 'FALSE';
301
                    }
302
                    if (is_array($raw)) {
303
                        $raw = implode(', ', $raw);
304
                    }
305
                    $contents = array($key, $raw);
306
                    $table->addRow($contents);
307
                }
308
            }
309
 
310
            $output .= $table->getTable();
311
        }
312
 
313
        // parser options resume
314
        if ($v & 2) {
315
            $output .= "\nParser options :\n\n";
316
 
317
            $table = new Console_Table();
318
            $table->setHeaders(array('Option', 'Value'));
319
 
320
            $filter0 = array(&$this, '_splitOption');
321
            $table->addFilter(0, $filter0);
322
            $filter1 = array(&$this, '_splitValue');
323
            $table->addFilter(1, $filter1);
324
 
325
            $opts = $this->_parser->options;
326
            if (is_array($opts)) {
327
                foreach ($opts as $key => $raw) {
328
                    if ($key == 'debug'
329
                        || $key == 'recurse_dir'
330
                        || $key == 'is_string') {
331
                        $raw = ($raw === true) ? 'TRUE' : 'FALSE';
332
                    }
333
                    if (substr($key, -6) == '_match') {
334
                        $val = array_values($raw[1]);
335
                        array_unshift($val, $raw[0]);
336
                        $raw = implode("\r\n", $val);
337
                    } else {
338
                        if (is_array($raw)) {
339
                            $raw = implode("\r\n", $raw);
340
                        }
341
                    }
342
                    $contents = array($key, $raw);
343
                    $table->addRow($contents);
344
                }
345
            }
346
 
347
            $output .= $table->getTable();
348
        }
349
 
350
        // extra information
351
        if ($v & 4) {
352
            $output .= "\nDebug:\n\n";
353
 
354
            $table = new Console_Table();
355
            $table->setHeaders(array('Version', 'Function', 'Extension', 'PECL'));
356
 
357
            foreach ($all_functions as $version => $functions) {
358
                foreach ($functions as $func) {
359
                    $table->addRow(array($version,
360
                        $func['function'], $func['extension'],
361
                        (isset($func['pecl']) ?
362
                        (($func['pecl'] === true) ? 'yes' : 'no') : '')));
363
                }
364
            }
365
 
366
            $output .= $table->getTable();
367
        }
368
        echo $output;
369
    }
370
 
371
    /**
372
     * The Console_Table filter callback limits table output to 80 columns,
373
     * and Path column to 29 characters
374
     * (27 + 1 blank margin left + 1 blank margin right).
375
     *
376
     * @param string $data Content of filename column (0)
377
     *
378
     * @return string
379
     * @access private
380
     * @since  1.3.0
381
     */
382
    function _splitFilename($data)
383
    {
384
        $w = $this->conf['colwidth']['f'] - 2;
385
 
386
        if (strlen($data) <= $w) {
387
            $str = str_pad($data, $w);
388
        } else {
389
            $str = '...' . substr($data, (strlen($data) - ($w - 3)));
390
        }
391
        return $str;
392
    }
393
 
394
    /**
395
     * The Console_Table filter callback limits table output to 80 columns,
396
     * and Extensions column to 12 characters
397
     * (10 + 1 blank margin left + 1 blank margin right).
398
     *
399
     * @param string $data Content of extensions column
400
     *
401
     * @return string
402
     * @access private
403
     * @since  1.7.0
404
     */
405
    function _splitExtname($data)
406
    {
407
        $szlim  = $this->conf['colwidth']['e'] - 2;
408
        $extArr = explode("\r\n", $data);
409
        $str    = '';
410
        foreach ($extArr as $ext) {
411
            if (strlen($ext) <= $szlim) {
412
                $str .= str_pad($ext, $szlim);
413
            } else {
414
                $str .= '...' . substr($ext, (strlen($ext) - ($szlim - 3)));
415
            }
416
            $str .= "\r\n";
417
        }
418
        $str = rtrim($str, "\r\n");
419
        return $str;
420
    }
421
 
422
    /**
423
     * The Console_Table filter callback limits table output to 80 columns,
424
     * and Constants/Tokens column to 20 characters
425
     * (18 + 1 blank margin left + 1 blank margin right)
426
     *
427
     * @param string $data Content of constants/tokens column
428
     *
429
     * @return string
430
     * @access private
431
     * @since  1.7.0
432
     */
433
    function _splitConstant($data)
434
    {
435
        $szlim  = $this->conf['colwidth']['c'] - 2;
436
        $cstArr = explode("\r\n", $data);
437
        $str    = '';
438
        foreach ($cstArr as $cst) {
439
            if (strlen($cst) <= $szlim) {
440
                $str .= str_pad($cst, $szlim);
441
            } else {
442
                $str .= '...' . substr($cst, (strlen($cst) - ($szlim - 3)));
443
            }
444
            $str .= "\r\n";
445
        }
446
        $str = rtrim($str, "\r\n");
447
        return $str;
448
    }
449
 
450
    /**
451
     * The Console_Table filter callback limits table output to 80 columns,
452
     * and Command line Option column to 25 characters
453
     * (23 + 1 blank margin left + 1 blank margin right).
454
     *
455
     * @param string $data Content of option column (0)
456
     *
457
     * @return string
458
     * @access private
459
     * @since  1.7.0
460
     */
461
    function _splitOption($data)
462
    {
463
        if (strlen($data) <= 23) {
464
            $str = str_pad($data, 23);
465
        } else {
466
            $str = '...' . substr($data, (strlen($data) - 20));
467
        }
468
        return $str;
469
    }
470
 
471
    /**
472
     * The Console_Table filter callback limits table output to 80 columns,
473
     * and Command line Value column to 51 characters
474
     * (49 + 1 blank margin left + 1 blank margin right)
475
     *
476
     * @param string $data Content of value column (1)
477
     *
478
     * @return string
479
     * @access private
480
     * @since  1.7.0
481
     */
482
    function _splitValue($data)
483
    {
484
        $cstArr = explode("\r\n", $data);
485
        $str    = '';
486
        foreach ($cstArr as $cst) {
487
            if (strlen($cst) <= 49) {
488
                $str .= str_pad($cst, 49);
489
            } else {
490
                $str .= '...' . substr($cst, (strlen($cst) - 46));
491
            }
492
            $str .= "\r\n";
493
        }
494
        $str = rtrim($str, "\r\n");
495
        return $str;
496
    }
497
}
498
?>