From 8cf3d69ce518fdbd5f2fc3290985ea361f6fde5a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 10 Nov 2014 16:30:03 +0100 Subject: [PATCH] [TwigBundle/DebugBundle] move dump extension & cleanups --- .../Bridge/Twig/Extension/DumpExtension.php | 17 ++++++++--------- .../DebugBundle/Resources/config/services.xml | 5 +++++ .../TwigBundle/Resources/config/debug.xml | 3 +-- .../DataCollector/DumpDataCollector.php | 19 ++++++++----------- .../VarDumper/Dumper/AbstractDumper.php | 6 +++--- .../Component/VarDumper/Dumper/HtmlDumper.php | 6 ++++-- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Extension/DumpExtension.php b/src/Symfony/Bridge/Twig/Extension/DumpExtension.php index 2fc02f3302..30318ecac6 100644 --- a/src/Symfony/Bridge/Twig/Extension/DumpExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/DumpExtension.php @@ -22,7 +22,9 @@ use Symfony\Component\VarDumper\Dumper\HtmlDumper; */ class DumpExtension extends \Twig_Extension { - public function __construct(ClonerInterface $cloner = null) + private $cloner; + + public function __construct(ClonerInterface $cloner) { $this->cloner = $cloner; } @@ -46,7 +48,7 @@ class DumpExtension extends \Twig_Extension public function dump(\Twig_Environment $env, $context) { - if (!$env->isDebug() || !$this->cloner) { + if (!$env->isDebug()) { return; } @@ -64,17 +66,14 @@ class DumpExtension extends \Twig_Extension unset($vars[0], $vars[1]); } - $html = ''; - $dumper = new HtmlDumper(function ($line, $depth) use (&$html) { - if (-1 !== $depth) { - $html .= str_repeat(' ', $depth).$line."\n"; - } - }); + $dump = fopen('php://memory', 'r+b'); + $dumper = new HtmlDumper($dump); foreach ($vars as $value) { $dumper->dump($this->cloner->cloneVar($value)); } + rewind($dump); - return $html; + return stream_get_contents($dump); } } diff --git a/src/Symfony/Bundle/DebugBundle/Resources/config/services.xml b/src/Symfony/Bundle/DebugBundle/Resources/config/services.xml index 0593cc7efa..14da81c3dd 100644 --- a/src/Symfony/Bundle/DebugBundle/Resources/config/services.xml +++ b/src/Symfony/Bundle/DebugBundle/Resources/config/services.xml @@ -5,6 +5,11 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + + + + + diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml index 70bdafa77c..c44ae63121 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml @@ -16,9 +16,8 @@ - + - diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php index ba19540585..be3cfdc400 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php @@ -155,24 +155,21 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1) { + $data = fopen('php://memory' 'r+b'); + if ('html' === $format) { - $dumper = new HtmlDumper(); + $dumper = new HtmlDumper($data); } else { throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format)); } $dumps = array(); foreach ($this->data as $dump) { - $data = ''; - $dumper->dump( - $dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth), - function ($line, $depth) use (&$data) { - if (-1 !== $depth) { - $data .= str_repeat(' ', $depth).$line."\n"; - } - } - ); - $dump['data'] = $data; + $dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth)); + rewind($data); + $dump['data'] = stream_get_contents($data); + ftruncate($data, 0); + rewind($data); $dumps[] = $dump; } diff --git a/src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php b/src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php index 3b6fbac631..bc793499ac 100644 --- a/src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php @@ -115,7 +115,7 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface */ protected function dumpLine($depth) { - call_user_func($this->lineDumper, $this->line, $depth); + call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad); $this->line = ''; } @@ -125,10 +125,10 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface * @param string $line The line to write. * @param int $depth The recursive depth in the dumped structure. */ - protected function echoLine($line, $depth) + protected function echoLine($line, $depth, $indentPad) { if (-1 !== $depth) { - fwrite($this->outputStream, str_repeat($this->indentPad, $depth).$line."\n"); + fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n"); } } } diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php index 30c3253129..b587e78f91 100644 --- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php @@ -51,9 +51,11 @@ class HtmlDumper extends CliDumper */ public function setOutput($output) { - $this->headerIsDumped = false; + if ($output !== $prev = parent::setOutput($output)) { + $this->headerIsDumped = false; + } - return parent::setOutput($output); + return $prev; } /**