‪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 
22 
27 {
28  protected static bool ‪$plainTextOutput = true;
29 
30  protected static bool ‪$ansiColorUsage = true;
31 
37  public static function ‪debug(mixed $var = '', string $header = 'Debug'): void
38  {
39  // buffer the output of debug if no buffering started before
40  if (ob_get_level() === 0) {
41  ob_start();
42  }
43 
44  echo ‪self::renderDump($var, $header);
45  }
46 
52  public static function ‪convertVariableToString(mixed $variable): string
53  {
54  $string = ‪self::renderDump($variable, '', true, false);
55  return $string === '' ? '| debug |' : $string;
56  }
57 
64  public static function ‪debugTrail(bool $prependFileNames = false): string
65  {
66  $trail = debug_backtrace(0);
67  $trail = array_reverse($trail);
68  array_pop($trail);
69  $path = [];
70  foreach ($trail as $dat) {
71  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
72  $pathFragment = $fileInformation . ($dat['class'] ?? '') . ($dat['type'] ?? '') . $dat['function'];
73  // add the path of the included file
74  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
75  $pathFragment .= '(' . ‪PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . ‪PathUtility::stripPathSitePrefix($dat['file']);
76  }
77  if (array_key_exists('line', $dat)) {
78  $path[] = $pathFragment . '#' . $dat['line'];
79  } else {
80  $path[] = $pathFragment;
81  }
82  }
83  return implode(' // ', $path);
84  }
85 
93  public static function ‪ordinalValue(string $string, int $characters = 100): string
94  {
95  if (strlen($string) < $characters) {
96  $characters = strlen($string);
97  }
98  $valuestring = '';
99  for ($i = 0; $i < $characters; $i++) {
100  $valuestring .= ' ' . ord($string[$i]);
101  }
102  return trim($valuestring);
103  }
104 
113  public static function ‪viewArray(mixed $array_in): string
114  {
115  return ‪self::renderDump($array_in);
116  }
117 
124  protected static function ‪renderDump(mixed $variable, string $title = '', ?bool $plainText = null, ?bool $ansiColors = null): string
125  {
126  $plainText = $plainText ?? ‪Environment::isCli() && ‪self::$plainTextOutput;
127  $ansiColors = $ansiColors ?? ‪Environment::isCli() && ‪self::$ansiColorUsage;
128  return trim(‪DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
129  }
130 
140  public static function ‪usePlainTextOutput(bool ‪$plainTextOutput): void
141  {
142  static::$plainTextOutput = ‪$plainTextOutput;
143  }
144 
154  public static function ‪useAnsiColor(bool ‪$ansiColorUsage): void
155  {
156  static::$ansiColorUsage = ‪$ansiColorUsage;
157  }
158 }
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static stripPathSitePrefix(string $path)
Definition: PathUtility.php:428
‪TYPO3\CMS\Core\Utility\DebugUtility\usePlainTextOutput
‪static usePlainTextOutput(bool $plainTextOutput)
Definition: DebugUtility.php:140
‪TYPO3\CMS\Core\Utility\DebugUtility\debugTrail
‪static string debugTrail(bool $prependFileNames=false)
Definition: DebugUtility.php:64
‪TYPO3\CMS\Core\Utility
Definition: ArrayUtility.php:18
‪TYPO3\CMS\Core\Utility\DebugUtility\viewArray
‪static string viewArray(mixed $array_in)
Definition: DebugUtility.php:113
‪TYPO3\CMS\Core\Utility\DebugUtility\useAnsiColor
‪static useAnsiColor(bool $ansiColorUsage)
Definition: DebugUtility.php:154
‪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:37
‪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:124
‪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:93
‪TYPO3\CMS\Core\Utility\DebugUtility
Definition: DebugUtility.php:27
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\DebugUtility\$ansiColorUsage
‪static bool $ansiColorUsage
Definition: DebugUtility.php:30
‪TYPO3\CMS\Core\Utility\DebugUtility\$plainTextOutput
‪static bool $plainTextOutput
Definition: DebugUtility.php:28
‪TYPO3\CMS\Core\Utility\DebugUtility\convertVariableToString
‪static string convertVariableToString(mixed $variable)
Definition: DebugUtility.php:52