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
{
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);
}
}

View File

@ -5,6 +5,11 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<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">
<tag name="data_collector" id="dump" template="@Debug/Profiler/dump.html.twig" />
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />

View File

@ -16,9 +16,8 @@
<argument type="service" id="debug.stopwatch" />
</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" />
<argument type="service" id="var_dumper.cloner" on-invalid="null" />
</service>
</services>
</container>

View File

@ -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;
}

View File

@ -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");
}
}
}

View File

@ -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;
}
/**