1 <?php
2 /**
3 * Plain Text debugger
4 *
5 * This class provides developer-friendly HTML output of strings for debugging.
6 * Specifically, it highlights the specified line of the text and can display
7 * the surrounding text using an nominal number of lines for padding. This
8 * functionality can be used anywhere a parse failure accurs and there is a line
9 * number involved.
10 *
11 * @author Terence Kearns
12 * @version 0.2
13 * @copyright Terence Kearns 2003
14 * @license LGPL
15 * @package XAO
16 * @link http://xao-php.sourceforge.net
17 */
18 class TextDebugger {
19
20 var $strHtml;
21
22 var $arrText;
23
24 var $intLine;
25
26 var $intPadding = 10;
27
28 var $strHighlightStyle =
29 "color: red; font-weight: bold; background: yellow;";
30
31 var $strTextStyle =
32 "color: black; font-weight: normal; background: #E0E0E0;";
33
34 var $strBorderStyle =
35 "border: solid red 1px; padding: 10px; font-size: 8pt;";
36
37 function TextDebugger($strText,$intLine = -1) {
38 if($intLine == -1) $this->intPadding = 0;
39 if(file_exists($strText)) {
40 $this->arrText = file($strText);
41 }
42 else {
43 $this->arrText = explode("\n",$strText);
44 }
45 $this->intLine = $intLine;
46 }
47
48 function strGetHtml() {
49
50 if(!strlen($this->strHtml)) {
51 $intPadding = $this->intPadding;
52 $intHilightLine = $this->intLine;
53
54 $strOut = "<pre style=\"".$this->strBorderStyle."\">Debug dump: ";
55
56 if($intPadding < 0)
57 $intPadding *= -1; // deal with negative numbers
58 if($intPadding != 0)
59 $strOut .=
60 "printing ".$intPadding
61 ." lines either side of highlight.\n";
62
63 $intCurrLine = 1;
64 foreach($this->arrText AS $strLine) {
65 if(
66 (
67 $intHilightLine > -1
68 && (
69 ($intHilightLine - $intCurrLine) <= $intPadding
70 && ($intCurrLine - $intHilightLine) <= $intPadding
71 )
72 )
73 || $intPadding == 0
74 ) {
75 if($intCurrLine == $intHilightLine) {
76 $strOut .=
77 "<div style=\"".$this->strHighlightStyle."\">";
78 }
79 else {
80 $strOut .= "<div style=\"".$this->strTextStyle."\">";
81 }
82 $strOut .=
83 sprintf(
84 "<b>%5d</b> %s",
85 $intCurrLine,
86 htmlentities($strLine)
87 );
88 $strOut .= "</div>";
89 if($intCurrLine == count($this->arrText)) {
90 $strOut .= "End of File!\n";
91 }
92 }
93 $intCurrLine++;
94 }
95 $strOut .= "</pre>\n";
96 $this->strHtml = $strOut;
97 }
98
99 return $this->strHtml;
100 }
101 }
102
103 ?>