[Bridge][Twig] Optionally pass dumper into DumpExtension
This commit is contained in:
parent
45b557a5a7
commit
d8c0f1df1a
|
@ -23,10 +23,12 @@ use Symfony\Component\VarDumper\Dumper\HtmlDumper;
|
|||
class DumpExtension extends \Twig_Extension
|
||||
{
|
||||
private $cloner;
|
||||
private $dumper;
|
||||
|
||||
public function __construct(ClonerInterface $cloner)
|
||||
public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
|
||||
{
|
||||
$this->cloner = $cloner;
|
||||
$this->dumper = $dumper ?: new HtmlDumper();
|
||||
}
|
||||
|
||||
public function getFunctions()
|
||||
|
@ -67,11 +69,14 @@ class DumpExtension extends \Twig_Extension
|
|||
}
|
||||
|
||||
$dump = fopen('php://memory', 'r+b');
|
||||
$dumper = new HtmlDumper($dump);
|
||||
$prevOutput = $this->dumper->setOutput($dump);
|
||||
|
||||
foreach ($vars as $value) {
|
||||
$dumper->dump($this->cloner->cloneVar($value));
|
||||
$this->dumper->dump($this->cloner->cloneVar($value));
|
||||
}
|
||||
|
||||
$this->dumper->setOutput($prevOutput);
|
||||
|
||||
rewind($dump);
|
||||
|
||||
return stream_get_contents($dump);
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Symfony\Bridge\Twig\Tests\Extension;
|
||||
|
||||
use Symfony\Bridge\Twig\Extension\DumpExtension;
|
||||
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
||||
|
||||
|
@ -103,4 +104,39 @@ class DumpExtensionTest extends \PHPUnit_Framework_TestCase
|
|||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testCustomDumper()
|
||||
{
|
||||
$output = '';
|
||||
$lineDumper = function ($line) use (&$output) {
|
||||
$output .= $line;
|
||||
};
|
||||
|
||||
$dumper = new HtmlDumper($lineDumper);
|
||||
|
||||
$dumper->setDumpHeader('');
|
||||
$dumper->setDumpBoundaries(
|
||||
'<pre class=sf-dump-test id=%s data-indent-pad="%s">',
|
||||
'</pre><script>Sfdump("%s")</script>'
|
||||
);
|
||||
$extension = new DumpExtension(new VarCloner(), $dumper);
|
||||
$twig = new \Twig_Environment($this->getMock('Twig_LoaderInterface'), array(
|
||||
'debug' => true,
|
||||
'cache' => false,
|
||||
'optimizations' => 0,
|
||||
));
|
||||
|
||||
$dump = $extension->dump($twig, array(), 'foo');
|
||||
$dump = preg_replace('/sf-dump-\d+/', 'sf-dump', $dump);
|
||||
|
||||
$this->assertEquals(
|
||||
'<pre class=sf-dump-test id=sf-dump data-indent-pad=" ">"'.
|
||||
"<span class=sf-dump-str title=\"3 characters\">foo</span>\"\n".
|
||||
"</pre><script>Sfdump(\"sf-dump\")</script>\n",
|
||||
$dump,
|
||||
'Custom dumper should be used to dump data.'
|
||||
);
|
||||
|
||||
$this->assertEmpty($output, 'Dumper output should be ignored.');
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue