Skip to content

Commit

Permalink
Dumper: dumps Closure as PHP code
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 5, 2021
1 parent 2ea0c5f commit 59bb35e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/PhpGenerator/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public function dump($var, int $column = 0): string

private function dumpVar(&$var, array $parents = [], int $level = 0, int $column = 0): string
{
if ($var instanceof Literal) {
return ltrim(Nette\Utils\Strings::indent(trim((string) $var), $level), "\t");

} elseif ($var === null) {
if ($var === null) {
return 'null';

} elseif (is_string($var)) {
Expand All @@ -50,6 +47,9 @@ private function dumpVar(&$var, array $parents = [], int $level = 0, int $column
return $this->dumpArray($var, $parents, $level, $column);

} elseif (is_object($var)) {
if ($var instanceof Literal || $var instanceof Closure) {
return ltrim(Nette\Utils\Strings::indent(trim((string) $var), $level), "\t");
}
return $this->dumpObject($var, $parents, $level);

} elseif (is_resource($var)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PhpGenerator/Dumper.dump().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ Assert::same("'He\\llo'", $dumper->dump('He\llo'));
Assert::same('\'He\ll\\\\\o \\\'wor\\\\\\\'ld\\\\\'', $dumper->dump('He\ll\\\o \'wor\\\'ld\\'));
Assert::same('[]', $dumper->dump([]));

// internal classes
Assert::same('[$s]', $dumper->dump([new PhpLiteral('$s')]));
same('[
function () {
return 1;
},
]', $dumper->dump([(new Nette\PhpGenerator\Closure)->setBody('return 1;')]));

Assert::same('[1, 2, 3]', $dumper->dump([1, 2, 3]));
Assert::same("['a']", $dumper->dump(['a']));
Expand Down

0 comments on commit 59bb35e

Please sign in to comment.