‪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 
65  public static function ‪debugTrail(bool $prependFileNames = false): string
66  {
67  $trail = debug_backtrace(0);
68  $trail = array_reverse($trail);
69  array_pop($trail);
70  $path = [];
71  foreach ($trail as $dat) {
72  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
73  $pathFragment = $fileInformation . ($dat['class'] ?? '') . ($dat['type'] ?? '') . $dat['function'];
74  // add the path of the included file
75  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
76  $pathFragment .= '(' . ‪PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . ‪PathUtility::stripPathSitePrefix($dat['file']);
77  }
78  if (array_key_exists('line', $dat)) {
79  $path[] = $pathFragment . '#' . $dat['line'];
80  } else {
81  $path[] = $pathFragment;
82  }
83  }
84  return implode(' // ', $path);
85  }
86 
94  public static function ‪ordinalValue(string $string, int $characters = 100): string
95  {
96  if (strlen($string) < $characters) {
97  $characters = strlen($string);
98  }
99  $valuestring = '';
100  for ($i = 0; $i < $characters; $i++) {
101  $valuestring .= ' ' . ord($string[$i]);
102  }
103  return trim($valuestring);
104  }
105 
114  public static function ‪viewArray(mixed $array_in): string
115  {
116  return ‪self::renderDump($array_in);
117  }
118 
125  protected static function ‪renderDump(mixed $variable, string $title = '', ?bool $plainText = null, ?bool $ansiColors = null): string
126  {
127  $plainText = $plainText ?? ‪Environment::isCli() && ‪self::$plainTextOutput;
128  $ansiColors = $ansiColors ?? ‪Environment::isCli() && ‪self::$ansiColorUsage;
129  return trim(‪DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
130  }
131 
141  public static function ‪usePlainTextOutput(bool ‪$plainTextOutput): void
142  {
143  static::$plainTextOutput = ‪$plainTextOutput;
144  }
145 
155  public static function ‪useAnsiColor(bool ‪$ansiColorUsage): void
156  {
157  static::$ansiColorUsage = ‪$ansiColorUsage;
158  }
159 
160  protected static function ‪resolveNonceValue(): string
161  {
162  return GeneralUtility::makeInstance(RequestId::class)->nonce->b64;
163  }
164 }
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static stripPathSitePrefix(string $path)
Definition: PathUtility.php:428
‪TYPO3\CMS\Core\Utility\DebugUtility\resolveNonceValue
‪static resolveNonceValue()
Definition: DebugUtility.php:160
‪TYPO3\CMS\Core\Utility\DebugUtility\usePlainTextOutput
‪static usePlainTextOutput(bool $plainTextOutput)
Definition: DebugUtility.php:141
‪TYPO3\CMS\Core\Utility\DebugUtility\debugTrail
‪static string debugTrail(bool $prependFileNames=false)
Definition: DebugUtility.php:65
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\DebugUtility\viewArray
‪static string viewArray(mixed $array_in)
Definition: DebugUtility.php:114
‪TYPO3\CMS\Core\Utility\DebugUtility\useAnsiColor
‪static useAnsiColor(bool $ansiColorUsage)
Definition: DebugUtility.php:155
‪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:125
‪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:94
‪TYPO3\CMS\Core\Utility\DebugUtility
Definition: DebugUtility.php:28
‪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