[TASK] Enable output of file names in DebugUtility::debugTrail
[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 * @param bool $prependFileNames If set to true file names are added to the output
132 * @return string
133 */
134 public static function debugTrail($prependFileNames = false)
135 {
136 $trail = debug_backtrace(0);
137 $trail = array_reverse($trail);
138 array_pop($trail);
139 $path = array();
140 foreach ($trail as $dat) {
141 $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
142 $pathFragment = $fileInformation . $dat['class'] . $dat['type'] . $dat['function'];
143 // add the path of the included file
144 if (in_array($dat['function'], array('require', 'include', 'require_once', 'include_once'))) {
145 $pathFragment .= '(' . PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . PathUtility::stripPathSitePrefix($dat['file']);
146 }
147 $path[] = $pathFragment . '#' . $dat['line'];
148 }
149 return implode(' // ', $path);
150 }
151
152 /**
153 * Displays an array as rows in a table. Useful to debug output like an array of database records.
154 *
155 * @param mixed $rows Array of arrays with similar keys
156 * @param string $header Table header
157 * @return void Outputs to browser.
158 */
159 public static function debugRows($rows, $header = '')
160 {
161 self::debug('<pre>' . DebuggerUtility::var_dump($rows, $header, 8, true, false, true), $header . '</pre>');
162 }
163
164 /**
165 * Returns a string with a list of ascii-values for the first $characters characters in $string
166 *
167 * @param string $string String to show ASCII value for
168 * @param int $characters Number of characters to show
169 * @return string The string with ASCII values in separated by a space char.
170 */
171 public static function ordinalValue($string, $characters = 100)
172 {
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 public static function viewArray($array_in)
192 {
193 return '<pre>' . DebuggerUtility::var_dump($array_in, '', 8, true, false, true) . '</pre>';
194 }
195
196 /**
197 * Prints an array
198 *
199 * @param mixed $array_in Array to print visually (in a table).
200 * @return void
201 * @see viewArray()
202 */
203 public static function printArray($array_in)
204 {
205 echo self::viewArray($array_in);
206 }
207 }