‪TYPO3CMS  10.4
DebugUtility.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
25 {
29  protected static ‪$plainTextOutput = true;
30 
34  protected static ‪$ansiColorUsage = true;
35 
45  public static function ‪debug($var = '', $header = 'Debug', $group = 'Debug')
46  {
47  // buffer the output of debug if no buffering started before
48  if (ob_get_level() === 0) {
49  ob_start();
50  }
51 
52  if (TYPO3_MODE === 'BE' && !‪Environment::isCli()) {
53  $debug = ‪self::renderDump($var);
54  $debugPlain = PHP_EOL . ‪self::renderDump($var, '', true, false);
55  $script = '
56  (function debug() {
57  var message = ' . GeneralUtility::quoteJSvalue($debug) . ',
58  messagePlain = ' . GeneralUtility::quoteJSvalue($debugPlain) . ',
59  header = ' . GeneralUtility::quoteJSvalue($header) . ',
60  group = ' . GeneralUtility::quoteJSvalue($group) . ';
61  if (top.TYPO3 && top.TYPO3.DebugConsole) {
62  top.TYPO3.DebugConsole.add(message, header, group);
63  } else {
64  var consoleMessage = [group, header, messagePlain].join(" | ");
65  if (typeof console === "object" && typeof console.log === "function") {
66  console.log(consoleMessage);
67  }
68  };
69  })();
70  ';
71  echo GeneralUtility::wrapJS($script);
72  } else {
73  echo ‪self::renderDump($var, $header);
74  }
75  }
76 
83  public static function ‪convertVariableToString($variable)
84  {
85  $string = ‪self::renderDump($variable, '', true, false);
86  return $string === '' ? '| debug |' : $string;
87  }
88 
96  public static function ‪debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug')
97  {
98  $debugString = ‪self::renderDump($debugVariable, sprintf('%s (%s)', $header, $group));
99  $script = '
100  (function debug() {
101  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
102  header = ' . GeneralUtility::quoteJSvalue($header) . ',
103  group = ' . GeneralUtility::quoteJSvalue($group) . ',
104 
105  browserWindow = function(debug, header, group) {
106  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
107  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
108  );
109  if (newWindow.document.body.innerHTML) {
110  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
111  "<hr />" + debugMessage;
112  } else {
113  newWindow.document.writeln(
114  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
115  + "<body onload=\\"self.focus()\\">"
116  + debugMessage
117  + "</body></html>"
118  );
119  }
120  };
121 
122  if (top && typeof top.TYPO3 !== "undefined" && typeof top.TYPO3.Modal !== "undefined") {
123  top.TYPO3.Modal.show(
124  "Debug: " + header + " (" + group + ")",
125  debugMessage,
126  top.TYPO3.Severity.notice
127  );
128  } else {
129  browserWindow(debugMessage, header, group);
130  }
131  })();
132  ';
133  echo GeneralUtility::wrapJS($script);
134  }
135 
142  public static function ‪debugTrail($prependFileNames = false)
143  {
144  $trail = debug_backtrace(0);
145  $trail = array_reverse($trail);
146  array_pop($trail);
147  $path = [];
148  foreach ($trail as $dat) {
149  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
150  $pathFragment = $fileInformation . $dat['class'] . $dat['type'] . $dat['function'];
151  // add the path of the included file
152  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
153  $pathFragment .= '(' . ‪PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . ‪PathUtility::stripPathSitePrefix($dat['file']);
154  }
155  $path[] = $pathFragment . '#' . $dat['line'];
156  }
157  return implode(' // ', $path);
158  }
159 
166  public static function ‪debugRows($rows, $header = '')
167  {
168  ‪self::debug($rows, $header);
169  }
170 
178  public static function ‪ordinalValue($string, $characters = 100)
179  {
180  if (strlen($string) < $characters) {
181  $characters = strlen($string);
182  }
183  $valuestring = '';
184  for ($i = 0; $i < $characters; $i++) {
185  $valuestring .= ' ' . ord(substr($string, $i, 1));
186  }
187  return trim($valuestring);
188  }
189 
198  public static function ‪viewArray($array_in)
199  {
200  return ‪self::renderDump($array_in);
201  }
202 
209  public static function ‪printArray($array_in)
210  {
211  echo ‪self::renderDump($array_in);
212  }
213 
223  protected static function ‪renderDump($variable, $title = '', $plainText = null, $ansiColors = null)
224  {
225  $plainText = $plainText ?? ‪Environment::isCli() && ‪self::$plainTextOutput;
226  $ansiColors = $ansiColors ?? ‪Environment::isCli() && ‪self::$ansiColorUsage;
227  return trim(‪DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
228  }
229 
240  public static function ‪usePlainTextOutput(‪$plainTextOutput)
241  {
242  static::$plainTextOutput = ‪$plainTextOutput;
243  }
244 
255  public static function ‪useAnsiColor(‪$ansiColorUsage)
256  {
257  static::$ansiColorUsage = ‪$ansiColorUsage;
258  }
259 }
‪TYPO3\CMS\Core\Utility\DebugUtility\debug
‪static debug($var='', $header='Debug', $group='Debug')
Definition: DebugUtility.php:43
‪TYPO3\CMS\Core\Utility\DebugUtility\renderDump
‪static string renderDump($variable, $title='', $plainText=null, $ansiColors=null)
Definition: DebugUtility.php:221
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:372
‪TYPO3\CMS\Core\Utility\DebugUtility\usePlainTextOutput
‪static usePlainTextOutput($plainTextOutput)
Definition: DebugUtility.php:238
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\DebugUtility\printArray
‪static printArray($array_in)
Definition: DebugUtility.php:207
‪TYPO3\CMS\Core\Utility\DebugUtility\debugInPopUpWindow
‪static debugInPopUpWindow($debugVariable, $header='Debug', $group='Debug')
Definition: DebugUtility.php:94
‪TYPO3\CMS\Core\Utility\DebugUtility\debugRows
‪static debugRows($rows, $header='')
Definition: DebugUtility.php:164
‪TYPO3\CMS\Extbase\Utility\DebuggerUtility\var_dump
‪static string var_dump( $variable, string $title=null, int $maxDepth=8, bool $plainText=false, bool $ansiColors=true, bool $return=false, array $blacklistedClassNames=null, array $blacklistedPropertyNames=null)
Definition: DebuggerUtility.php:518
‪TYPO3\CMS\Core\Utility\DebugUtility\debugTrail
‪static string debugTrail($prependFileNames=false)
Definition: DebugUtility.php:140
‪TYPO3\CMS\Core\Utility\DebugUtility\viewArray
‪static string viewArray($array_in)
Definition: DebugUtility.php:196
‪TYPO3\CMS\Extbase\Utility\DebuggerUtility
Definition: DebuggerUtility.php:41
‪TYPO3\CMS\Core\Utility\DebugUtility
Definition: DebugUtility.php:25
‪TYPO3\CMS\Core\Utility\DebugUtility\useAnsiColor
‪static useAnsiColor($ansiColorUsage)
Definition: DebugUtility.php:253
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\DebugUtility\$ansiColorUsage
‪static bool $ansiColorUsage
Definition: DebugUtility.php:32
‪TYPO3\CMS\Core\Utility\DebugUtility\ordinalValue
‪static string ordinalValue($string, $characters=100)
Definition: DebugUtility.php:176
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:154
‪TYPO3\CMS\Core\Utility\DebugUtility\convertVariableToString
‪static string convertVariableToString($variable)
Definition: DebugUtility.php:81
‪TYPO3\CMS\Core\Utility\DebugUtility\$plainTextOutput
‪static bool $plainTextOutput
Definition: DebugUtility.php:28