Added a shorthand stackTrace() method

This commit is contained in:
euromark 2014-07-10 01:49:28 +02:00
parent 3a70d9c033
commit ac0053d660
2 changed files with 80 additions and 30 deletions

View file

@ -20,6 +20,7 @@ require_once CAKE . 'basics.php';
App::uses('Folder', 'Utility'); App::uses('Folder', 'Utility');
App::uses('CakeResponse', 'Network'); App::uses('CakeResponse', 'Network');
App::uses('Debugger', 'Utility');
/** /**
* BasicsTest class * BasicsTest class
@ -1140,6 +1141,24 @@ EXPECTED;
$this->assertEquals($expected, stripslashes_deep($nested)); $this->assertEquals($expected, stripslashes_deep($nested));
} }
/**
* Tests that the stackTrace() method is a shortcut for Debugger::trace()
*
* @return void
*/
public function testStackTrace() {
ob_start();
list(, $expected) = array(stackTrace(), Debugger::trace());
$result = ob_get_clean();
$this->assertEquals($expected, $result);
$opts = array('args' => true);
ob_start();
list(, $expected) = array(stackTrace($opts), Debugger::trace($opts));
$result = ob_get_clean();
$this->assertEquals($expected, $result);
}
/** /**
* test pluginSplit * test pluginSplit
* *

View file

@ -71,8 +71,11 @@ if (!function_exists('debug')) {
* @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug * @link http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html#debug
*/ */
function debug($var, $showHtml = null, $showFrom = true) { function debug($var, $showHtml = null, $showFrom = true) {
if (Configure::read('debug') > 0) { if (!Configure::read('debug')) {
return;
}
App::uses('Debugger', 'Utility'); App::uses('Debugger', 'Utility');
$file = ''; $file = '';
$line = ''; $line = '';
$lineInfo = ''; $lineInfo = '';
@ -116,6 +119,34 @@ TEXT;
} }
printf($template, $lineInfo, $var); printf($template, $lineInfo, $var);
} }
}
if (!function_exists('stackTrace')) {
/**
* Outputs a stack trace based on the supplied options.
*
* ### Options
*
* - `depth` - The number of stack frames to return. Defaults to 999
* - `args` - Should arguments for functions be shown? If true, the arguments for each method call
* will be displayed.
* - `start` - The stack frame to start generating a trace from. Defaults to 1
*
* @param array $options Format for outputting stack trace
* @return mixed Formatted stack trace
* @see Debugger::trace()
*/
function stackTrace(array $options = array()) {
if (!Configure::read('debug')) {
return;
}
App::uses('Debugger', 'Utility');
$options += array('start' => 0);
$options['start']++;
echo Debugger::trace($options);
} }
} }