diff --git a/src/Symfony/Bridge/Twig/Extension/DumpExtension.php b/src/Symfony/Bridge/Twig/Extension/DumpExtension.php index 6c7a9c9a345f4..02cf782592522 100644 --- a/src/Symfony/Bridge/Twig/Extension/DumpExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/DumpExtension.php @@ -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. @@ -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()); + } else { + $this->dumper = $dumper; + } } public function getFunctions() @@ -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; } } diff --git a/src/Symfony/Component/VarDumper/CHANGELOG.md b/src/Symfony/Component/VarDumper/CHANGELOG.md index 6b08aa77ac7b1..5732eca5a2a4e 100644 --- a/src/Symfony/Component/VarDumper/CHANGELOG.md +++ b/src/Symfony/Component/VarDumper/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.2.0 +----- + + * added `ToStringDumper` + 2.7.0 ----- diff --git a/src/Symfony/Component/VarDumper/Dumper/ToStringDumper.php b/src/Symfony/Component/VarDumper/Dumper/ToStringDumper.php new file mode 100644 index 0000000000000..f46923ef3d749 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Dumper/ToStringDumper.php @@ -0,0 +1,47 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Symfony/Component/VarDumper/Tests/Dumper/ToStringDumperTest.php b/src/Symfony/Component/VarDumper/Tests/Dumper/ToStringDumperTest.php new file mode 100644 index 0000000000000..876815018d18f --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Dumper/ToStringDumperTest.php @@ -0,0 +1,45 @@ + + * + * 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 + */ +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)))); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy