‪TYPO3CMS  9.5
DebugUtility.php
Go to the documentation of this file.
1 <?php
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 
19 
24 {
28  protected static ‪$plainTextOutput = true;
29 
33  protected static ‪$ansiColorUsage = true;
34 
44  public static function ‪debug($var = '', $header = 'Debug', $group = 'Debug')
45  {
46  // buffer the output of debug if no buffering started before
47  if (ob_get_level() === 0) {
48  ob_start();
49  }
50 
51  if (TYPO3_MODE === 'BE' && !‪Environment::isCli()) {
52  $debug = ‪self::renderDump($var);
53  $debugPlain = PHP_EOL . ‪self::renderDump($var, '', true, false);
54  $script = '
55  (function debug() {
56  var message = ' . GeneralUtility::quoteJSvalue($debug) . ',
57  messagePlain = ' . GeneralUtility::quoteJSvalue($debugPlain) . ',
58  header = ' . GeneralUtility::quoteJSvalue($header) . ',
59  group = ' . GeneralUtility::quoteJSvalue($group) . ';
60  if (top.TYPO3 && top.TYPO3.DebugConsole) {
61  top.TYPO3.DebugConsole.add(message, header, group);
62  } else {
63  var consoleMessage = [group, header, messagePlain].join(" | ");
64  if (typeof console === "object" && typeof console.log === "function") {
65  console.log(consoleMessage);
66  }
67  };
68  })();
69  ';
70  echo GeneralUtility::wrapJS($script);
71  } else {
72  echo ‪self::renderDump($var, $header);
73  }
74  }
75 
82  public static function ‪convertVariableToString($variable)
83  {
84  $string = ‪self::renderDump($variable, '', true, false);
85  return $string === '' ? '| debug |' : $string;
86  }
87 
95  public static function ‪debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug')
96  {
97  $debugString = ‪self::renderDump($debugVariable, sprintf('%s (%s)', $header, $group));
98  $script = '
99  (function debug() {
100  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
101  header = ' . GeneralUtility::quoteJSvalue($header) . ',
102  group = ' . GeneralUtility::quoteJSvalue($group) . ',
103 
104  browserWindow = function(debug, header, group) {
105  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
106  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
107  );
108  if (newWindow.document.body.innerHTML) {
109  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
110  "<hr />" + debugMessage;
111  } else {
112  newWindow.document.writeln(
113  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
114  + "<body onload=\\"self.focus()\\">"
115  + debugMessage
116  + "</body></html>"
117  );
118  }
119  };
120 
121  if (top && typeof top.TYPO3 !== "undefined" && typeof top.TYPO3.Modal !== "undefined") {
122  top.TYPO3.Modal.show(
123  "Debug: " + header + " (" + group + ")",
124  debugMessage,
125  top.TYPO3.Severity.notice
126  );
127  } else {
128  browserWindow(debugMessage, header, group);
129  }
130  })();
131  ';
132  echo GeneralUtility::wrapJS($script);
133  }
134 
141  public static function ‪debugTrail($prependFileNames = false)
142  {
143  $trail = debug_backtrace(0);
144  $trail = array_reverse($trail);
145  array_pop($trail);
146  $path = [];
147  foreach ($trail as $dat) {
148  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
149  $pathFragment = $fileInformation . $dat['class'] . $dat['type'] . $dat['function'];
150  // add the path of the included file
151  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
152  $pathFragment .= '(' . ‪PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . ‪PathUtility::stripPathSitePrefix($dat['file']);
153  }
154  $path[] = $pathFragment . '#' . $dat['line'];
155  }
156  return implode(' // ', $path);
157  }
158 
165  public static function ‪debugRows($rows, $header = '')
166  {
167  ‪self::debug($rows, $header);
168  }
169 
177  public static function ‪ordinalValue($string, $characters = 100)
178  {
179  if (strlen($string) < $characters) {
180  $characters = strlen($string);
181  }
182  $valuestring = '';
183  for ($i = 0; $i < $characters; $i++) {
184  $valuestring .= ' ' . ord(substr($string, $i, 1));
185  }
186  return trim($valuestring);
187  }
188 
197  public static function ‪viewArray($array_in)
198  {
199  return ‪self::renderDump($array_in);
200  }
201 
208  public static function ‪printArray($array_in)
209  {
210  echo ‪self::renderDump($array_in);
211  }
212 
222  protected static function ‪renderDump($variable, $title = '', $plainText = null, $ansiColors = null)
223  {
224  $plainText = $plainText ?? ‪Environment::isCli() && ‪self::$plainTextOutput;
225  $ansiColors = $ansiColors ?? ‪Environment::isCli() && ‪self::$ansiColorUsage;
226  return trim(‪DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
227  }
228 
239  public static function ‪usePlainTextOutput(‪$plainTextOutput)
240  {
241  static::$plainTextOutput = ‪$plainTextOutput;
242  }
243 
254  public static function ‪useAnsiColor(‪$ansiColorUsage)
255  {
256  static::$ansiColorUsage = ‪$ansiColorUsage;
257  }
258 }
‪TYPO3\CMS\Core\Utility\DebugUtility\debug
‪static debug($var='', $header='Debug', $group='Debug')
Definition: DebugUtility.php:42
‪TYPO3\CMS\Core\Utility\DebugUtility\renderDump
‪static string renderDump($variable, $title='', $plainText=null, $ansiColors=null)
Definition: DebugUtility.php:220
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:371
‪TYPO3\CMS\Core\Utility\DebugUtility\usePlainTextOutput
‪static usePlainTextOutput($plainTextOutput)
Definition: DebugUtility.php:237
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:2
‪TYPO3\CMS\Core\Utility\DebugUtility\printArray
‪static printArray($array_in)
Definition: DebugUtility.php:206
‪TYPO3\CMS\Core\Utility\DebugUtility\debugInPopUpWindow
‪static debugInPopUpWindow($debugVariable, $header='Debug', $group='Debug')
Definition: DebugUtility.php:93
‪TYPO3\CMS\Core\Utility\DebugUtility\debugRows
‪static debugRows($rows, $header='')
Definition: DebugUtility.php:163
‪TYPO3\CMS\Core\Utility\DebugUtility\debugTrail
‪static string debugTrail($prependFileNames=false)
Definition: DebugUtility.php:139
‪TYPO3\CMS\Core\Utility\DebugUtility\viewArray
‪static string viewArray($array_in)
Definition: DebugUtility.php:195
‪TYPO3\CMS\Extbase\Utility\DebuggerUtility
Definition: DebuggerUtility.php:24
‪TYPO3\CMS\Core\Utility\DebugUtility
Definition: DebugUtility.php:24
‪TYPO3\CMS\Core\Utility\DebugUtility\useAnsiColor
‪static useAnsiColor($ansiColorUsage)
Definition: DebugUtility.php:252
‪TYPO3\CMS\Extbase\Utility\DebuggerUtility\var_dump
‪static string var_dump($variable, $title=null, $maxDepth=8, $plainText=false, $ansiColors=true, $return=false, $blacklistedClassNames=null, $blacklistedPropertyNames=null)
Definition: DebuggerUtility.php:496
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Utility\DebugUtility\$ansiColorUsage
‪static bool $ansiColorUsage
Definition: DebugUtility.php:31
‪TYPO3\CMS\Core\Utility\DebugUtility\ordinalValue
‪static string ordinalValue($string, $characters=100)
Definition: DebugUtility.php:175
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:127
‪TYPO3\CMS\Core\Utility\DebugUtility\convertVariableToString
‪static string convertVariableToString($variable)
Definition: DebugUtility.php:80
‪TYPO3\CMS\Core\Utility\DebugUtility\$plainTextOutput
‪static bool $plainTextOutput
Definition: DebugUtility.php:27