diff --git a/examples/example.php b/examples/example.php index fcd36bc..4298ef8 100644 --- a/examples/example.php +++ b/examples/example.php @@ -22,8 +22,18 @@ require __DIR__ . '/../vendor/autoload.php'; class Exception extends BaseException {} -$run = new Run; -$run->pushHandler(new PrettyPageHandler); +$run = new Run; +$handler = new PrettyPageHandler; + +// Add a custom table to the layout: +$handler->addDataTable('Ice-cream I like', array( + 'Chocolate' => 'yes', + 'Coffee & chocolate' => 'a lot', + 'Strawberry & chocolate' => 'it\'s alright', + 'Vanilla' => 'ew' +)); + +$run->pushHandler($handler); // Example: tag all frames with a comment $run->pushHandler(function($exception, $inspector, $run) { diff --git a/src/Damnit/Handler/PrettyPageHandler.php b/src/Damnit/Handler/PrettyPageHandler.php index f6050e9..7c4edd8 100644 --- a/src/Damnit/Handler/PrettyPageHandler.php +++ b/src/Damnit/Handler/PrettyPageHandler.php @@ -15,6 +15,11 @@ class PrettyPageHandler extends Handler */ private $resourcesPath; + /** + * @var array[] + */ + private $extraTables = array(); + /** * @return int|null */ @@ -57,6 +62,9 @@ class PrettyPageHandler extends Handler ) ); + // Add extra entries to the "super" list of data tables: + $v->super = array_merge($v->super, $this->getDataTables()); + call_user_func(function() use($templateFile, $v) { // $e -> cleanup output $e = function($_) { return htmlspecialchars($_, ENT_QUOTES, 'UTF-8'); }; @@ -74,6 +82,35 @@ class PrettyPageHandler extends Handler return Handler::LAST_HANDLER; } + /** + * Adds an entry to the list of superglobals displayed in the template. + * The expected data is a simple associative array. Any nested arrays + * will be flattened with print_r + * @param string $label + * @param array $data + */ + public function addDataTable($label, array $data) + { + $this->extraTables[$label] = $data; + } + + /** + * Returns all the extra data tables registered with this handler. + * Optionally accepts a 'label' parameter, to only return the data + * table under that label. + * @param string|null $label + * @return array[] + */ + public function getDataTables($label = null) + { + if($label !== null) { + return isset($this->extraTables[$label]) ? + $this->extraTables[$labe] : array(); + } + + return $this->extraTables; + } + /** * @return string */