[FOLLOWUP][TASK] Refactor debugger console
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / DebugUtility.php
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 */
16 use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
17
18 /**
19 * Class to handle debug
20 *
21 * @author Steffen Kamper <steffen@typo3.org>
22 */
23 class DebugUtility {
24
25 /**
26 * Debug
27 *
28 * @param string $var
29 * @param string $header
30 * @param string $group
31 * @return void
32 */
33 static public function debug($var = '', $header = '', $group = 'Debug') {
34 // buffer the output of debug if no buffering started before
35 if (ob_get_level() == 0) {
36 ob_start();
37 }
38 $debug = self::convertVariableToString($var);
39 if (TYPO3_MODE === 'BE' && !(TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI)) {
40 $tabHeader = $header ?: 'Debug';
41 $script = '
42 (function debug() {
43 var message = ' . GeneralUtility::quoteJSvalue($debug) . ',
44 header = ' . GeneralUtility::quoteJSvalue($header) . ',
45 group = ' . GeneralUtility::quoteJSvalue($group) . ';
46 if (top.TYPO3.DebugConsole) {
47 top.TYPO3.DebugConsole.add(message, header, group);
48 } else {
49 var consoleMessage = [group, header, message].join(" | ");
50 if (typeof console === "object" && typeof console.log === "function") {
51 console.log(consoleMessage);
52 }
53 };
54 })();
55 ';
56 echo GeneralUtility::wrapJS($script);
57 } else {
58 echo $debug;
59 }
60 }
61
62 /**
63 * Converts a variable to a string
64 *
65 * @param mixed $variable
66 * @return string
67 */
68 static public function convertVariableToString($variable) {
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
81 /**
82 * Opens a debug message inside a popup window
83 *
84 * @param mixed $debugVariable
85 * @param string $header
86 * @param string $group
87 */
88 static public function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
89 $debugString = self::convertVariableToString($debugVariable);
90 $script = '
91 (function debug() {
92 var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
93 header = ' . GeneralUtility::quoteJSvalue($header) . ',
94 group = ' . GeneralUtility::quoteJSvalue($group) . ',
95
96 browserWindow = function(debug, header, group) {
97 var newWindow = window.open("", "TYPO3DebugWindow_" + group,
98 "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
99 );
100 if (newWindow.document.body.innerHTML) {
101 newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
102 "<hr />" + debugMessage;
103 } else {
104 newWindow.document.writeln(
105 "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
106 + "<body onload=\\"self.focus()\\">"
107 + debugMessage
108 + "</body></html>"
109 );
110 }
111 }
112
113 if (!top.Ext) {
114 browserWindow(debugMessage, header, group);
115 } else {
116 top.Ext.onReady(function() {
117 if (top && top.TYPO3 && top.TYPO3.Backend) {
118 top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
119 } else {
120 browserWindow(debugMessage, header, group);
121 }
122 });
123 }
124 })();
125 ';
126 echo GeneralUtility::wrapJS($script);
127 }
128
129 /**
130 * Displays the "path" of the function call stack in a string, using debug_backtrace
131 *
132 * @return string
133 */
134 static public function debugTrail() {
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
150 /**
151 * Displays an array as rows in a table. Useful to debug output like an array of database records.
152 *
153 * @param mixed $rows Array of arrays with similar keys
154 * @param string $header Table header
155 * @param bool $returnHTML If TRUE, will return content instead of echo'ing out. Deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8
156 * @return void Outputs to browser.
157 */
158 static public function debugRows($rows, $header = '', $returnHTML = FALSE) {
159 if ($returnHTML !== FALSE) {
160 GeneralUtility::deprecationLog('Setting the parameter $returnHTML is deprecated since TYPO3 CMS 7 and will be removed in TYPO3 CMS 8.');
161 }
162 self::debug('<pre>' . DebuggerUtility::var_dump($rows, $header, 8, TRUE, FALSE, TRUE), $header . '</pre>');
163 }
164
165 /**
166 * Returns a string with a list of ascii-values for the first $characters characters in $string
167 *
168 * @param string $string String to show ASCII value for
169 * @param int $characters Number of characters to show
170 * @return string The string with ASCII values in separated by a space char.
171 */
172 static public function ordinalValue($string, $characters = 100) {
173 if (strlen($string) < $characters) {
174 $characters = strlen($string);
175 }
176 $valuestring = '';
177 for ($i = 0; $i < $characters; $i++) {
178 $valuestring .= ' ' . ord(substr($string, $i, 1));
179 }
180 return trim($valuestring);
181 }
182
183 /**
184 * Returns HTML-code, which is a visual representation of a multidimensional array
185 * use \TYPO3\CMS\Core\Utility\GeneralUtility::print_array() in order to print an array
186 * Returns FALSE if $array_in is not an array
187 *
188 * @param mixed $array_in Array to view
189 * @return string HTML output
190 */
191 static public function viewArray($array_in) {
192 return '<pre>' . DebuggerUtility::var_dump($array_in, '', 8, TRUE, FALSE, TRUE) . '</pre>';
193 }
194
195 /**
196 * Prints an array
197 *
198 * @param mixed $array_in Array to print visually (in a table).
199 * @return void
200 * @see viewArray()
201 */
202 static public function printArray($array_in) {
203 echo self::viewArray($array_in);
204 }
205
206 }