TYPO3 CMS  TYPO3_7-6
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 
138  public static function debugTrail()
139  {
140  $trail = debug_backtrace(0);
141  $trail = array_reverse($trail);
142  array_pop($trail);
143  $path = [];
144  foreach ($trail as $dat) {
145  $pathFragment = $dat['class'] . $dat['type'] . $dat['function'];
146  // add the path of the included file
147  if (in_array($dat['function'], ['require', 'include', 'require_once', 'include_once'])) {
148  $pathFragment .= '(' . PathUtility::stripPathSitePrefix($dat['args'][0]) . '),' . PathUtility::stripPathSitePrefix($dat['file']);
149  }
150  $path[] = $pathFragment . '#' . $dat['line'];
151  }
152  return implode(' // ', $path);
153  }
154 
162  public static function debugRows($rows, $header = '', $returnHTML = false)
163  {
164  if ($returnHTML !== false) {
165  GeneralUtility::deprecationLog('Setting the parameter $returnHTML is deprecated since TYPO3 CMS 7 and will be removed in TYPO3 CMS 8.');
166  }
167  self::debug($rows, $header);
168  }
169 
177  public static function ordinalValue($string, $characters = 100)
178  {
179  if (strlen($string) < $characters) {
180  $characters = strlen($string);
181  }
182  $valuestring = '';
183  for ($i = 0; $i < $characters; $i++) {
184  $valuestring .= ' ' . ord(substr($string, $i, 1));
185  }
186  return trim($valuestring);
187  }
188 
197  public static function viewArray($array_in)
198  {
199  return self::renderDump($array_in);
200  }
201 
209  public static function printArray($array_in)
210  {
211  echo self::renderDump($array_in);
212  }
213 
223  protected static function renderDump($variable, $title = '', $plainText = null, $ansiColors = null)
224  {
225  $plainText = $plainText === null ? self::isCommandLine() && self::$plainTextOutput : $plainText;
226  $ansiColors = $ansiColors === null ? self::isCommandLine() && self::$ansiColorUsage : $ansiColors;
227  return trim(DebuggerUtility::var_dump($variable, $title, 8, $plainText, $ansiColors, true));
228  }
229 
235  protected static function isCommandLine()
236  {
237  return (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_CLI) || PHP_SAPI === 'cli';
238  }
239 
250  public static function usePlainTextOutput($plainTextOutput)
251  {
252  static::$plainTextOutput = $plainTextOutput;
253  }
254 
265  public static function useAnsiColor($ansiColorUsage)
266  {
267  static::$ansiColorUsage = $ansiColorUsage;
268  }
269 }
static var_dump($variable, $title=null, $maxDepth=8, $plainText=false, $ansiColors=true, $return=false, $blacklistedClassNames=null, $blacklistedPropertyNames=null)
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)
static convertVariableToString($variable)
static usePlainTextOutput($plainTextOutput)
static renderDump($variable, $title='', $plainText=null, $ansiColors=null)
static debugInPopUpWindow($debugVariable, $header='Debug', $group='Debug')
static debugRows($rows, $header='', $returnHTML=false)
static wrapJS($string, $linebreak=true)