Skip to content

Commit 541170f

Browse files
committed
Dumper: added $indentation
1 parent 80d0c0b commit 541170f

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ final class Dumper
2525
/** @var int */
2626
public $wrapLength = 120;
2727

28+
/** @var string */
29+
public $indentation = "\t";
30+
2831

2932
/**
3033
* Returns a PHP representation of a variable.
@@ -92,7 +95,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
9295
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
9396
}
9497

95-
$space = str_repeat("\t", $level);
98+
$space = str_repeat($this->indentation, $level);
9699
$outInline = '';
97100
$outWrapped = "\n$space";
98101
$parents[] = $var;
@@ -106,7 +109,7 @@ private function dumpArray(array &$var, array $parents, int $level, int $column)
106109
$counter = is_int($k) ? max($k + 1, $counter) : $counter;
107110
$outInline .= ($outInline === '' ? '' : ', ') . $keyPart;
108111
$outInline .= $this->dumpVar($v, $parents, 0, $column + strlen($outInline));
109-
$outWrapped .= "\t"
112+
$outWrapped .= $this->indentation
110113
. $keyPart
111114
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
112115
. ",\n$space";
@@ -145,7 +148,7 @@ private function dumpObject(&$var, array $parents, int $level): string
145148
}
146149

147150
$arr = (array) $var;
148-
$space = str_repeat("\t", $level);
151+
$space = str_repeat($this->indentation, $level);
149152

150153
if ($level > $this->maxDepth || in_array($var, $parents ?? [], true)) {
151154
throw new Nette\InvalidArgumentException('Nesting level too deep or recursive dependency.');
@@ -161,7 +164,7 @@ private function dumpObject(&$var, array $parents, int $level): string
161164

162165
foreach ($arr as $k => &$v) {
163166
if (!isset($props) || isset($props[$k])) {
164-
$out .= "$space\t"
167+
$out .= $space . $this->indentation
165168
. ($keyPart = $this->dumpVar($k) . ' => ')
166169
. $this->dumpVar($v, $parents, $level + 1, strlen($keyPart))
167170
. ",\n";
@@ -178,8 +181,8 @@ private function dumpObject(&$var, array $parents, int $level): string
178181

179182
private function dumpLiteral(Literal $var, int $level): string
180183
{
181-
$s = Nette\Utils\Strings::indent(trim((string) $var), $level, "\t");
182-
return ltrim($s, "\t");
184+
$s = Nette\Utils\Strings::indent(trim((string) $var), $level, $this->indentation);
185+
return ltrim($s, $this->indentation);
183186
}
184187

185188

@@ -229,7 +232,8 @@ private function dumpArguments(array &$var, int $column, bool $named): string
229232
$k = !$named || is_int($k) ? '' : $k . ': ';
230233
$outInline .= $outInline === '' ? '' : ', ';
231234
$outInline .= $k . $this->dumpVar($v, [$var], 0, $column + strlen($outInline));
232-
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n\t" . $k . $this->dumpVar($v, [$var], 1);
235+
$outWrapped .= ($outWrapped === '' ? '' : ',') . "\n"
236+
. $this->indentation . $k . $this->dumpVar($v, [$var], 1);
233237
}
234238

235239
return count($var) > 1 && (strpos($outInline, "\n") !== false || $column + strlen($outInline) > $this->wrapLength)

src/PhpGenerator/Printer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ protected function indent(string $s): string
279279

280280
protected function dump($var, int $column = 0): string
281281
{
282+
$this->dumper->indentation = $this->indentation;
282283
return $this->dumper->dump($var, $column);
283284
}
284285

@@ -369,6 +370,7 @@ private function printAttributes(array $attrs, bool $inline = false): string
369370
if (!$attrs) {
370371
return '';
371372
}
373+
$this->dumper->indentation = $this->indentation;
372374
$items = [];
373375
foreach ($attrs as $attr) {
374376
$args = $this->dumper->format('...?:', $attr->getArguments());

tests/PhpGenerator/Dumper.dump().indent.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
declare(strict_types=1);
88

99
use Nette\PhpGenerator\Dumper;
10+
use Nette\PhpGenerator\Literal;
1011
use Tester\Assert;
1112

1213

@@ -38,3 +39,21 @@ same('[
3839
2,
3940
3,
4041
]', $dumper->dump([8 => 1, 2, 3], $dumper->wrapLength - 13));
42+
43+
44+
$dumper = new Dumper;
45+
$dumper->indentation = ' ';
46+
same('[
47+
1,
48+
2,
49+
3,
50+
]', $dumper->dump([1, 2, 3], $dumper->wrapLength - 8));
51+
52+
same(
53+
"[
54+
'multi' => [
55+
1,
56+
],
57+
]",
58+
$dumper->dump(['multi' => new Literal("[\n1,\n]\n")])
59+
);

0 commit comments

Comments
 (0)