1 | <?php |
||
12 | class Text_Diff_Renderer { |
||
|
|||
13 | |||
14 | /** |
||
15 | * Number of leading context "lines" to preserve. |
||
16 | * |
||
17 | * This should be left at zero for this class, but subclasses may want to |
||
18 | * set this to other values. |
||
19 | */ |
||
20 | var $_leading_context_lines = 0; |
||
21 | |||
22 | /** |
||
23 | * Number of trailing context "lines" to preserve. |
||
24 | * |
||
25 | * This should be left at zero for this class, but subclasses may want to |
||
26 | * set this to other values. |
||
27 | */ |
||
28 | var $_trailing_context_lines = 0; |
||
29 | |||
30 | /** |
||
31 | * Constructor. |
||
32 | */ |
||
33 | function __construct($params = array()) |
||
34 | { |
||
35 | foreach ($params as $param => $value) { |
||
36 | $v = '_' . $param; |
||
37 | if (isset($this->$v)) { |
||
38 | $this->$v = $value; |
||
39 | } |
||
40 | } |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Get any renderer parameters. |
||
45 | * |
||
46 | * @return array All parameters of this renderer object. |
||
47 | */ |
||
48 | function getParams() |
||
59 | |||
60 | /** |
||
61 | * Renders a diff. |
||
62 | * |
||
63 | * @param Text_Diff $diff A Text_Diff object. |
||
64 | * |
||
65 | * @return string The formatted output. |
||
66 | */ |
||
67 | function render($diff) |
||
68 | { |
||
69 | $xi = $yi = 1; |
||
70 | $block = false; |
||
71 | $context = array(); |
||
72 | |||
73 | $nlead = $this->_leading_context_lines; |
||
74 | $ntrail = $this->_trailing_context_lines; |
||
75 | |||
76 | $output = $this->_startDiff(); |
||
77 | |||
78 | foreach ($diff->getDiff() as $edit) { |
||
79 | if (is_a($edit, 'Text_Diff_Op_copy')) { |
||
80 | if (is_array($block)) { |
||
81 | if (count($edit->orig) <= $nlead + $ntrail) { |
||
82 | $block[] = $edit; |
||
83 | } else { |
||
84 | if ($ntrail) { |
||
85 | $context = array_slice($edit->orig, 0, $ntrail); |
||
86 | $block[] = new Text_Diff_Op_copy($context); |
||
87 | } |
||
88 | $output .= $this->_block($x0, $ntrail + $xi - $x0, |
||
89 | $y0, $ntrail + $yi - $y0, |
||
90 | $block); |
||
91 | $block = false; |
||
92 | } |
||
93 | } |
||
94 | $context = $edit->orig; |
||
95 | } else { |
||
96 | if (!is_array($block)) { |
||
97 | $context = array_slice($context, count($context) - $nlead); |
||
98 | $x0 = $xi - count($context); |
||
99 | $y0 = $yi - count($context); |
||
100 | $block = array(); |
||
101 | if ($context) { |
||
102 | $block[] = new Text_Diff_Op_copy($context); |
||
103 | } |
||
104 | } |
||
105 | $block[] = $edit; |
||
106 | } |
||
107 | |||
108 | if ($edit->orig) { |
||
109 | $xi += count($edit->orig); |
||
110 | } |
||
111 | if ($edit->final) { |
||
112 | $yi += count($edit->final); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if (is_array($block)) { |
||
117 | $output .= $this->_block($x0, $xi - $x0, |
||
118 | $y0, $yi - $y0, |
||
119 | $block); |
||
120 | } |
||
121 | |||
122 | return $output . $this->_endDiff(); |
||
123 | } |
||
124 | |||
125 | function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) |
||
151 | |||
152 | function _startDiff() |
||
156 | |||
157 | function _endDiff() |
||
161 | |||
162 | function _blockHeader($xbeg, $xlen, $ybeg, $ylen) |
||
173 | |||
174 | function _startBlock($header) |
||
178 | |||
179 | function _endBlock() |
||
183 | |||
184 | function _lines($lines, $prefix = ' ') |
||
188 | |||
189 | function _context($lines) |
||
193 | |||
194 | function _added($lines) |
||
198 | |||
199 | function _deleted($lines) |
||
203 | |||
204 | function _changed($orig, $final) |
||
208 | |||
209 | } |
||
210 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.