diff --git a/README.md b/README.md index c817bc3..09b1f79 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,32 @@ include __DIR__ . '/src/VarDump.php'; // prints a variable d("any variable"); +// or multiple variables, as many as your memory can handle +d("any variable", "any other variable"); + // prints a variable and exits dd("any variable"); + +// or with multiple variables +dd("any variable", "any other variable"); + +// prints one variable with a specific dump configuration +$maxRecursiveDepth = 10; +$maxStringLength = 100; +$theme = new HulkHtmlVarDumpTheme(); + +dc("any variable", $maxRecursiveDepth, $maxStringLength, $theme); + +// as called for the sample the screenshots +d([ + null, + true, + 42, + 3.1415, + "any string", + new Exception(), + fopen('php://stdout', 'w'), +]); ``` ## Configuration @@ -33,15 +57,24 @@ $_ENV['VAR_DUMP_STRING_LENGTH'] = 100; // Name of the CLI theme class, only one implemented $_ENV['VAR_DUMP_THEME_CLI'] = 'CliVarDumpTheme'; -// Name of the Html theme class, choose between: +// Name of the HTML theme class, choose between: // - HtmlVarDumpTheme -// - BlueHtmlVarDumpTheme -// - RedHtmlVarDumpTheme, -// - GreenHtmlVarDumpTheme, -// - BrownHtmlVarDumpTheme, -$_ENV['VAR_DUMP_THEME_HTML'] = 'HtmlVarDumpTheme'; +// - BatmanHtmlVarDumpTheme +// - HulkHtmlVarDumpTheme, +// - IronmanHtmlVarDumpTheme, +// - SpidermanHtmlVarDumpTheme, +// - SupermanHtmlVarDumpTheme, +$_ENV['VAR_DUMP_THEME_HTML'] = 'SpidermanHtmlVarDumpTheme'; ``` +## Screenshots + +Output on a HTML page: +![Screenshot HTML](screenshot-html.png "Screenshot HTML") + +Output in a CLI: +![Screenshot CLI](screenshot-cli.png "Screenshot CLI") + ## Installation You can use [Composer](http://getcomposer.org) to install this helper into your project. diff --git a/screenshot-cli.png b/screenshot-cli.png new file mode 100644 index 0000000..8153efa Binary files /dev/null and b/screenshot-cli.png differ diff --git a/screenshot-html.png b/screenshot-html.png new file mode 100644 index 0000000..9ec0a0f Binary files /dev/null and b/screenshot-html.png differ diff --git a/src/VarDump.php b/src/VarDump.php index 72745d1..99f6acc 100644 --- a/src/VarDump.php +++ b/src/VarDump.php @@ -1,42 +1,60 @@ print($variable); + $variables = func_get_args(); + foreach ($variables as $variable) { + $varDump->print($variable); + } } - } -/** - * Shortcut to dump a value for debug purposes. This function will stop your - * script after the dump - * @param mixed $arg1 First value to print - * @param mixed $arg2 Second value to print - * @param mixed $arg3 ... - * @return null - */ -function dd() { - call_user_func_array('d', func_get_args()); +if (!function_exists('dd')) { + /** + * Shortcut to print a value for debug purposes. This function will stop your + * script after the print + * @param mixed $variable1 First variable to print + * @param mixed $variable2 Second variable to print + * @param mixed $variable3 ... + * @return null + */ + function dd() { + call_user_func_array('d', func_get_args()); - exit; + exit; + } +} + +if (!function_exists('dc')) { + /** + * Shortcut to print a value for debug purposes. + * @param mixed $variable Variable to print + * @param integer $recursiveDepth Maximum level of recursiveness + * @param integer $stringLength Maximum length for the preview of a string + * @param VarDumpTheme $theme Theme for the output + * @return null + */ + function dc($variable, $recursiveDepth = null, $stringLength = null, VarDumpTheme $theme = null) { + $varDump = new VarDump($recursiveDepth, $stringLength, $theme); + $varDump->print($variable); + } } /** - * Class to dump variables like vardump + * Class to print variables like var_dump */ class VarDump { @@ -81,7 +99,7 @@ public function setTheme(VarDumpTheme $theme = null) { if (php_sapi_name() === 'cli') { $themeName = isset($_ENV['VAR_DUMP_THEME_CLI']) ? $_ENV['VAR_DUMP_THEME_CLI'] : 'CliVarDumpTheme'; } else { - $themeName = isset($_ENV['VAR_DUMP_THEME_HTML']) ? $_ENV['VAR_DUMP_THEME_HTML'] : 'HtmlVarDumpTheme'; + $themeName = isset($_ENV['VAR_DUMP_THEME_HTML']) ? $_ENV['VAR_DUMP_THEME_HTML'] : 'SpidermanHtmlVarDumpTheme'; } $theme = new $themeName(); @@ -99,6 +117,8 @@ public function print($value) { $output = ''; if ($this->isFirst) { + $this->isFirst = false; + $output .= $this->theme->beforeFirstPrint(); } @@ -106,8 +126,6 @@ public function print($value) { $output .= $this->getValue($value); $output .= $this->theme->afterPrint(); - $this->isFirst = false; - echo $output; } @@ -128,8 +146,6 @@ private function getValue($value, $showType = true, $encode = true) { return $this->theme->formatValue('boolean', $value ? 'true' : 'false', null, $showType, $encode); case 'NULL': return $this->theme->formatValue(null, 'null', null, $showType, $encode); - case 'unknown type': - return $this->theme->formatValue('unknown', '???', null, $showType, $encode); case 'integer': case 'double': case 'resource': @@ -140,6 +156,8 @@ private function getValue($value, $showType = true, $encode = true) { return $this->getArrayValue($value, $showType, $encode); case 'object': return $this->getObjectValue($value); + default: + return $this->theme->formatValue('unknown', '???', null, $showType, $encode); } } @@ -185,11 +203,14 @@ private function getStringValue($string, $showType, $encode) { private function getArrayValue($array, $showType, $encode) { $numItems = count($array); if ($numItems == 0) { + // empty array return $this->theme->formatValue('array(0)', '[]', null, $showType, $encode); } elseif ($this->recursiveDepth == $this->recursiveMaximum) { + // too deep in recursiveness return $this->theme->formatValue('array(' . $numItems . ')', '[...]', null, $showType, $encode); } + // retrieve array dump $this->recursiveDepth++; $items = array(); @@ -221,7 +242,7 @@ private function getObjectValue($object) { return $this->theme->formatValue($className . '#' . $this->objectId, '{...}', null); } - // retrieve instance dump + // retrieve object instance dump $id = $this->objectId++; $this->recursiveDepth++; @@ -353,7 +374,7 @@ public function formatValue($type, $short, $full = null, $showType = true, $enco * @param string $value Formatted display of the value * @return string Output of the list item */ - public function formatListItem($key, $value); + public function formatListItem($key, $value = null); /** * Formats a list from items @@ -391,7 +412,7 @@ public function beforePrint($trace) { * @return string Output after the print */ public function afterPrint() { - return null; + return "\n"; } /** @@ -429,7 +450,7 @@ public function formatValue($type, $short, $full = null, $showType = true, $enco * @param string $value Formatted display of the value * @return string Output of the list item */ - public function formatListItem($key, $value) { + public function formatListItem($key, $value = null) { return '- ' . $key . ($value !== null ? ' => ' . $value : ''); } @@ -456,6 +477,24 @@ class HtmlVarDumpTheme implements VarDumpTheme { */ protected $colors; + /** + * Style definitions + * @var array + */ + protected $styles; + + /** + * Id of the print call + * @var integer + */ + private static $printId = 1; + + /** + * Id of the curent element + * @var integer + */ + private static $elementId = 1; + /** * Constructs a new HTML theme * @return null @@ -473,7 +512,7 @@ public function __construct() { } $this->styles = array( - 'container' => 'font-family: monospace; padding: 1em; margin: 1em; line-height: 1.5em; border: 1px solid ' . $this->colors['general-border'] . '; color: ' . $this->colors['general-text'] . '; background-color: ' . $this->colors['general-background'], + 'container' => 'font-family: monospace; padding: 1em; margin: 1em; line-height: 1.5em; border-radius: 5px; border: 1px solid ' . $this->colors['general-border'] . '; color: ' . $this->colors['general-text'] . '; background-color: ' . $this->colors['general-background'], 'trace' => 'font-size: 0.8em', 'link' => 'font-size: 0.8em; color: '. $this->colors['general-link'], 'code' => 'background-color: ' . $this->colors['code-background'] . '; color: ' . $this->colors['code-text'], @@ -482,9 +521,6 @@ public function __construct() { 'expand-string' => 'margin-left: 1.5em', 'expand-block' => 'display: none', ); - - $this->printId = 1; - $this->elementId = 1; } /** @@ -494,7 +530,7 @@ public function __construct() { public function beforeFirstPrint() { return '