Unwinding several ifs used in debug(). Creating simple string templates instead. Fixes #818

This commit is contained in:
mark_story 2010-10-24 18:23:27 -04:00
parent cef9927604
commit 02ade65509
2 changed files with 25 additions and 19 deletions

View file

@ -92,33 +92,39 @@
*/ */
function debug($var = false, $showHtml = false, $showFrom = true) { function debug($var = false, $showHtml = false, $showFrom = true) {
if (Configure::read('debug') > 0) { if (Configure::read('debug') > 0) {
$file = '';
$line = '';
if ($showFrom) { if ($showFrom) {
$calledFrom = debug_backtrace(); $calledFrom = debug_backtrace();
if(defined('CAKEPHP_SHELL')){ $file = substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1);
echo "##########DEBUG##########\n"; $line = $calledFrom[0]['line'];
echo substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) ;
echo ' (line ' . $calledFrom[0]['line'] . ')'."\n";
}else{
echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
}
}
if(!defined('CAKEPHP_SHELL')){
echo "\n<pre class=\"cake-debug\">\n";
} }
$html = <<<HTML
<strong>%s</strong> (line <strong>%s</strong>)
<pre class="cake-debug">
%s
</pre>
HTML;
$text = <<<TEXT
%s (line %s)
########## DEBUG ##########
%s
###########################
TEXT;
$template = $html;
if (php_sapi_name() == 'cli') {
$template = $text;
}
$var = print_r($var, true); $var = print_r($var, true);
if ($showHtml) { if ($showHtml) {
$var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var)); $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
} }
echo $var . "\n"; printf($template, $file, $line, $var);
if(!defined('CAKEPHP_SHELL')){
echo "<pre class=\"cake-debug\">\n";
}else{
echo "#########################\n";
};
} }
} }
if (!function_exists('sortByKey')) { if (!function_exists('sortByKey')) {
/** /**

View file

@ -585,7 +585,7 @@ class BasicsTest extends CakeTestCase {
ob_start(); ob_start();
debug('this-is-a-test'); debug('this-is-a-test');
$result = ob_get_clean(); $result = ob_get_clean();
$pattern = '/.*\>(.+?tests(\/|\\\)cases(\/|\\\)basics\.test\.php|'; $pattern = '/(.+?tests(\/|\\\)cases(\/|\\\)basics\.test\.php|';
$pattern .= preg_quote(substr(__FILE__, 1), '/') . ')'; $pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
$pattern .= '.*line.*' . (__LINE__ - 4) . '.*this-is-a-test.*/s'; $pattern .= '.*line.*' . (__LINE__ - 4) . '.*this-is-a-test.*/s';
$this->assertPattern($pattern, $result); $this->assertPattern($pattern, $result);
@ -593,7 +593,7 @@ class BasicsTest extends CakeTestCase {
ob_start(); ob_start();
debug('<div>this-is-a-test</div>', true); debug('<div>this-is-a-test</div>', true);
$result = ob_get_clean(); $result = ob_get_clean();
$pattern = '/.*\>(.+?tests(\/|\\\)cases(\/|\\\)basics\.test\.php|'; $pattern = '/(.+?tests(\/|\\\)cases(\/|\\\)basics\.test\.php|';
$pattern .= preg_quote(substr(__FILE__, 1), '/') . ')'; $pattern .= preg_quote(substr(__FILE__, 1), '/') . ')';
$pattern .= '.*line.*' . (__LINE__ - 4) . '.*&lt;div&gt;this-is-a-test&lt;\/div&gt;.*/s'; $pattern .= '.*line.*' . (__LINE__ - 4) . '.*&lt;div&gt;this-is-a-test&lt;\/div&gt;.*/s';
$this->assertPattern($pattern, $result); $this->assertPattern($pattern, $result);