From f214edac7649bba5ca5f4e3c9146151b1b032b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Fri, 3 Oct 2014 10:06:07 +0200 Subject: [PATCH] [VarDumper] Added unique id for every single dump in html --- .../Tests/Extension/DumpExtensionTest.php | 1 + .../DataCollector/DumpDataCollectorTest.php | 19 +++++---- .../Component/VarDumper/Dumper/HtmlDumper.php | 41 ++++++++++++------- .../VarDumper/Tests/HtmlDumperTest.php | 22 +++++----- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php index 3de9a0e91eee7..35d51d3a4cd39 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php @@ -77,6 +77,7 @@ public function testDump($context, $args, $expectedOutput, $debug = true) if ($debug) { $this->assertStringStartsWith('\n", - 'name' => 'DumpDataCollectorTest.php', - 'file' => __FILE__, - 'line' => $line, - 'fileExcerpt' => false, + 'data' => "
123\n
\n", + 'name' => 'DumpDataCollectorTest.php', + 'file' => __FILE__, + 'line' => $line, + 'fileExcerpt' => false, ), ); - $dump = $collector->getDumps('html'); - $this->assertTrue(isset($dump[0]['data'])); - $dump[0]['data'] = preg_replace('/^.*?
assertSame($xDump, $dump);
 
         $this->assertStringStartsWith(
diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
index 6e740c9dcd15b..1f8d72892c397 100644
--- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
+++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
@@ -12,6 +12,7 @@
 namespace Symfony\Component\VarDumper\Dumper;
 
 use Symfony\Component\VarDumper\Cloner\Cursor;
+use Symfony\Component\VarDumper\Cloner\Data;
 
 /**
  * HtmlDumper dumps variables as HTML.
@@ -23,8 +24,9 @@ class HtmlDumper extends CliDumper
     public static $defaultOutputStream = 'php://output';
 
     protected $dumpHeader;
-    protected $dumpPrefix = '
';
+    protected $dumpPrefix = '
';
     protected $dumpSuffix = '
'; + protected $dumpId = 'sf-dump'; protected $colors = true; protected $headerIsDumped = false; protected $lastDepth = -1; @@ -82,6 +84,15 @@ public function setDumpBoundaries($prefix, $suffix) $this->dumpSuffix = $suffix; } + /** + * {@inheritdoc} + */ + public function dump(Data $data, $lineDumper = null) + { + $this->dumpId = 'sf-dump-'.mt_rand(); + parent::dump($data, $lineDumper); + } + /** * Dumps the HTML header. */ @@ -90,7 +101,7 @@ protected function getDumpHeader() $this->headerIsDumped = true; if (null !== $this->dumpHeader) { - return $this->dumpHeader; + return str_replace('%id%', $this->dumpId, $this->dumpHeader); } $line = <<<'EOHTML' @@ -129,7 +140,7 @@ protected function getDumpHeader() }; '.$this->dumpHeader; + $this->dumpHeader = preg_replace('/\s+/', ' ', $line).''.$this->dumpHeader; + + return str_replace('%id%', $this->dumpId, $this->dumpHeader); } /** @@ -201,9 +214,9 @@ protected function style($style, $val) if ('ref' === $style) { $ref = substr($val, 1); if ('#' === $val[0]) { - return "$val"; + return "dumpId}-ref$ref\">$val"; } else { - return "$val"; + return "dumpId}-ref$ref\">$val"; } } @@ -236,14 +249,14 @@ protected function dumpLine($depth) } if (-1 === $this->lastDepth) { - $this->line = $this->dumpPrefix.$this->line; + $this->line = str_replace('%id%', $this->dumpId, $this->dumpPrefix).$this->line; } if (!$this->headerIsDumped) { $this->line = $this->getDumpHeader().$this->line; } if (-1 === $depth) { - $this->line .= $this->dumpSuffix; + $this->line .= str_replace('%id%', $this->dumpId, $this->dumpSuffix); parent::dumpLine(0); } $this->lastDepth = $depth; diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index e93ff236c238c..82c41b4666dd6 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -33,7 +33,7 @@ public function testGet() unset($a['uri']); return $a; - } + }, )); $data = $cloner->cloneVar($var); @@ -44,12 +44,14 @@ public function testGet() $out = preg_replace('/[ \t]+$/m', '', $out); $var['file'] = htmlspecialchars($var['file'], ENT_QUOTES, 'UTF-8'); $intMax = PHP_INT_MAX; + preg_match('/sf-dump-(\\d{2,})/', $out, $matches); + $dumpId = $matches[1]; $this->assertSame( <<array:25 [ "number" => 1 - 0 => null #1 + 0 => null #1 "const" => 1.1 1 => true 2 => false @@ -72,7 +74,7 @@ public function testGet() options: [] } 8 => resource:Unknown {} - "obj" => DumbFoo { #2 + "obj" => DumbFoo { #2 foo: "foo" "bar": "bar" } @@ -90,15 +92,15 @@ public function testGet() } "line" => {$var['line']} "nobj" => array:1 [ - 0 => {} #3 + 0 => {} #3 ] - "recurs" => array:1 [ #4 - 0 => &4 array:1 [@4] + "recurs" => array:1 [ #4 + 0 => &4 array:1 [@4] ] - 9 => &1 null - "sobj" => DumbFoo {@2} - "snobj" => &3 {@3} - "snobj2" => {@3} + 9 => &1 null + "sobj" => DumbFoo {@2} + "snobj" => &3 {@3} + "snobj2" => {@3} "file" => "{$var['file']}" b"bin-key-é" => "" ]