Skip to content

[POC] [VarDumper] Added StringDumper to export dump as string #19616

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 10 additions & 9 deletions src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Dumper\ToStringDumper;

/**
* Provides integration of the dump() function with Twig.
Expand All @@ -28,7 +29,12 @@ class DumpExtension extends \Twig_Extension
public function __construct(ClonerInterface $cloner, HtmlDumper $dumper = null)
{
$this->cloner = $cloner;
$this->dumper = $dumper ?: new HtmlDumper();

if (null === $dumper || !$dumper instanceof ToStringDumper) {
$this->dumper = new ToStringDumper(new HtmlDumper());
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we wrap passed dumper instead of silent replace it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, that's a typo

} else {
$this->dumper = $dumper;
}
}

public function getFunctions()
Expand Down Expand Up @@ -68,17 +74,12 @@ public function dump(\Twig_Environment $env, $context)
unset($vars[0], $vars[1]);
}

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

foreach ($vars as $value) {
$this->dumper->dump($this->cloner->cloneVar($value));
$dump .= $this->dumper->dump($this->cloner->cloneVar($value));
}

$this->dumper->setOutput($prevOutput);

rewind($dump);

return stream_get_contents($dump);
return $dump;
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/VarDumper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.2.0
-----

* added `ToStringDumper`

2.7.0
-----

Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/VarDumper/Dumper/ToStringDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Dumper;

use Symfony\Component\VarDumper\Cloner\Data;

/**
* A dumper decorator to return the dump as string.
*
* @author Wouter de Jong <wouter@wouterj.nl>
*/
class ToStringDumper implements DataDumperInterface
{
private $dumper;

public function __construct(AbstractDumper $dumper)
{
$this->dumper = $dumper;
}

/**
* {@inheritdoc}
*
* @return string
*/
public function dump(Data $data)
{
$dump = fopen('php://memory', 'r+b');
$prevOutput = $this->dumper->setOutput($dump);

$this->dumper->dump($data);

$this->dumper->setOutput($prevOutput);
rewind($dump);

return stream_get_contents($dump);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarDumper\Tests\Dumper;

use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ToStringDumper;
use Symfony\Component\VarDumper\Dumper\CliDumper;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ToStringDumperTest extends \PHPUnit_Framework_TestCase
{
public function testDecoratesDump()
{
$data = new Data(array());
$stubDumper = $this->getMock('Symfony\Component\VarDumper\Dumper\AbstractDumper');
$stubDumper->expects($this->exactly(2))->method('setOutput');

$dumper = new ToStringDumper($stubDumper);
$stubDumper->expects($this->once())->method('dump')->with($data);

$dumper->dump($data);
}

public function testReturnsStreamContents()
{
$innerDumper = new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY);
$cloner = new VarCloner();

$dumper = new ToStringDumper($innerDumper);

$this->assertEquals("[\n \"a\" => 1\n 0 => 2\n]\n", $dumper->dump($cloner->cloneVar(array('a' => 1, 2))));
}
}