TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
DebugUtility.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
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  */
17 
22 {
26  protected static $plainTextOutput = true;
27 
31  protected static $ansiColorUsage = true;
32 
42  public static function debug($var = '', $header = '', $group = 'Debug')
43  {
44  // buffer the output of debug if no buffering started before
45  if (ob_get_level() === 0) {
46  ob_start();
47  }
48  if (TYPO3_MODE === 'BE' && !self::isCommandLine()) {
49  $tabHeader = $header ?: 'Debug';
50  $debug = self::renderDump($var);
51  $debugPlain = PHP_EOL . self::renderDump($var, '', true, false);
52  $script = '
53  (function debug() {
54  var message = ' . GeneralUtility::quoteJSvalue($debug) . ',
55  messagePlain = ' . GeneralUtility::quoteJSvalue($debugPlain) . ',
56  header = ' . GeneralUtility::quoteJSvalue($tabHeader) . ',
57  group = ' . GeneralUtility::quoteJSvalue($group) . ';
58  if (top.TYPO3 && top.TYPO3.DebugConsole) {
59  top.TYPO3.DebugConsole.add(message, header, group);
60  } else {
61  var consoleMessage = [group, header, messagePlain].join(" | ");
62  if (typeof console === "object" && typeof console.log === "function") {
63  console.log(consoleMessage);
64  }
65  };
66  })();
67  ';
68  echo GeneralUtility::wrapJS($script);
69  } else {
70  echo self::renderDump($var);
71  }
72  }
73 
80  public static function convertVariableToString($variable)
81  {
82  $string = self::renderDump($variable, '', true, false);
83  return $string === '' ? '| debug |' : $string;
84  }
85 
93  public static function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug')
94  {
95  $debugString = self::renderDump($debugVariable, sprintf('%s (%s)', $header, $group));
96  $script = '
97  (function debug() {
98  var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
99  header = ' . GeneralUtility::quoteJSvalue($header) . ',
100  group = ' . GeneralUtility::quoteJSvalue($group) . ',
101 
102  browserWindow = function(debug, header, group) {
103  var newWindow = window.open("", "TYPO3DebugWindow_" + group,
104  "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
105  );
106  if (newWindow.document.body.innerHTML) {
107  newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
108  "<hr />" + debugMessage;
109  } else {
110  newWindow.document.writeln(
111  "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
112  + "<body onload=\\"self.focus()\\">"
113  + debugMessage
114  + "</body></html>"
115  );
116  }
117  };
118 
119  if (top && typeof top.TYPO3 !== "undefined" && typeof top.TYPO3.Modal !== "undefined") {
120  top.TYPO3.Modal.show(
121  "Debug: " + header + " (" + group + ")",
122  debugMessage,
123  top.TYPO3.Severity.notice
124  );
125  } else {
126  browserWindow(debugMessage, header, group);
127  }
128  })();
129  ';
130  echo GeneralUtility::wrapJS($script);
131  }
132 
139  public static function debugTrail($prependFileNames = false)
140  {
141  $trail = debug_backtrace(0);
142  $trail = array_reverse($trail);
143  array_pop($trail);
144  $path = [];
145  foreach ($trail as $dat) {
146  $fileInformation = $prependFileNames && !empty($dat['file']) ? $dat['file'] . ':' : '';
147  $pathFragment = $fileInformation . $dat['class'] . $dat['type'] . $dat['function'];
148  // add the path of the included file
149  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
150  $pathFragment .= '(' . PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . PathUtility::stripPathSitePrefix($dat['file']);
151  }
152  $path[] = $pathFragment . '#' . $dat['line'];
153  }
154  return implode(' // ', $path);
155  }
156 
163  public static function debugRows($rows, $header = '')
164  {
165  self::debug($rows, $header);
166  }
167 
175  public static function ordinalValue($string, $characters = 100)
176  {
177  if (strlen($string) < $characters) {
178  $characters = strlen($string);
179  }
180  $valuestring = '';
181  for ($i = 0; $i < $characters; $i++) {
182  $valuestring .= ' ' . ord(substr($string, $i, 1));
183  }
184  return trim($valuestring);
185  }
186 
195  public static function viewArray($array_in)
196  {
197  return self::renderDump($array_in);
198  }
199 
207  public static function printArray($array_in)
208  {
209  echo self::renderDump($array_in);
210  }
211 
221  protected static function renderDump($variable, $title = '', $plainText = null, $ansiColors = null)
222  {
223  $plainText = $plainText === null ? self::isCommandLine() && self::$plainTextOutput : $plainText;
224  $ansiColors = $ansiColors === null ? self::isCommandLine() && self::$ansiColorUsage : $ansiColors;
225  return trim(DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
226  }
227 
233  protected static function isCommandLine()
234  {
235  return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) || PHP_SAPI === 'cli';
236  }
237 
248  public static function usePlainTextOutput($plainTextOutput)
249  {
250  static::$plainTextOutput = $plainTextOutput;
251  }
252 
263  public static function useAnsiColor($ansiColorUsage)
264  {
265  static::$ansiColorUsage = $ansiColorUsage;
266  }
267 }
static usePlainTextOutput($plainTextOutput)
static var_dump($variable, $title=null, $maxDepth=8, $plainText=false, $ansiColors=true, $return=false, $blacklistedClassNames=null, $blacklistedPropertyNames=null)
static debugInPopUpWindow($debugVariable, $header= 'Debug', $group= 'Debug')
static debugRows($rows, $header= '')
static convertVariableToString($variable)
static renderDump($variable, $title= '', $plainText=null, $ansiColors=null)
static debugTrail($prependFileNames=false)
static debug($var= '', $header= '', $group= 'Debug')
debug($variable= '', $name= '*variable *', $line= '*line *', $file= '*file *', $recursiveDepth=3, $debugLevel=E_DEBUG)
static useAnsiColor($ansiColorUsage)
static ordinalValue($string, $characters=100)