bug #12446 [Twig/DebugBundle] move dump extension registration (nicolas-grekas)

This PR was submitted for the master branch but it was merged into the 2.6 branch instead (closes #12446).

Discussion
----------

[Twig/DebugBundle] move dump extension registration

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #12440
| License       | MIT
| Doc PR        | -

An alternative fix for #12440

Commits
-------

8cf3d69 [TwigBundle/DebugBundle] move dump extension & cleanups
This commit is contained in:
Fabien Potencier 2014-11-18 11:08:24 +01:00
commit 018fc153b9
6 changed files with 29 additions and 27 deletions

View File

@ -22,7 +22,9 @@ use Symfony\Component\VarDumper\Dumper\HtmlDumper;
*/ */
class DumpExtension extends \Twig_Extension class DumpExtension extends \Twig_Extension
{ {
public function __construct(ClonerInterface $cloner = null) private $cloner;
public function __construct(ClonerInterface $cloner)
{ {
$this->cloner = $cloner; $this->cloner = $cloner;
} }
@ -46,7 +48,7 @@ class DumpExtension extends \Twig_Extension
public function dump(\Twig_Environment $env, $context) public function dump(\Twig_Environment $env, $context)
{ {
if (!$env->isDebug() || !$this->cloner) { if (!$env->isDebug()) {
return; return;
} }
@ -64,17 +66,14 @@ class DumpExtension extends \Twig_Extension
unset($vars[0], $vars[1]); unset($vars[0], $vars[1]);
} }
$html = ''; $dump = fopen('php://memory', 'r+b');
$dumper = new HtmlDumper(function ($line, $depth) use (&$html) { $dumper = new HtmlDumper($dump);
if (-1 !== $depth) {
$html .= str_repeat(' ', $depth).$line."\n";
}
});
foreach ($vars as $value) { foreach ($vars as $value) {
$dumper->dump($this->cloner->cloneVar($value)); $dumper->dump($this->cloner->cloneVar($value));
} }
rewind($dump);
return $html; return stream_get_contents($dump);
} }
} }

View File

@ -5,6 +5,11 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services> <services>
<service id="twig.extension.dump" class="Symfony\Bridge\Twig\Extension\DumpExtension" public="false">
<tag name="twig.extension" />
<argument type="service" id="var_dumper.cloner" />
</service>
<service id="data_collector.dump" class="Symfony\Component\HttpKernel\DataCollector\DumpDataCollector"> <service id="data_collector.dump" class="Symfony\Component\HttpKernel\DataCollector\DumpDataCollector">
<tag name="data_collector" id="dump" template="@Debug/Profiler/dump.html.twig" /> <tag name="data_collector" id="dump" template="@Debug/Profiler/dump.html.twig" />
<argument type="service" id="debug.stopwatch" on-invalid="ignore" /> <argument type="service" id="debug.stopwatch" on-invalid="ignore" />

View File

@ -16,9 +16,8 @@
<argument type="service" id="debug.stopwatch" /> <argument type="service" id="debug.stopwatch" />
</service> </service>
<service id="twig.extension.dump" class="Symfony\Bridge\Twig\Extension\DumpExtension" public="false"> <service id="twig.extension.debug" class="Twig_Extension_Debug" public="false">
<tag name="twig.extension" /> <tag name="twig.extension" />
<argument type="service" id="var_dumper.cloner" on-invalid="null" />
</service> </service>
</services> </services>
</container> </container>

View File

@ -155,24 +155,21 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1) public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
{ {
$data = fopen('php://memory' 'r+b');
if ('html' === $format) { if ('html' === $format) {
$dumper = new HtmlDumper(); $dumper = new HtmlDumper($data);
} else { } else {
throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format)); throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
} }
$dumps = array(); $dumps = array();
foreach ($this->data as $dump) { foreach ($this->data as $dump) {
$data = ''; $dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth));
$dumper->dump( rewind($data);
$dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth), $dump['data'] = stream_get_contents($data);
function ($line, $depth) use (&$data) { ftruncate($data, 0);
if (-1 !== $depth) { rewind($data);
$data .= str_repeat(' ', $depth).$line."\n";
}
}
);
$dump['data'] = $data;
$dumps[] = $dump; $dumps[] = $dump;
} }

View File

@ -115,7 +115,7 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
*/ */
protected function dumpLine($depth) protected function dumpLine($depth)
{ {
call_user_func($this->lineDumper, $this->line, $depth); call_user_func($this->lineDumper, $this->line, $depth, $this->indentPad);
$this->line = ''; $this->line = '';
} }
@ -125,10 +125,10 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
* @param string $line The line to write. * @param string $line The line to write.
* @param int $depth The recursive depth in the dumped structure. * @param int $depth The recursive depth in the dumped structure.
*/ */
protected function echoLine($line, $depth) protected function echoLine($line, $depth, $indentPad)
{ {
if (-1 !== $depth) { if (-1 !== $depth) {
fwrite($this->outputStream, str_repeat($this->indentPad, $depth).$line."\n"); fwrite($this->outputStream, str_repeat($indentPad, $depth).$line."\n");
} }
} }
} }

View File

@ -51,9 +51,11 @@ class HtmlDumper extends CliDumper
*/ */
public function setOutput($output) public function setOutput($output)
{ {
$this->headerIsDumped = false; if ($output !== $prev = parent::setOutput($output)) {
$this->headerIsDumped = false;
}
return parent::setOutput($output); return $prev;
} }
/** /**