From d0c1cba8eb69f30bcbf07ee8a0de2591bbb30cb4 Mon Sep 17 00:00:00 2001 From: Andrij Date: Mon, 21 Mar 2016 12:46:16 +0200 Subject: [PATCH] Pass Inspector into data table callbacks --- examples/example.php | 11 ++++++++++ src/Whoops/Handler/PrettyPageHandler.php | 8 +++---- .../Whoops/Handler/PrettyPageHandlerTest.php | 21 ++++++++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/examples/example.php b/examples/example.php index 155f40f..38ee8e1 100644 --- a/examples/example.php +++ b/examples/example.php @@ -36,6 +36,17 @@ $handler->addDataTable('Ice-cream I like', array( 'Vanilla' => 'ew', )); +$handler->addDataTableCallback('Details', function(\Whoops\Exception\Inspector $inspector) { + $data = array(); + $exception = $inspector->getException(); + if ($exception instanceof SomeSpecificException) { + $data['Important exception data'] = $exception->getSomeSpecificData(); + } + $data['Exception class'] = get_class($exception); + $data['Exception code'] = $exception->getCode(); + return $data; +}); + $run->pushHandler($handler); // Example: tag all frames inside a function with their function name diff --git a/src/Whoops/Handler/PrettyPageHandler.php b/src/Whoops/Handler/PrettyPageHandler.php index 2e838ac..ed5f288 100644 --- a/src/Whoops/Handler/PrettyPageHandler.php +++ b/src/Whoops/Handler/PrettyPageHandler.php @@ -180,8 +180,8 @@ class PrettyPageHandler extends Handler // Add extra entries list of data tables: // @todo: Consolidate addDataTable and addDataTableCallback - $extraTables = array_map(function ($table) { - return $table instanceof \Closure ? $table() : $table; + $extraTables = array_map(function ($table) use ($inspector) { + return $table instanceof \Closure ? $table($inspector) : $table; }, $this->getDataTables()); $vars["tables"] = array_merge($extraTables, $vars["tables"]); @@ -223,9 +223,9 @@ class PrettyPageHandler extends Handler throw new InvalidArgumentException('Expecting callback argument to be callable'); } - $this->extraTables[$label] = function () use ($callback) { + $this->extraTables[$label] = function (\Whoops\Exception\Inspector $inspector = null) use ($callback) { try { - $result = call_user_func($callback); + $result = call_user_func($callback, $inspector); // Only return the result if it can be iterated over by foreach(). return is_array($result) || $result instanceof \Traversable ? $result : array(); diff --git a/tests/Whoops/Handler/PrettyPageHandlerTest.php b/tests/Whoops/Handler/PrettyPageHandlerTest.php index 12b73fc..7ee5acd 100644 --- a/tests/Whoops/Handler/PrettyPageHandlerTest.php +++ b/tests/Whoops/Handler/PrettyPageHandlerTest.php @@ -155,27 +155,46 @@ class PrettyPageHandlerTest extends TestCase $table3 = create_function('', 'return array("oh my" => "how times have changed!");'); $expected3 = array('oh my' => 'how times have changed!'); + // Test inspector parameter in data table callback + $table4 = function (\Whoops\Exception\Inspector $inspector) use ($expected1) { + return array( + 'Exception class' => get_class($inspector->getException()), + 'Exception message' => $inspector->getExceptionMessage(), + ); + }; + $expected4 = array( + 'Exception class' => 'InvalidArgumentException', + 'Exception message' => 'Test exception message', + ); + $inspectorForTable4 = new \Whoops\Exception\Inspector( + new \InvalidArgumentException('Test exception message') + ); + // Sanity check, make sure expected values really are correct. $this->assertSame($expected1, $table1()); $this->assertSame($expected2, $table2()); $this->assertSame($expected3, $table3()); + $this->assertSame($expected4, $table4($inspectorForTable4)); $handler->addDataTableCallback('table1', $table1); $handler->addDataTableCallback('table2', $table2); $handler->addDataTableCallback('table3', $table3); + $handler->addDataTableCallback('table4', $table4); $tables = $handler->getDataTables(); - $this->assertCount(3, $tables); + $this->assertCount(4, $tables); // Supplied callable is wrapped in a closure $this->assertInstanceOf('Closure', $tables['table1']); $this->assertInstanceOf('Closure', $tables['table2']); $this->assertInstanceOf('Closure', $tables['table3']); + $this->assertInstanceOf('Closure', $tables['table4']); // Run each wrapped callable and check results against expected output. $this->assertEquals($expected1, $tables['table1']()); $this->assertEquals($expected2, $tables['table2']()); $this->assertEquals($expected3, $tables['table3']()); + $this->assertEquals($expected4, $tables['table4']($inspectorForTable4)); $this->assertSame($tables['table1'], $handler->getDataTables('table1')); $this->assertSame($expected1, call_user_func($handler->getDataTables('table1')));