‪TYPO3CMS  11.5
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 
18 use Psr\Http\Message\ServerRequestInterface;
22 
27 {
31  protected static ‪$plainTextOutput = true;
32 
36  protected static ‪$ansiColorUsage = true;
37 
47  public static function ‪debug($var = '', $header = 'Debug', $group = 'Debug')
48  {
49  // buffer the output of debug if no buffering started before
50  if (ob_get_level() === 0) {
51  ob_start();
52  }
53 
55  && (‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
56  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isBackend()
57  ) {
58  $debug = ‪self::renderDump($var);
59  $debugPlain = PHP_EOL . ‪self::renderDump($var, '', true, false);
60  $script = '
61  (function debug() {
62  var message = ' . GeneralUtility::quoteJSvalue($debug) . ',
63  messagePlain = ' . GeneralUtility::quoteJSvalue($debugPlain) . ',
64  header = ' . GeneralUtility::quoteJSvalue($header) . ',
65  group = ' . GeneralUtility::quoteJSvalue($group) . ';
66  if (top.TYPO3 && top.TYPO3.DebugConsole) {
67  top.TYPO3.DebugConsole.add(message, header, group);
68  } else {
69  var consoleMessage = [group, header, messagePlain].join(" | ");
70  if (typeof console === "object" && typeof console.log === "function") {
71  console.log(consoleMessage);
72  }
73  };
74  })();
75  ';
76  echo GeneralUtility::wrapJS($script);
77  } else {
78  echo ‪self::renderDump($var, $header);
79  }
80  }
81 
88  public static function ‪convertVariableToString($variable)
89  {
90  $string = ‪self::renderDump($variable, '', true, false);
91  return $string === '' ? '| debug |' : $string;
92  }
93 
101  public static function ‪debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug')
102  {
103  $debugString = ‪self::renderDump($debugVariable, sprintf('%s (%s)', $header, $group));
104  $script = '
105  (function debug() {
106  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
107  header = ' . GeneralUtility::quoteJSvalue($header) . ',
108  group = ' . GeneralUtility::quoteJSvalue($group) . ',
109 
110  browserWindow = function(debug, header, group) {
111  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
112  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
113  );
114  if (newWindow.document.body.innerHTML) {
115  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
116  "<hr />" + debugMessage;
117  } else {
118  newWindow.document.writeln(
119  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
120  + "<body onload=\\"self.focus()\\">"
121  + debugMessage
122  + "</body></html>"
123  );
124  }
125  };
126 
127  if (top && typeof top.TYPO3 !== "undefined" && typeof top.TYPO3.Modal !== "undefined") {
128  top.TYPO3.Modal.show(
129  "Debug: " + header + " (" + group + ")",
130  debugMessage,
131  top.TYPO3.Severity.notice
132  );
133  } else {
134  browserWindow(debugMessage, header, group);
135  }
136  })();
137  ';
138  echo GeneralUtility::wrapJS($script);
139  }
140 
147  public static function ‪debugTrail($prependFileNames = false)
148  {
149  $trail = debug_backtrace(0);
150  $trail = array_reverse($trail);
151  array_pop($trail);
152  $path = [];
153  foreach ($trail as $dat) {
154  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
155  $pathFragment = $fileInformation . ($dat['class'] ?? '') . ($dat['type'] ?? '') . $dat['function'];
156  // add the path of the included file
157  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
158  $pathFragment .= '(' . ‪PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . ‪PathUtility::stripPathSitePrefix($dat['file']);
159  }
160  if (array_key_exists('line', $dat)) {
161  $path[] = $pathFragment . '#' . $dat['line'];
162  } else {
163  $path[] = $pathFragment;
164  }
165  }
166  return implode(' // ', $path);
167  }
168 
175  public static function ‪debugRows($rows, $header = '')
176  {
177  ‪self::debug($rows, $header);
178  }
179 
187  public static function ‪ordinalValue($string, $characters = 100)
188  {
189  if (strlen($string) < $characters) {
190  $characters = strlen($string);
191  }
192  $valuestring = '';
193  for ($i = 0; $i < $characters; $i++) {
194  $valuestring .= ' ' . ord(substr($string, $i, 1));
195  }
196  return trim($valuestring);
197  }
198 
207  public static function ‪viewArray($array_in)
208  {
209  return ‪self::renderDump($array_in);
210  }
211 
218  public static function ‪printArray($array_in)
219  {
220  echo ‪self::renderDump($array_in);
221  }
222 
232  protected static function ‪renderDump($variable, $title = '', $plainText = null, $ansiColors = null)
233  {
234  $plainText = $plainText ?? ‪Environment::isCli() && ‪self::$plainTextOutput;
235  $ansiColors = $ansiColors ?? ‪Environment::isCli() && ‪self::$ansiColorUsage;
236  return trim(‪DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
237  }
238 
249  public static function ‪usePlainTextOutput(‪$plainTextOutput)
250  {
251  static::$plainTextOutput = ‪$plainTextOutput;
252  }
253 
264  public static function ‪useAnsiColor(‪$ansiColorUsage)
265  {
266  static::$ansiColorUsage = ‪$ansiColorUsage;
267  }
268 }
‪TYPO3\CMS\Core\Http\ApplicationType\fromRequest
‪static static fromRequest(ServerRequestInterface $request)
Definition: ApplicationType.php:62
‪TYPO3\CMS\Core\Utility\DebugUtility\debug
‪static debug($var='', $header='Debug', $group='Debug')
Definition: DebugUtility.php:45
‪TYPO3\CMS\Core\Utility\DebugUtility\renderDump
‪static string renderDump($variable, $title='', $plainText=null, $ansiColors=null)
Definition: DebugUtility.php:230
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:445
‪TYPO3\CMS\Core\Utility\DebugUtility\usePlainTextOutput
‪static usePlainTextOutput($plainTextOutput)
Definition: DebugUtility.php:247
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:16
‪TYPO3\CMS\Core\Utility\DebugUtility\printArray
‪static printArray($array_in)
Definition: DebugUtility.php:216
‪TYPO3\CMS\Core\Http\ApplicationType
Definition: ApplicationType.php:52
‪TYPO3\CMS\Core\Utility\DebugUtility\debugInPopUpWindow
‪static debugInPopUpWindow($debugVariable, $header='Debug', $group='Debug')
Definition: DebugUtility.php:99
‪TYPO3\CMS\Core\Utility\DebugUtility\debugRows
‪static debugRows($rows, $header='')
Definition: DebugUtility.php:173
‪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:532
‪TYPO3\CMS\Core\Utility\DebugUtility\debugTrail
‪static string debugTrail($prependFileNames=false)
Definition: DebugUtility.php:145
‪TYPO3\CMS\Core\Utility\DebugUtility\viewArray
‪static string viewArray($array_in)
Definition: DebugUtility.php:205
‪TYPO3\CMS\Extbase\Utility\DebuggerUtility
Definition: DebuggerUtility.php:41
‪TYPO3\CMS\Core\Utility\DebugUtility
Definition: DebugUtility.php:27
‪TYPO3\CMS\Core\Utility\DebugUtility\useAnsiColor
‪static useAnsiColor($ansiColorUsage)
Definition: DebugUtility.php:262
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Core\Utility\DebugUtility\$ansiColorUsage
‪static bool $ansiColorUsage
Definition: DebugUtility.php:34
‪TYPO3\CMS\Core\Utility\DebugUtility\ordinalValue
‪static string ordinalValue($string, $characters=100)
Definition: DebugUtility.php:185
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static bool isCli()
Definition: Environment.php:162
‪TYPO3\CMS\Core\Utility\DebugUtility\convertVariableToString
‪static string convertVariableToString($variable)
Definition: DebugUtility.php:86
‪TYPO3\CMS\Core\Utility\DebugUtility\$plainTextOutput
‪static bool $plainTextOutput
Definition: DebugUtility.php:30