Add method which allows tables data to be gathered when an error is caught

This commit is contained in:
Chris Smith
2013-05-10 14:15:32 +01:00
parent ce3c4dd3ed
commit 92c55f59a3
+32 -1
View File
@@ -97,8 +97,12 @@ class PrettyPageHandler extends Handler
)
);
$extraTables = array_map(function($table) {
return $table instanceof \Closure ? $table() : $table;
}, $this->getDataTables());
// Add extra entries list of data tables:
$v->tables = array_merge($this->getDataTables(), $v->tables);
$v->tables = array_merge($extraTables, $v->tables);
call_user_func(function() use($templateFile, $v) {
// $e -> cleanup output, optionally preserving URIs as anchors:
@@ -142,6 +146,33 @@ class PrettyPageHandler extends Handler
$this->extraTables[$label] = $data;
}
/**
* Lazily adds an entry to the list of tables displayed in the table.
* The supplied callback argument will be called when the error is rendered,
* it should produce a simple associative array. Any nested arrays will
* be flattened with print_r.
* @param string $label
* @param callable $callback Callable returning an associative array
*/
public function addDataTableCallback($label, /* callable */ $callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException('Expecting callback argument to be callable');
}
$this->extraTables[$label] = function() use ($callback) {
try {
$result = call_user_func($callback);
// Only return the result if it can be iterated over by foreach().
return is_array($result) || $result instanceof \Traversable ? $result : array();
} catch (\Exception $e) {
// Don't allow failiure to break the rendering of the original exception.
return array();
}
};
}
/**
* Returns all the extra data tables registered with this handler.
* Optionally accepts a 'label' parameter, to only return the data