[TASK] Disable compression for inlineJs in Backend
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_debug.php
index 1947653..d819f66 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
  * Copyright notice
  *
- * (c) 2010 Steffen Kamper <steffen@typo3.org>
+ * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
  * All rights reserved
  *
  * This script is part of the TYPO3 project. The TYPO3 project is
 /**
  * Class to handle debug
  *
- * $Id: $
- *
  *
  * @author      Steffen Kamper <steffen@typo3.org>
  * @package TYPO3
  * @subpackage t3lib
  */
-class t3lib_utility_Debug {
+final class t3lib_utility_Debug {
 
        /**
         * Template for debug output
@@ -56,30 +54,19 @@ class t3lib_utility_Debug {
        ';
 
 
-       public function debug($var = '', $header = '', $group = 'Debug') {
-               // buffer the output of debug if no buffering started before
+       public static function debug($var = '', $header = '', $group = 'Debug') {
+                       // buffer the output of debug if no buffering started before
                if (ob_get_level() == 0) {
                        ob_start();
                }
-               $debug = '';
-
-               if (is_array($var)) {
-                       $debug .= self::viewArray($var);
-               } elseif (is_object($var)) {
-                       $debug .= '<strong>|Object:<pre>';
-                       $debug .= print_r($var, TRUE);
-                       $debug .= '</pre>|</strong>';
-               } elseif ((string) $var !== '') {
-                       $debug .= '<strong>|' . htmlspecialchars((string) $var) . '|</strong>';
-               } else {
-                       $debug .= '<strong>| debug |</strong>';
-               }
 
+               $debug = self::convertVariableToString($var);
                if ($header) {
                        $debug = sprintf(self::DEBUG_TABLE_TEMPLATE, htmlspecialchars((string) $header), $debug);
                }
 
                if (TYPO3_MODE === 'BE') {
+                       $debugString = self::prepareVariableForJavascript($debug, is_object($var));
                        $group = htmlspecialchars($group);
 
                        if ($header !== '') {
@@ -88,23 +75,9 @@ class t3lib_utility_Debug {
                                $tabHeader = 'Debug';
                        }
 
-                       if (is_object($var)) {
-                               $debug = str_replace(array (
-                                       '"', '/', '<', "\n", "\r"
-                               ), array (
-                                       '\"', '\/', '\<', '<br />', ''
-                               ), $debug);
-                       } else {
-                               $debug = str_replace(array (
-                                       '"', '/', '<', "\n", "\r"
-                               ), array (
-                                       '\"', '\/', '\<', '', ''
-                               ), $debug);
-                       }
-
                        $script = '
                                (function debug() {
-                                       var debugMessage = "' . $debug . '";
+                                       var debugMessage = "' . $debugString . '";
                                        var header = "' . $tabHeader . '";
                                        var group = "' . $group . '";
 
@@ -141,16 +114,116 @@ class t3lib_utility_Debug {
        }
 
        /**
+        * Replaces special characters for the usage inside javascript
+        *
+        * @param string $string
+        * @param boolean $asObject
+        * @return string
+        */
+       public static function prepareVariableForJavascript($string, $asObject) {
+               if ($asObject) {
+                       $string = str_replace(array(
+                               '"', '/', '<', "\n", "\r"
+                       ), array(
+                               '\"', '\/', '\<', '<br />', ''
+                       ), $string);
+               } else {
+                       $string = str_replace(array(
+                               '"', '/', '<', "\n", "\r"
+                 ), array(
+                               '\"', '\/', '\<', '', ''
+                       ), $string);
+               }
+
+               return $string;
+       }
+
+       /**
+        * Converts a variable to a string
+        *
+        * @param mixed $variable
+        * @return string
+        */
+       public static function convertVariableToString($variable) {
+               $string = '';
+               if (is_array($variable)) {
+                       $string = self::viewArray($variable);
+               } elseif (is_object($variable)) {
+                       $string = '<strong>|Object:<pre>';
+                       $string .= print_r($variable, TRUE);
+                       $string .= '</pre>|</strong>';
+               } elseif ((string) $variable !== '') {
+                       $string = '<strong>|' . htmlspecialchars((string) $variable) . '|</strong>';
+               } else {
+                       $string = '<strong>| debug |</strong>';
+               }
+
+               return $string;
+       }
+
+       /**
+        * Opens a debug message inside a popup window
+        *
+        * @param mixed $debugVariable
+        * @param string $header
+        * @param string $group
+        */
+       public static function debugInPopUpWindow($debugVariable, $header = 'Debug', $group = 'Debug') {
+               $debugString = self::prepareVariableForJavascript(
+                       self::convertVariableToString($debugVariable),
+                       is_object($debugVariable)
+               );
+
+               $script = '
+                       (function debug() {
+                               var debugMessage = "' . $debugString . '",
+                                       header = "' . htmlspecialchars($header) . '",
+                                       group = "' . htmlspecialchars($group) . '",
+
+                                       browserWindow = function(debug, header, group) {
+                                               var newWindow = window.open("", "TYPO3DebugWindow_" + group,
+                                                       "width=600,height=400,menubar=0,toolbar=1,status=0,scrollbars=1,resizable=1"
+                                               );
+                                               if (newWindow.document.body.innerHTML) {
+                                                       newWindow.document.body.innerHTML = newWindow.document.body.innerHTML +
+                                                               "<hr />" + debugMessage;
+                                               } else {
+                                                       newWindow.document.writeln(
+                                                               "<html><head><title>Debug: " + header + "(" + group + ")</title></head>"
+                                                               + "<body onload=\"self.focus()\">"
+                                                               + debugMessage
+                                                               + "</body></html>"
+                                                       );
+                                               }
+                                       }
+
+                               if (!top.Ext) {
+                                       browserWindow(debugMessage, header, group);
+                               } else {
+                                       top.Ext.onReady(function() {
+                                               if (top && top.TYPO3 && top.TYPO3.Backend) {
+                                                       top.TYPO3.Backend.DebugConsole.openBrowserWindow(header, debugMessage, group);
+                                               } else {
+                                                       browserWindow(debugMessage, header, group);
+                                               }
+                                       });
+                               }
+                       })();
+               ';
+               echo t3lib_div::wrapJS($script);
+       }
+
+       /**
         * Displays the "path" of the function call stack in a string, using debug_backtrace
         *
         * @return      string
         */
-       public function debugTrail() {
+       public static function debugTrail() {
                $trail = debug_backtrace();
                $trail = array_reverse($trail);
                array_pop($trail);
 
-               $path = array ();
+               $path = array();
                foreach ($trail as $dat) {
                        $path[] = $dat['class'] . $dat['type'] . $dat['function'] . '#' . $dat['line'];
                }
@@ -166,19 +239,18 @@ class t3lib_utility_Debug {
         * @param       boolean         If TRUE, will return content instead of echo'ing out.
         * @return      void            Outputs to browser.
         */
-       public function debugRows($rows, $header = '', $returnHTML = FALSE) {
+       public static function debugRows($rows, $header = '', $returnHTML = FALSE) {
                if (is_array($rows)) {
-                       reset($rows);
-                       $firstEl = current($rows);
+                       $firstEl = reset($rows);
                        if (is_array($firstEl)) {
                                $headerColumns = array_keys($firstEl);
-                               $tRows = array ();
+                               $tRows = array();
 
-                               // Header:
+                                       // Header:
                                $tRows[] = '<tr><td colspan="' . count($headerColumns) .
-                                       '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' .
-                                       htmlspecialchars($header) . '</strong></td></tr>';
-                               $tCells = array ();
+                                                  '" style="background-color:#bbbbbb; font-family: verdana,arial; font-weight: bold; font-size: 10px;"><strong>' .
+                                                  htmlspecialchars($header) . '</strong></td></tr>';
+                               $tCells = array();
                                foreach ($headerColumns as $key) {
                                        $tCells[] = '
                                                        <td><font face="Verdana,Arial" size="1"><strong>' . htmlspecialchars($key) . '</strong></font></td>';
@@ -187,14 +259,14 @@ class t3lib_utility_Debug {
                                                <tr>' . implode('', $tCells) . '
                                                </tr>';
 
-                               // Rows:
+                                       // Rows:
                                foreach ($rows as $singleRow) {
-                                       $tCells = array ();
+                                       $tCells = array();
                                        foreach ($headerColumns as $key) {
                                                $tCells[] = '
                                                        <td><font face="Verdana,Arial" size="1">' .
-                                                       (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) .
-                                                       '</font></td>';
+                                                                       (is_array($singleRow[$key]) ? self::debugRows($singleRow[$key], '', TRUE) : htmlspecialchars($singleRow[$key])) .
+                                                                       '</font></td>';
                                        }
                                        $tRows[] = '
                                                <tr>' . implode('', $tCells) . '
@@ -204,12 +276,17 @@ class t3lib_utility_Debug {
                                $table = '
                                        <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">' . implode('', $tRows) . '
                                        </table>';
-                               if ($returnHTML)
+                               if ($returnHTML) {
                                        return $table;
+                               }
                                else
+                               {
                                        echo $table;
+                               }
                        } else
+                       {
                                debug('Empty array of rows', $header);
+                       }
                } else {
                        debug('No array of rows', $header);
                }
@@ -221,11 +298,11 @@ class t3lib_utility_Debug {
         * @param       string          String to show ASCII value for
         * @param       integer         Number of characters to show
         * @return      string          The string with ASCII values in separated by a space char.
-        * @deprecated since TYPO3 4.5 - Use t3lib_utility_Debug::debug_ordvalue instead
         */
-       public function ordinalValue($string, $characters = 100) {
-               if (strlen($string) < $characters)
+       public static function ordinalValue($string, $characters = 100) {
+               if (strlen($string) < $characters) {
                        $characters = strlen($string);
+               }
                for ($i = 0; $i < $characters; $i++) {
                        $valuestring .= ' ' . ord(substr($string, $i, 1));
                }
@@ -235,12 +312,12 @@ class t3lib_utility_Debug {
        /**
         * Returns HTML-code, which is a visual representation of a multidimensional array
         * use t3lib_div::print_array() in order to print an array
-        * Returns false if $array_in is not an array
+        * Returns FALSE if $array_in is not an array
         *
         * @param       mixed           Array to view
         * @return      string          HTML output
         */
-       public function viewArray($array_in) {
+       public static function viewArray($array_in) {
                if (is_array($array_in)) {
                        $result = '
                        <table border="1" cellpadding="1" cellspacing="0" bgcolor="white">';
@@ -261,8 +338,8 @@ class t3lib_utility_Debug {
                                                        $string .= print_r($val, TRUE);
                                                }
                                                $result .= '<font face="Verdana,Arial" size="1" color="red">' .
-                                                       nl2br(htmlspecialchars($string)) .
-                                                       '<br /></font>';
+                                                                  nl2br(htmlspecialchars($string)) .
+                                                                  '<br /></font>';
                                        } else {
                                                if (gettype($val) == 'object') {
                                                        $string = 'Unknown object';
@@ -270,8 +347,8 @@ class t3lib_utility_Debug {
                                                        $string = (string) $val;
                                                }
                                                $result .= '<font face="Verdana,Arial" size="1" color="red">' .
-                                                       nl2br(htmlspecialchars($string)) .
-                                                       '<br /></font>';
+                                                                  nl2br(htmlspecialchars($string)) .
+                                                                  '<br /></font>';
                                        }
                                        $result .= '</td>
                                        </tr>';
@@ -282,8 +359,8 @@ class t3lib_utility_Debug {
                        $result = '<table border="1" cellpadding="1" cellspacing="0" bgcolor="white">
                                <tr>
                                        <td><font face="Verdana,Arial" size="1" color="red">' .
-                                       nl2br(htmlspecialchars((string) $array_in)) .
-                                       '<br /></font></td>
+                                         nl2br(htmlspecialchars((string) $array_in)) .
+                                         '<br /></font></td>
                                </tr>
                        </table>'; // Output it as a string.
                }
@@ -297,13 +374,9 @@ class t3lib_utility_Debug {
         * @return      void
         * @see view_array()
         */
-       public function printArray($array_in) {
+       public static function printArray($array_in) {
                echo self::viewArray($array_in);
        }
 }
 
-
-if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/utility/class.t3lib_utility_debug.php'])    {
-       include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/utility/class.t3lib_utility_debug.php']);
-}
 ?>
\ No newline at end of file