TYPO3 CMS  TYPO3_6-2
DebugUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
21 class DebugUtility {
22 
29  <table class="typo3-debug" border="0" cellpadding="0" cellspacing="0" bgcolor="white" style="border:0px; margin-top:3px; margin-bottom:3px;">
30  <tr>
31  <td style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;">%s</td>
32  </tr>
33  <tr>
34  <td>
35  %s
36  </td>
37  </tr>
38  </table>
39  ';
40 
49  static public function debug($var = '', $header = '', $group = 'Debug') {
50  // buffer the output of debug if no buffering started before
51  if (ob_get_level() == 0) {
52  ob_start();
53  }
54  $debug = self::convertVariableToString($var);
55  if ($header) {
56  $debug = sprintf(self::DEBUG_TABLE_TEMPLATE, htmlspecialchars((string) $header), $debug);
57  }
58  if (TYPO3_MODE === 'BE' && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
59  $tabHeader = $header ?: 'Debug';
60  $script = '
61  (function debug() {
62  var debugMessage = ' . GeneralUtility::quoteJSvalue($debug) . ';
63  var header = ' . GeneralUtility::quoteJSvalue($tabHeader) . ';
64  var group = ' . GeneralUtility::quoteJSvalue($group) . ';
65 
66  if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
67  document.write(debugMessage);
68  return;
69  }
70 
71  if (top && typeof Ext !== "object") {
72  Ext = top.Ext;
73  }
74 
75  Ext.onReady(function() {
76  var TYPO3ViewportInstance = null;
77 
78  if (top && top.TYPO3 && typeof top.TYPO3.Backend === "object") {
79  TYPO3ViewportInstance = top.TYPO3.Backend;
80  } else if (typeof TYPO3 === "object" && typeof TYPO3.Backend === "object") {
81  TYPO3ViewportInstance = TYPO3.Backend;
82  }
83 
84  if (TYPO3ViewportInstance !== null) {
85  TYPO3ViewportInstance.DebugConsole.addTab(debugMessage, header, group);
86  } else {
87  document.write(debugMessage);
88  }
89  });
90  })();
91  ';
92  echo \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($script);
93  } else {
94  echo $debug;
95  }
96  }
97 
104  static public function convertVariableToString($variable) {
105  if (is_array($variable)) {
106  $string = self::viewArray($variable);
107  } elseif (is_object($variable)) {
108  $string = '<strong>|Object:<pre>';
109  $string .= print_r($variable, TRUE);
110  $string .= '</pre>|</strong>';
111  } elseif ((string) $variable !== '') {
112  $string = '<strong>|' . htmlspecialchars((string) $variable) . '|</strong>';
113  } else {
114  $string = '<strong>| debug |</strong>';
115  }
116  return $string;
117  }
118 
126  static public function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
127  $debugString = self::convertVariableToString($debugVariable);
128  $script = '
129  (function debug() {
130  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
131  header = ' . GeneralUtility::quoteJSvalue($header) . ',
132  group = ' . GeneralUtility::quoteJSvalue($group) . ',
133 
134  browserWindow = function(debug, header, group) {
135  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
136  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
137  );
138  if (newWindow.document.body.innerHTML) {
139  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
140  "<hr />" + debugMessage;
141  } else {
142  newWindow.document.writeln(
143  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
144  + "<body onload=\\"self.focus()\\">"
145  + debugMessage
146  + "</body></html>"
147  );
148  }
149  }
150 
151  if (!top.Ext) {
152  browserWindow(debugMessage, header, group);
153  } else {
154  top.Ext.onReady(function() {
155  if (top && top.TYPO3 && top.TYPO3.Backend) {
156  top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
157  } else {
158  browserWindow(debugMessage, header, group);
159  }
160  });
161  }
162  })();
163  ';
164  echo \TYPO3\CMS\Core\Utility\GeneralUtility::wrapJS($script);
165  }
166 
172  static public function debugTrail() {
173  $trail = debug_backtrace();
174  $trail = array_reverse($trail);
175  array_pop($trail);
176  $path = array();
177  foreach ($trail as $dat) {
178  $pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
179  // add the path of the included file
180  if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
182  }
183  $path[] = $pathFragment . '#' . $dat['line'];
184  }
185  return implode(' // ', $path);
186  }
187 
196  static public function debugRows($rows, $header = '', $returnHTML = FALSE) {
197  if (is_array($rows)) {
198  $firstEl = reset($rows);
199  if (is_array($firstEl)) {
200  $headerColumns = array_keys($firstEl);
201  $tRows = array();
202  // Header:
203  $tRows[] = '<tr><td colspan="' . count($headerColumns) . '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' . htmlspecialchars($header) . '</strong></td></tr>';
204  $tCells = array();
205  foreach ($headerColumns as $key) {
206  $tCells[] = '
207  <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
208  }
209  $tRows[] = '
210  <tr>' . implode('', $tCells) . '
211  </tr>';
212  // Rows:
213  foreach ($rows as $singleRow) {
214  $tCells = array();
215  foreach ($headerColumns as $key) {
216  $tCells[] = '
217  <td><font face="Verdana,Arial" size="1">' . (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) . '</font></td>';
218  }
219  $tRows[] = '
220  <tr>' . implode('', $tCells) . '
221  </tr>';
222  }
223  $table = '
224  <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
225  </table>';
226  if ($returnHTML) {
227  return $table;
228  } else {
229  echo $table;
230  }
231  } else {
232  debug('Empty array of rows', $header);
233  }
234  } else {
235  debug('No array of rows', $header);
236  }
237  }
238 
246  static public function ordinalValue($string, $characters = 100) {
247  if (strlen($string) < $characters) {
248  $characters = strlen($string);
249  }
250  $valuestring = '';
251  for ($i = 0; $i < $characters; $i++) {
252  $valuestring .= ' ' . ord(substr($string, $i, 1));
253  }
254  return trim($valuestring);
255  }
256 
265  static public function viewArray($array_in) {
266  if (is_array($array_in)) {
267  $result = '
268  <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
269  if (count($array_in) == 0) {
270  $result .= '<tr><td><font face="Verdana,Arial" size="1"><strong>EMPTY!</strong></font></td></tr>';
271  } else {
272  foreach ($array_in as $key => $val) {
273  $result .= '<tr>
274  <td valign="top"><font face="Verdana,Arial" size="1">' . htmlspecialchars((string) $key) . '</font></td>
275  <td>';
276  if (is_array($val)) {
277  $result .= self::viewArray($val);
278  } elseif (is_object($val)) {
279  $string = '';
280  if (method_exists($val, '__toString')) {
281  $string .= get_class($val) . ': ' . (string) $val;
282  } else {
283  $string .= print_r($val, TRUE);
284  }
285  $result .= '<font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars($string)) . '<br /></font>';
286  } else {
287  if (gettype($val) == 'object') {
288  $string = 'Unknown object';
289  } else {
290  $string = (string) $val;
291  }
292  $result .= '<font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars($string)) . '<br /></font>';
293  }
294  $result .= '</td>
295  </tr>';
296  }
297  }
298  $result .= '</table>';
299  } else {
300  $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
301  <tr>
302  <td><font face="Verdana,Arial" size="1" color="red">' . nl2br(htmlspecialchars((string) $array_in)) . '<br /></font></td>
303  </tr>
304  </table>';
305  }
306  // Output it as a string.
307  return $result;
308  }
309 
317  static public function printArray($array_in) {
318  echo self::viewArray($array_in);
319  }
320 
321 }
static debug($var='', $header='', $group='Debug')
const TYPO3_MODE
Definition: init.php:40
static ordinalValue($string, $characters=100)
static convertVariableToString($variable)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
static debugRows($rows, $header='', $returnHTML=FALSE)
static debugInPopUpWindow($debugVariable, $header='Debug', $group='Debug')