Pass Inspector into data table callbacks

This commit is contained in:
Andrij
2016-03-21 12:46:16 +02:00
parent 1de56025c4
commit d0c1cba8eb
3 changed files with 35 additions and 5 deletions
+11
View File
@@ -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
+4 -4
View File
@@ -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();
+20 -1
View File
@@ -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')));