Skip to content

Commit

Permalink
Add Debugger::print()
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jan 8, 2017
1 parent c8eb2e1 commit 230d494
Show file tree
Hide file tree
Showing 3 changed files with 244 additions and 55 deletions.
62 changes: 62 additions & 0 deletions src/Error/Debugger.php
Expand Up @@ -791,6 +791,68 @@ public static function getType($var)
return 'unknown';
}

/**
* 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.
*
Expand Down
70 changes: 15 additions & 55 deletions src/basics.php
Expand Up @@ -46,64 +46,18 @@ function debug($var, $showHtml = null, $showFrom = true)
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;
}

}
Expand Down Expand Up @@ -176,7 +130,13 @@ function dd($var, $showHtml = null)
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 changes: 167 additions & 0 deletions tests/TestCase/Error/DebuggerTest.php
Expand Up @@ -585,4 +585,171 @@ public function testDebugInfo()
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);
}
}

0 comments on commit 230d494

Please sign in to comment.