[TASK] Streamline usage of DebugConsole
[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 class DebugUtility
22 {
23 /**
24 * Debug
25 *
26 * @param string $var
27 * @param string $header
28 * @param string $group
29 * @return void
30 */
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
61 /**
62 * Converts a variable to a string
63 *
64 * @param mixed $variable
65 * @return string
66 */
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
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 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
128 /**
129 * Displays the "path" of the function call stack in a string, using debug_backtrace
130 *
131 * @return string
132 */
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
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 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
166 /**
167 * Returns a string with a list of ascii-values for the first $characters characters in $string
168 *
169 * @param string $string String to show ASCII value for
170 * @param int $characters Number of characters to show
171 * @return string The string with ASCII values in separated by a space char.
172 */
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
185 /**
186 * Returns HTML-code, which is a visual representation of a multidimensional array
187 * use \TYPO3\CMS\Core\Utility\GeneralUtility::print_array() in order to print an array
188 * Returns FALSE if $array_in is not an array
189 *
190 * @param mixed $array_in Array to view
191 * @return string HTML output
192 */
193 public static function viewArray($array_in)
194 {
195 return '<pre>' . DebuggerUtility::var_dump($array_in, '', 8, true, false, true) . '</pre>';
196 }
197
198 /**
199 * Prints an array
200 *
201 * @param mixed $array_in Array to print visually (in a table).
202 * @return void
203 * @see viewArray()
204 */
205 public static function printArray($array_in)
206 {
207 echo self::viewArray($array_in);
208 }
209 }