Browse Source

Add Debugger::print()

ADmad 9 years ago
parent
commit
230d4945ee
3 changed files with 244 additions and 55 deletions
  1. 62 0
      src/Error/Debugger.php
  2. 15 55
      src/basics.php
  3. 167 0
      tests/TestCase/Error/DebuggerTest.php

+ 62 - 0
src/Error/Debugger.php

@@ -792,6 +792,68 @@ class Debugger
     }
 
     /**
+     * Prints out debug information about given variable.
+     *
+     * @param mixed $var Variable to show debug information for.
+     * @param array $location If contains keys "file" and "line" their values will
+     *    be used to show location info.
+     * @param bool|null $showHtml If set to true, the method prints the debug
+     *    data in a browser-friendly way.
+     * @return void
+     */
+    public static function print($var, $location = [], $showHtml = null)
+    {
+        $location += ['file' => null, 'line' => null];
+        $file = $location['file'];
+        $line = $location['line'];
+        $lineInfo = '';
+        if ($file) {
+            $search = [];
+            if (defined('ROOT')) {
+                $search = [ROOT];
+            }
+            if (defined('CAKE_CORE_INCLUDE_PATH')) {
+                array_unshift($search, CAKE_CORE_INCLUDE_PATH);
+            }
+            $file = str_replace($search, '', $file);
+        }
+        $html = <<<HTML
+<div class="cake-debug-output">
+%s
+<pre class="cake-debug">
+%s
+</pre>
+</div>
+HTML;
+        $text = <<<TEXT
+%s
+########## DEBUG ##########
+%s
+###########################
+
+TEXT;
+        $template = $html;
+        if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') || $showHtml === false) {
+            $template = $text;
+            if ($file && $line) {
+                $lineInfo = sprintf('%s (line %s)', $file, $line);
+            }
+        }
+        if ($showHtml === null && $template !== $text) {
+            $showHtml = true;
+        }
+        $var = Debugger::exportVar($var, 25);
+        if ($showHtml) {
+            $template = $html;
+            $var = h($var);
+            if ($file && $line) {
+                $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
+            }
+        }
+        printf($template, $lineInfo, $var);
+    }
+
+    /**
      * Verifies that the application's salt and cipher seed value has been changed from the default value.
      *
      * @return void

+ 15 - 55
src/basics.php

@@ -46,64 +46,18 @@ if (!function_exists('debug')) {
             return $var;
         }
 
-        $originalVar = $var;
-        $file = '';
-        $line = '';
-        $lineInfo = '';
+        $location = [];
         if ($showFrom) {
-            $trace = Debugger::trace(['start' => 1, 'depth' => 3, 'format' => 'array']);
-            if (count($trace) > 1
-                && $trace[1]['function'] === 'dd'
-                && !isset($trace[1]['object'])
-            ) {
-                array_shift($trace);
-            }
-            $search = [];
-            if (defined('ROOT')) {
-                $search = [ROOT];
-            }
-            if (defined('CAKE_CORE_INCLUDE_PATH')) {
-                array_unshift($search, CAKE_CORE_INCLUDE_PATH);
-            }
-            $file = str_replace($search, '', $trace[0]['file']);
-            $line = $trace[0]['line'];
+            $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
+            $location = [
+                'line' => $trace[0]['line'],
+                'file' => $trace[0]['file']
+            ];
         }
-        $html = <<<HTML
-<div class="cake-debug-output">
-%s
-<pre class="cake-debug">
-%s
-</pre>
-</div>
-HTML;
-        $text = <<<TEXT
-%s
-########## DEBUG ##########
-%s
-###########################
 
-TEXT;
-        $template = $html;
-        if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') || $showHtml === false) {
-            $template = $text;
-            if ($showFrom) {
-                $lineInfo = sprintf('%s (line %s)', $file, $line);
-            }
-        }
-        if ($showHtml === null && $template !== $text) {
-            $showHtml = true;
-        }
-        $var = Debugger::exportVar($var, 25);
-        if ($showHtml) {
-            $template = $html;
-            $var = h($var);
-            if ($showFrom) {
-                $lineInfo = sprintf('<span><strong>%s</strong> (line <strong>%s</strong>)</span>', $file, $line);
-            }
-        }
-        printf($template, $lineInfo, $var);
+        Debugger::print($var, $location, $showHtml);
 
-        return $originalVar;
+        return $var;
     }
 
 }
@@ -176,7 +130,13 @@ if (!function_exists('dd')) {
             return;
         }
 
-        debug($var, $showHtml);
+        $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
+        $location = [
+            'line' => $trace[0]['line'],
+            'file' => $trace[0]['file']
+        ];
+
+        Debugger::print($var, $location);
         die(1);
     }
 }

+ 167 - 0
tests/TestCase/Error/DebuggerTest.php

@@ -585,4 +585,171 @@ object(Cake\Test\TestCase\Error\DebuggableThing) {
 eos;
         $this->assertEquals($expected, $result);
     }
+
+    /**
+     * test testPrint()
+     *
+     * @return void
+     */
+    public function testPrint()
+    {
+        ob_start();
+        Debugger::print('this-is-a-test', ['file' => __FILE__, 'line' => __LINE__], false);
+        $result = ob_get_clean();
+        $expectedText = <<<EXPECTED
+%s (line %d)
+########## DEBUG ##########
+'this-is-a-test'
+###########################
+
+EXPECTED;
+        $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
+
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        $value = '<div>this-is-a-test</div>';
+        Debugger::print($value, ['file' => __FILE__, 'line' => __LINE__], true);
+        $result = ob_get_clean();
+        $expectedHtml = <<<EXPECTED
+<div class="cake-debug-output">
+<span><strong>%s</strong> (line <strong>%d</strong>)</span>
+<pre class="cake-debug">
+&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
+</pre>
+</div>
+EXPECTED;
+        $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], true);
+        $result = ob_get_clean();
+        $expected = <<<EXPECTED
+<div class="cake-debug-output">
+<span><strong>%s</strong> (line <strong>%d</strong>)</span>
+<pre class="cake-debug">
+&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
+</pre>
+</div>
+EXPECTED;
+        $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>', [], true);
+        $result = ob_get_clean();
+        $expected = <<<EXPECTED
+<div class="cake-debug-output">
+
+<pre class="cake-debug">
+&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
+</pre>
+</div>
+EXPECTED;
+        $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 10);
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__]);
+        $result = ob_get_clean();
+        $expectedHtml = <<<EXPECTED
+<div class="cake-debug-output">
+<span><strong>%s</strong> (line <strong>%d</strong>)</span>
+<pre class="cake-debug">
+&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
+</pre>
+</div>
+EXPECTED;
+        $expectedText = <<<EXPECTED
+%s (line %d)
+########## DEBUG ##########
+'<div>this-is-a-test</div>'
+###########################
+
+EXPECTED;
+        if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
+            $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
+        } else {
+            $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
+        }
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>');
+        $result = ob_get_clean();
+        $expectedHtml = <<<EXPECTED
+<div class="cake-debug-output">
+
+<pre class="cake-debug">
+&#039;&lt;div&gt;this-is-a-test&lt;/div&gt;&#039;
+</pre>
+</div>
+EXPECTED;
+        $expectedText = <<<EXPECTED
+
+########## DEBUG ##########
+'<div>this-is-a-test</div>'
+###########################
+
+EXPECTED;
+        if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
+            $expected = sprintf($expectedText, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 18);
+        } else {
+            $expected = sprintf($expectedHtml, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 19);
+        }
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
+        $result = ob_get_clean();
+        $expected = <<<EXPECTED
+%s (line %d)
+########## DEBUG ##########
+'<div>this-is-a-test</div>'
+###########################
+
+EXPECTED;
+        $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>', ['file' => __FILE__, 'line' => __LINE__], false);
+        $result = ob_get_clean();
+        $expected = <<<EXPECTED
+%s (line %d)
+########## DEBUG ##########
+'<div>this-is-a-test</div>'
+###########################
+
+EXPECTED;
+        $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print('<div>this-is-a-test</div>', [], false);
+        $result = ob_get_clean();
+        $expected = <<<EXPECTED
+
+########## DEBUG ##########
+'<div>this-is-a-test</div>'
+###########################
+
+EXPECTED;
+        $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
+        $this->assertEquals($expected, $result);
+
+        ob_start();
+        Debugger::print(false, [], false);
+        $result = ob_get_clean();
+        $expected = <<<EXPECTED
+
+########## DEBUG ##########
+false
+###########################
+
+EXPECTED;
+        $expected = sprintf($expected, str_replace(CAKE_CORE_INCLUDE_PATH, '', __FILE__), __LINE__ - 9);
+        $this->assertEquals($expected, $result);
+    }
 }