8000 [Bridge][Twig] Optionally pass dumper into DumpExtension · symfony/symfony@d8c0f1d · GitHub
[go: up one dir, main page]

Skip to content

Commit d8c0f1d

Browse files
committed
[Bridge][Twig] Optionally pass dumper into DumpExtension
1 parent 45b557a commit d8c0f1d

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Symfony/Bridge/Twig/Extension/DumpExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
class DumpExtension extends \Twig_Extension
2424
{
2525
private $cloner;
26+
private $dumper;
2627

27-
public function __construct(ClonerInterface $cloner)
28+
public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
2829
{
2930
$this->cloner = $cloner;
31+
$this->dumper = $dumper ?: new HtmlDumper();
3032
}
3133

3234
public function getFunctions()
@@ -67,11 +69,14 @@ public function dump(\Twig_Environment $env, $context)
6769
}
6870

6971
$dump = fopen('php://memory', 'r+b');
70-
$dumper = new HtmlDumper($dump);
72+
$prevOutput = $this->dumper->setOutput($dump);
7173

7274
foreach ($vars as $value) {
73-
$dumper->dump($this->cloner->cloneVar($value));
75+
$this->dumper->dump($this->cloner->cloneVar($value));
7476
}
77+
78+
$this->dumper->setOutput($prevOutput);
79+
7580
rewind($dump);
7681

7782
return stream_get_contents($dump);

src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

1414
use Symfony\Bridge\Twig\Extension\DumpExtension;
15+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
1516
use Symfony\Component\VarDumper\VarDumper;
1617
use Symfony\Component\VarDumper\Cloner\VarCloner;
1718

@@ -103,4 +104,39 @@ public function getDumpArgs()
103104
),
104105
);
105106
}
107+
108+
public function testCustomDumper()
109+
{
110+
$output = '';
111+
$lineDumper = function ($line) use (&$output) {
112+
$output .= $line;
113+
};
114+
115+
$dumper = new HtmlDumper($lineDumper);
116+
117+
$dumper->setDumpHeader('');
118+
$dumper->setDumpBoundaries(
119+
'<pre class=sf-dump-test id=%s data-indent-pad="%s">',
120+
'</pre><script>Sfdump("%s")</script>'
121+
);
122+
$extension = new DumpExtension(new VarCloner(), $dumper);
123+
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array(
124+
'debug' => true,
125+
'cache' => false,
126+
'optimizations' => 0,
127+
));
128+
129+
$dump = $extension->dump($twig, array(), 'foo');
130+
$dump = preg_replace('/sf-dump-\d+/', 'sf-dump', $dump);
131+
132+
$this->assertEquals(
133+
'<pre class=sf-dump-test id=sf-dump data-indent-pad=" ">"'.
134+
"<span class=sf-dump-str title=\"3 characters\">foo</span>\"\n".
135+
"</pre><script>Sfdump(\"sf-dump\")</script>\n",
136+
$dump,
137+
'Custom dumper should be used to dump data.'
138+
);
139+
140+
$this->assertEmpty($output, 'Dumper output should be ignored.');
141+
}
106142
}

0 commit comments

Comments
 (0)
0