Skip to content

[Bridge][Twig] Optionally pass dumper into DumpExtension #18466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
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()
Expand Down Expand Up @@ -67,11 +69,14 @@ public function dump(\Twig_Environment $env, $context)
}

$dump = fopen('php://memory', 'r+b');
$dumper = new HtmlDumper($dump);
$prevOutput = $this->dumper->setOutput($dump);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setOutput() method is not part of the DataDumperInterface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch...that's an oversight.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you recommend? Should I require AbstractDumper in constructor? I don't think it would be ok to assume the dumper has null output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setOutput, you could use dump's 2nd arg.
An you could also create a new interface to enforce what you need here.

Copy link
Contributor Author

@CarsonF CarsonF Apr 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas Using the 2nd arg doesn't work as it resets the header sent flag which causes tests to fail (due to recursion I'm assuming).

Even now isn't ideal with the header being dumped once per twig call. Thoughts?

Maybe we set the header to empty string after the first dump call? .... Hmm or maybe not. I guess we can't guarantee that that twig block will get into the actual html page.


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);
Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -103,4 +104,39 @@ public function getDumpArgs()
),
);
}

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.');
}
}