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: Html.php,v 1.14 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.0b4
41
 */
42
 
43
require_once 'HTML/Table.php';
44
require_once 'HTML/CSS.php';
45
 
46
/**
47
 * Html renderer for PHP_CompatInfo component.
48
 *
49
 * The PHP_CompatInfo_Renderer_Html class is a concrete implementation
50
 * of PHP_CompatInfo_Renderer abstract class. It simply display results
51
 * as web/html content with help of PEAR::Html_Table
52
 *
53
 * @category PHP
54
 * @package  PHP_CompatInfo
55
 * @author   Laurent Laville <pear@laurent-laville.org>
56
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
57
 * @version  Release: 1.9.0
58
 * @link     http://pear.php.net/package/PHP_CompatInfo
59
 * @since    Class available since Release 1.8.0b4
60
 */
61
class PHP_CompatInfo_Renderer_Html extends PHP_CompatInfo_Renderer
62
{
63
    /**
64
     * Style sheet for the custom layout
65
     *
66
     * @var    string
67
     * @access public
68
     * @since  1.8.0b4
69
     */
70
    var $css;
71
 
72
    /**
73
     * Html Renderer Class constructor (ZE1) for PHP4
74
     *
75
     * @param object &$parser Instance of the parser (model of MVC pattern)
76
     * @param array  $conf    A hash containing any additional configuration
77
     *
78
     * @access public
79
     * @since  version 1.8.0b4 (2008-06-18)
80
     */
81
    function PHP_CompatInfo_Renderer_Html(&$parser, $conf)
82
    {
83
        $this->__construct($parser, $conf);
84
    }
85
 
86
    /**
87
     * Html Renderer Class constructor (ZE2) for PHP5+
88
     *
89
     * @param object &$parser Instance of the parser (model of MVC pattern)
90
     * @param array  $conf    A hash containing any additional configuration
91
     *
92
     * @access public
93
     * @since  version 1.8.0b4 (2008-06-18)
94
     */
95
    function __construct(&$parser, $conf)
96
    {
97
        $defaults = array('tdwidth' => array(18, 4, 2, 7, 13));
98
        $conf     = array_merge($defaults, $conf);
99
 
100
        parent::PHP_CompatInfo_Renderer($parser, $conf);
101
 
102
    }
103
 
104
    /**
105
     * Display final results
106
     *
107
     * Display final results, when data source parsing is over.
108
     *
109
     * @access public
110
     * @return void
111
     * @since  version 1.8.0b4 (2008-06-18)
112
     */
113
    function display()
114
    {
115
        $o    = $this->args['output-level'];
116
        $info = $this->parseData;
117
 
118
        if ($info == false) {
119
            // protect against invalid data source
120
            print 'Invalid data source';
121
            return;
122
        }
123
 
124
        $src = $this->_parser->dataSource;
125
        if ($src['dataType'] == 'directory') {
126
            $dir      = $src['dataSource'];
127
            $hdr_col1 = 'Directory';
128
        } elseif ($src['dataType'] == 'file') {
129
            $file     = $src['dataSource'];
130
            $hdr_col1 = 'File';
131
        } else {
132
            $string   = $src['dataSource'];
133
            $hdr_col1 = 'Source code';
134
        }
135
 
136
        $dataTable = new HTML_Table();
137
        $thead     =& $dataTable->getHeader();
138
        $tbody     =& $dataTable->getBody();
139
        $tfoot     =& $dataTable->getFooter();
140
 
141
        $hdr = array($hdr_col1);
142
        $atr = array('scope="col"');
143
        if ($o & 16) {
144
            $hdr[] = 'Version';
145
            $atr[] = 'scope="col"';
146
        }
147
        if ($o & 1) {
148
            $hdr[] = 'C';
149
            $atr[] = 'scope="col"';
150
        }
151
        if ($o & 2) {
152
            $hdr[] = 'Extensions';
153
            $atr[] = 'scope="col"';
154
        }
155
        if ($o & 4) {
156
            if ($o & 8) {
157
                $hdr[] = 'Constants/Tokens';
158
                $atr[] = 'scope="col"';
159
            } else {
160
                $hdr[] = 'Constants';
161
                $atr[] = 'scope="col"';
162
            }
163
        } else {
164
            if ($o & 8) {
165
                $hdr[] = 'Tokens';
166
                $atr[] = 'scope="col"';
167
            }
168
        }
169
 
170
        $thead->addRow($hdr, $atr);
171
 
172
        $ext   = implode("<br/>", $info['extensions']);
173
        $const = implode("<br/>", array_merge($info['constants'], $info['tokens']));
174
        if (isset($dir)) {
175
            $ds    = DIRECTORY_SEPARATOR;
176
            $dir   = str_replace(array('\\', '/'), $ds, $dir);
177
            $title = $src['dataCount'] . ' file';
178
            if ($src['dataCount'] > 1) {
179
                $title .= 's'; // plural
180
            }
181
        } elseif (isset($file)) {
182
            $title = '1 file';
183
        } else {
184
            $title = '1 chunk of code';
185
        }
186
        $data = array('Summary: '. $title . ' parsed');
187
 
188
        if ($o & 16) {
189
            if (empty($info['max_version'])) {
190
                $data[] = $info['version'];
191
            } else {
192
                $data[] = implode("<br/>", array($info['version'],
193
                                                $info['max_version']));
194
            }
195
        }
196
        if ($o & 1) {
197
            $data[] = $info['cond_code'][0];
198
        }
199
        if ($o & 2) {
200
            $data[] = $ext;
201
        }
202
        if ($o & 4) {
203
            if ($o & 8) {
204
                $data[] = $const;
205
            } else {
206
                $data[] = implode("<br/>", $info['constants']);
207
            }
208
        } else {
209
            if ($o & 8) {
210
                $data[] = implode("<br/>", $info['tokens']);
211
            }
212
        }
213
 
214
        // summary informations
215
        $tfoot->addRow($data);
216
 
217
        // summarize : print only summary for directory without files details
218
        if ($this->args['summarize'] === false && isset($dir)) {
219
            // display result of parsing multiple files
220
 
221
            unset($info['max_version']);
222
            unset($info['version']);
223
            unset($info['classes']);
224
            unset($info['functions']);
225
            unset($info['extensions']);
226
            unset($info['constants']);
227
            unset($info['tokens']);
228
            unset($info['cond_code']);
229
 
230
            $ignored = $info['ignored_files'];
231
 
232
            unset($info['ignored_files']);
233
            unset($info['ignored_functions']);
234
            unset($info['ignored_extensions']);
235
            unset($info['ignored_constants']);
236
 
237
            foreach ($info as $file => $info) {
238
                if ($info === false) {
239
                    continue;  // skip this (invalid) file
240
                }
241
                $ext   = implode("<br/>", $info['extensions']);
242
                $const = implode("<br/>", array_merge($info['constants'],
243
                                                      $info['tokens']));
244
 
245
                $file = str_replace(array('\\', '/'), $ds, $file);
246
 
247
                $path = dirname($file);
248
                $tbody->addRow(array($path), array('class' => 'dirname',
249
                                                   'colspan' => count($hdr)));
250
 
251
                $data = array(basename($file));
252
                if ($o & 16) {
253
                    if (empty($info['max_version'])) {
254
                        $data[] = $info['version'];
255
                    } else {
256
                        $data[] = implode("<br/>", array($info['version'],
257
                                                         $info['max_version']));
258
                    }
259
                }
260
                if ($o & 1) {
261
                    $data[] = $info['cond_code'][0];
262
                }
263
                if ($o & 2) {
264
                    $data[] = $ext;
265
                }
266
                if ($o & 4) {
267
                    if ($o & 8) {
268
                        $data[] = $const;
269
                    } else {
270
                        $data[] = implode("<br/>", $info['constants']);
271
                    }
272
                } else {
273
                    if ($o & 8) {
274
                        $data[] = implode("<br/>", $info['tokens']);
275
                    }
276
                }
277
 
278
                $tbody->addRow($data);
279
            }
280
        } elseif ($this->args['summarize'] === false && !isset($dir)) {
281
            // display result of parsing a single file, or a chunk of code
282
            if (isset($file)) {
283
                $path = dirname($file);
284
            } else {
285
                $path = '.';
286
            }
287
            $tbody->addRow(array($path), array('class' => 'dirname',
288
                                               'colspan' => count($hdr)));
289
            if (isset($file)) {
290
                $data[0] = basename($file);
291
            } else {
292
                $data[0] = htmlspecialchars('<?php ... ?>');
293
            }
294
            $tbody->addRow($data);
295
        } else {
296
            // display only result summary of parsing a data source
297
            if (isset($dir)) {
298
                $path = dirname($dir[0]);
299
            } elseif (isset($file)) {
300
                $path = dirname($file);
301
            } else {
302
                $path = '.';
303
            }
304
            $tbody->addRow(array($path), array('class' => 'dirname',
305
                                               'colspan' => count($hdr)));
306
        }
307
 
308
        $evenRow = array('class' => 'even');
309
        $oddRow  = null;
310
        $tbody->altRowAttributes(1, $evenRow, $oddRow, true);
311
 
312
        echo $this->toHtml($dataTable);
313
    }
314
 
315
    /**
316
     * Returns the custom style sheet
317
     *
318
     * Returns the custom style sheet to use for layout
319
     *
320
     * @param int   $destination (optional) Destination of css content
321
     * @param mixed $extra       (optional) Additional data depending of destination
322
     *
323
     * @return mixed
324
     * @access public
325
     * @since  version 1.8.0b4 (2008-06-18)
326
     */
327
    function getStyleSheet($destination = 1, $extra = null)
328
    {
329
        $css = new HTML_CSS();
330
        $css->parseFile($this->css);
331
 
332
        $tdw = $this->conf['tdwidth'];
333
        $em  = array_sum($tdw);
334
        $td  = 'td';
335
        $o   = $this->args['output-level'];
336
 
337
        $css->setStyle('.outer td.dirname', 'width', $em.'em');
338
        if ($o & 16) {
339
            $td .= '+td';
340
            $css->setStyle('.outer '.$td, 'width', $tdw[1].'em');
341
            $em = $em - $tdw[1];
342
        }
343
        if ($o & 1) {
344
            $td .= '+td';
345
            $css->setStyle('.outer '.$td, 'width', $tdw[2].'em');
346
            $em = $em - $tdw[2];
347
        }
348
        if ($o & 2) {
349
            $td .= '+td';
350
            $css->setStyle('.outer '.$td, 'width', $tdw[3].'em');
351
            $em = $em - $tdw[3];
352
        }
353
        if ($o & 12) {
354
            $td .= '+td';
355
            $css->setStyle('.outer '.$td, 'width', $tdw[4].'em');
356
            $em = $em - $tdw[4];
357
        }
358
        $css->setStyle('.outer td', 'width', $em .'em');
359
 
360
        $styles = '';
361
 
362
        switch ($destination) {
363
        case 1:  // embedded styles
364
            $styles = $css->toString();
365
            break;
366
        case 2:  // save only to file
367
            $css->toFile($extra);
368
            $styles = $extra;
369
            break;
370
        case 3:  // apply a user function
371
            if (is_callable($extra)) {
372
                $styles = call_user_func_array($extra, array($css));
373
            }
374
            break;
375
        default:
376
            break;
377
        }
378
        return $styles;
379
    }
380
 
381
    /**
382
     * Set a custom style sheet
383
     *
384
     * Set a custom style sheet to use your own styles
385
     *
386
     * @param string $css (optional) File to read user-defined styles from
387
     *
388
     * @return bool    True if custom styles, false if default styles applied
389
     * @access public
390
     * @since  version 1.8.0b4 (2008-06-18)
391
     */
392
    function setStyleSheet($css = null)
393
    {
394
        // default stylesheet is into package data directory
395
        if (!isset($css)) {
396
            $css = '/var/www/pear' . DIRECTORY_SEPARATOR
397
                 . 'PHP_CompatInfo' . DIRECTORY_SEPARATOR
398
                 . 'pci.css';
399
        }
400
 
401
        $res = isset($css) && file_exists($css);
402
        if ($res) {
403
            $this->css = $css;
404
        }
405
        return $res;
406
    }
407
 
408
    /**
409
     * Returns HTML code
410
     *
411
     * Returns HTML code of parsing result
412
     *
413
     * @param object $obj instance of HTML_Table
414
     *
415
     * @access public
416
     * @return string
417
     * @since  version 1.8.0b4 (2008-06-18)
418
     */
419
    function toHtml($obj)
420
    {
421
        if (!isset($this->css)) {
422
            // when no user-styles defined, used the default values
423
            $this->setStyleSheet();
424
        }
425
        $styles = $this->getStyleSheet();
426
 
427
        $body = $obj->toHtml();
428
 
429
        $html = <<<HTML
430
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
431
    "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
432
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
433
<head>
434
<title>PHP_CompatInfo</title>
435
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
436
<style type="text/css">
437
<!--
438
$styles
439
 -->
440
</style>
441
</head>
442
<body>
443
<div class="outer">
444
<div class="inner">
445
$body
446
</div>
447
</div>
448
</body>
449
</html>
450
HTML;
451
        return $html;
452
    }
453
}
454
?>