TYPO3  7.6
DebugUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
17 
22 {
31  public static function debug($var = '', $header = '', $group = 'Debug')
32  {
33  // buffer the output of debug if no buffering started before
34  if (ob_get_level() == 0) {
35  ob_start();
36  }
37  $debug = self::convertVariableToString($var);
38  if (TYPO3_MODE === 'BE' && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
39  $tabHeader = $header ?: 'Debug';
40  $script = '
41  (function debug() {
42  var message = ' . GeneralUtility::quoteJSvalue($debug) . ',
43  header = ' . GeneralUtility::quoteJSvalue($header) . ',
44  group = ' . GeneralUtility::quoteJSvalue($group) . ';
45  if (top.TYPO3.DebugConsole) {
46  top.TYPO3.DebugConsole.add(message, header, group);
47  } else {
48  var consoleMessage = [group, header, message].join(" | ");
49  if (typeof console === "object" && typeof console.log === "function") {
50  console.log(consoleMessage);
51  }
52  };
53  })();
54  ';
55  echo GeneralUtility::wrapJS($script);
56  } else {
57  echo $debug;
58  }
59  }
60 
67  public static function convertVariableToString($variable)
68  {
69  if (is_array($variable)) {
70  $string = self::viewArray($variable);
71  } elseif (is_object($variable)) {
72  $string = json_encode($variable, true);
73  } elseif ((string)$variable !== '') {
74  $string = htmlspecialchars((string)$variable);
75  } else {
76  $string = '| debug |';
77  }
78  return $string;
79  }
80 
88  public static function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug')
89  {
90  $debugString = self::convertVariableToString($debugVariable);
91  $script = '
92  (function debug() {
93  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
94  header = ' . GeneralUtility::quoteJSvalue($header) . ',
95  group = ' . GeneralUtility::quoteJSvalue($group) . ',
96 
97  browserWindow = function(debug, header, group) {
98  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
99  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
100  );
101  if (newWindow.document.body.innerHTML) {
102  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
103  "<hr />" + debugMessage;
104  } else {
105  newWindow.document.writeln(
106  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
107  + "<body onload=\\"self.focus()\\">"
108  + debugMessage
109  + "</body></html>"
110  );
111  }
112  };
113 
114  if (top && typeof top.TYPO3 !== "undefined" && typeof top.TYPO3.Modal !== "undefined") {
115  top.TYPO3.Modal.show(
116  "Debug: " + header + " (" + group + ")",
117  debugMessage,
118  top.TYPO3.Severity.notice
119  );
120  } else {
121  browserWindow(debugMessage, header, group);
122  }
123  })();
124  ';
125  echo GeneralUtility::wrapJS($script);
126  }
127 
133  public static function debugTrail()
134  {
135  $trail = debug_backtrace();
136  $trail = array_reverse($trail);
137  array_pop($trail);
138  $path = array();
139  foreach ($trail as $dat) {
140  $pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
141  // add the path of the included file
142  if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
143  $pathFragment .= '(' . PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . PathUtility::stripPathSitePrefix($dat['file']);
144  }
145  $path[] = $pathFragment . '#' . $dat['line'];
146  }
147  return implode(' // ', $path);
148  }
149 
158  public static function debugRows($rows, $header = '', $returnHTML = false)
159  {
160  if ($returnHTML !== false) {
161  GeneralUtility::deprecationLog('Setting the parameter $returnHTML is deprecated since TYPO3 CMS 7 and will be removed in TYPO3 CMS 8.');
162  }
163  self::debug('<pre>' . DebuggerUtility::var_dump($rows, $header, 8, true, false, true), $header . '</pre>');
164  }
165 
173  public static function ordinalValue($string, $characters = 100)
174  {
175  if (strlen($string) < $characters) {
176  $characters = strlen($string);
177  }
178  $valuestring = '';
179  for ($i = 0; $i < $characters; $i++) {
180  $valuestring .= ' ' . ord(substr($string, $i, 1));
181  }
182  return trim($valuestring);
183  }
184 
193  public static function viewArray($array_in)
194  {
195  return '<pre>' . DebuggerUtility::var_dump($array_in, '', 8, true, false, true) . '</pre>';
196  }
197 
205  public static function printArray($array_in)
206  {
207  echo self::viewArray($array_in);
208  }
209 }