‪TYPO3CMS  ‪main
DebugUtility.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
28 {
29  protected static bool ‪$plainTextOutput = true;
30 
31  protected static bool ‪$ansiColorUsage = true;
32 
38  public static function ‪debug(mixed $var = '', string $header = 'Debug'): void
39  {
40  // buffer the output of debug if no buffering started before
41  if (ob_get_level() === 0) {
42  ob_start();
43  }
44 
45  echo ‪self::renderDump($var, $header);
46  }
47 
53  public static function ‪convertVariableToString(mixed $variable): string
54  {
55  $string = ‪self::renderDump($variable, '', true, false);
56  return $string === '' ? '| debug |' : $string;
57  }
58 
62  public static function ‪debugInPopUpWindow(mixed $debugVariable, string $header = 'Debug', string $group = 'Debug'): void
63  {
64  $debugString = ‪self::renderDump($debugVariable, sprintf('%s (%s)', $header, $group));
65  $script = '
66  (function debug() {
67  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
68  header = ' . GeneralUtility::quoteJSvalue($header) . ',
69  group = ' . GeneralUtility::quoteJSvalue($group) . ',
70 
71  browserWindow = function(debug, header, group) {
72  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
73  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
74  );
75  if (newWindow.document.body.innerHTML) {
76  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
77  "<hr />" + debugMessage;
78  } else {
79  newWindow.document.writeln(
80  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
81  + "<body onload=\\"self.focus()\\">"
82  + debugMessage
83  + "</body></html>"
84  );
85  }
86  };
87 
88  if (top && typeof top.TYPO3 !== "undefined" && typeof top.TYPO3.Modal !== "undefined") {
89  top.TYPO3.Modal.show(
90  "Debug: " + header + " (" + group + ")",
91  debugMessage,
92  top.TYPO3.Severity.notice
93  );
94  } else {
95  browserWindow(debugMessage, header, group);
96  }
97  })();
98  ';
99  echo GeneralUtility::wrapJS($script, ['nonce' => self::resolveNonceValue()]);
100  }
101 
108  public static function ‪debugTrail(bool $prependFileNames = false): string
109  {
110  $trail = debug_backtrace(0);
111  $trail = array_reverse($trail);
112  array_pop($trail);
113  $path = [];
114  foreach ($trail as $dat) {
115  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
116  $pathFragment = $fileInformation . ($dat['class'] ?? '') . ($dat['type'] ?? '') . $dat['function'];
117  // add the path of the included file
118  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
119  $pathFragment .= '(' . ‪PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . ‪PathUtility::stripPathSitePrefix($dat['file']);
120  }
121  if (array_key_exists('line', $dat)) {
122  $path[] = $pathFragment . '#' . $dat['line'];
123  } else {
124  $path[] = $pathFragment;
125  }
126  }
127  return implode(' // ', $path);
128  }
129 
136  public static function ‪debugRows(array $rows, string $header = ''): void
137  {
138  ‪self::debug($rows, $header);
139  }
140 
148  public static function ‪ordinalValue(string $string, int $characters = 100): string
149  {
150  if (strlen($string) < $characters) {
151  $characters = strlen($string);
152  }
153  $valuestring = '';
154  for ($i = 0; $i < $characters; $i++) {
155  $valuestring .= ' ' . ord($string[$i]);
156  }
157  return trim($valuestring);
158  }
159 
168  public static function ‪viewArray(mixed $array_in): string
169  {
170  return ‪self::renderDump($array_in);
171  }
172 
179  public static function ‪printArray(mixed $array_in): void
180  {
181  echo ‪self::renderDump($array_in);
182  }
183 
190  protected static function ‪renderDump(mixed $variable, string $title = '', ?bool $plainText = null, ?bool $ansiColors = null): string
191  {
192  $plainText = $plainText ?? ‪Environment::isCli() && ‪self::$plainTextOutput;
193  $ansiColors = $ansiColors ?? ‪Environment::isCli() && ‪self::$ansiColorUsage;
194  return trim(‪DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
195  }
196 
206  public static function ‪usePlainTextOutput(bool ‪$plainTextOutput): void
207  {
208  static::$plainTextOutput = ‪$plainTextOutput;
209  }
210 
220  public static function ‪useAnsiColor(bool ‪$ansiColorUsage): void
221  {
222  static::$ansiColorUsage = ‪$ansiColorUsage;
223  }
224 
225  protected static function ‪resolveNonceValue(): string
226  {
227  return GeneralUtility::makeInstance(RequestId::class)->nonce->b64;
228  }
229 }
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static stripPathSitePrefix(string $path)
Definition: PathUtility.php:428
‪TYPO3\CMS\Core\Utility\DebugUtility\debugRows
‪static debugRows(array $rows, string $header='')
Definition: DebugUtility.php:136
‪TYPO3\CMS\Core\Utility\DebugUtility\resolveNonceValue
‪static resolveNonceValue()
Definition: DebugUtility.php:225
‪TYPO3\CMS\Core\Utility\DebugUtility\usePlainTextOutput
‪static usePlainTextOutput(bool $plainTextOutput)
Definition: DebugUtility.php:206
‪TYPO3\CMS\Core\Utility\DebugUtility\debugTrail
‪static string debugTrail(bool $prependFileNames=false)
Definition: DebugUtility.php:108
‪TYPO3\CMS\Core\Utility\DebugUtility\debugInPopUpWindow
‪static debugInPopUpWindow(mixed $debugVariable, string $header='Debug', string $group='Debug')
Definition: DebugUtility.php:62
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\DebugUtility\viewArray
‪static string viewArray(mixed $array_in)
Definition: DebugUtility.php:168
‪TYPO3\CMS\Core\Utility\DebugUtility\useAnsiColor
‪static useAnsiColor(bool $ansiColorUsage)
Definition: DebugUtility.php:220
‪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:490
‪TYPO3\CMS\Core\Utility\DebugUtility\debug
‪static debug(mixed $var='', string $header='Debug')
Definition: DebugUtility.php:38
‪TYPO3\CMS\Core\Core\RequestId
Definition: RequestId.php:26
‪TYPO3\CMS\Extbase\Utility\DebuggerUtility
Definition: DebuggerUtility.php:41
‪TYPO3\CMS\Core\Utility\DebugUtility\renderDump
‪static renderDump(mixed $variable, string $title='', ?bool $plainText=null, ?bool $ansiColors=null)
Definition: DebugUtility.php:190
‪TYPO3\CMS\Core\Core\Environment\isCli
‪static isCli()
Definition: Environment.php:145
‪TYPO3\CMS\Core\Utility\DebugUtility\ordinalValue
‪static string ordinalValue(string $string, int $characters=100)
Definition: DebugUtility.php:148
‪TYPO3\CMS\Core\Utility\DebugUtility
Definition: DebugUtility.php:28
‪TYPO3\CMS\Core\Utility\DebugUtility\printArray
‪static printArray(mixed $array_in)
Definition: DebugUtility.php:179
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\DebugUtility\$ansiColorUsage
‪static bool $ansiColorUsage
Definition: DebugUtility.php:31
‪TYPO3\CMS\Core\Utility\DebugUtility\$plainTextOutput
‪static bool $plainTextOutput
Definition: DebugUtility.php:29
‪TYPO3\CMS\Core\Utility\DebugUtility\convertVariableToString
‪static string convertVariableToString(mixed $variable)
Definition: DebugUtility.php:53