From: Jan Schneider Date: Tue, 22 Dec 2009 00:49:09 +0000 (+0100) Subject: Add Horde_Support_Backtrace#getMap(). X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=67c8356f34e93a36beb529f62ed759aaa1de1db1;p=horde.git Add Horde_Support_Backtrace#getMap(). --- diff --git a/framework/Support/lib/Horde/Support/Backtrace.php b/framework/Support/lib/Horde/Support/Backtrace.php index aed0b270e..cd4f895e7 100644 --- a/framework/Support/lib/Horde/Support/Backtrace.php +++ b/framework/Support/lib/Horde/Support/Backtrace.php @@ -111,4 +111,30 @@ class Horde_Support_Backtrace { return $this->getContext(1); } + + /** + * Returns a simple, human-readable list of the complete backtrace. + * + * @return string The backtrace map. + */ + public function getMap() + { + $count = count($this->_backtrace); + $pad = strlen($count); + $map = ''; + for ($i = $count - 1; $i >= 0; $i--) { + $map .= str_pad($count - $i, $pad, ' ', STR_PAD_LEFT) . '. '; + if (isset($this->_backtrace[$i]['class'])) { + $map .= $this->_backtrace[$i]['class'] + . $this->_backtrace[$i]['type']; + } + $map .= $this->_backtrace[$i]['function'] . '()'; + if (isset($this->_backtrace[$i]['file'])) { + $map .= ' ' . $this->_backtrace[$i]['file'] + . ':' . $this->_backtrace[$i]['line']; + } + $map .= "\n"; + } + return $map; + } } diff --git a/framework/Support/test/Horde/Support/BacktraceTest.php b/framework/Support/test/Horde/Support/BacktraceTest.php index 8c026cfbe..12d25e6ab 100644 --- a/framework/Support/test/Horde/Support/BacktraceTest.php +++ b/framework/Support/test/Horde/Support/BacktraceTest.php @@ -7,6 +7,11 @@ * @license http://opensource.org/licenses/bsd-license.php */ +function backtraceTestFunction() +{ + return debug_backtrace(false); +} + /** * @group support * @category Horde @@ -17,6 +22,18 @@ */ class Horde_Support_BacktraceTest extends PHPUnit_Framework_TestCase { + // Keep these two methods on the top so that the line numbers don't change + // when new tests are added. + public function instanceMethod() + { + return Horde_Support_BacktraceTest::staticMethod(); + } + + public static function staticMethod() + { + return backtraceTestFunction(); + } + public function testCreateFromDefaultBacktrace() { $trace = new Horde_Support_Backtrace(); @@ -67,6 +84,18 @@ class Horde_Support_BacktraceTest extends PHPUnit_Framework_TestCase $this->assertEquals(count($dbt), $backtrace->getNestingLevel()); } + public function testGetMap() + { + $backtrace = new Horde_Support_Backtrace(array_slice($this->instanceMethod(), 0, 4)); + $file = __FILE__; + $this->assertEquals("1. Horde_Support_BacktraceTest->testGetMap() +2. Horde_Support_BacktraceTest->instanceMethod() $file:89 +3. Horde_Support_BacktraceTest::staticMethod() $file:29 +4. backtraceTestFunction() $file:34 +", + $backtrace->getMap()); + } + public function returnBacktrace() { return debug_backtrace();