Skip to content

[VarDumper][CliDumper][WIP] Add a way to export the HTML dump of the dumped var to view it in the browser #22987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\VarDumper\Dumper;

use Symfony\Component\VarDumper\Cloner\Cursor;
use Symfony\Component\VarDumper\Cloner\Data;

/**
* CliDumper dumps variables for command line output.
Expand Down Expand Up @@ -51,6 +52,26 @@ class CliDumper extends AbstractDumper
"\033" => '\e',
);

/**
* @var HtmlDumper
*/
private $htmlDumper;

/**
* @var bool
*/
private $htmlDumpEnabled;

/**
* @var string|null
*/
private $htmlDumpDirectoryPath;

/**
* @var string|null
*/
private $htmlDumpBaseUrl;

/**
* {@inheritdoc}
*/
Expand All @@ -72,6 +93,9 @@ public function __construct($output = null, $charset = null, $flags = 0)
'index' => '34',
));
}

$this->htmlDumper = new HtmlDumper($output, $charset, $flags);
$this->htmlDumpEnabled = false;
}

/**
Expand Down Expand Up @@ -287,6 +311,91 @@ public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
$this->endValue($cursor);
}

/**
* {@inheritdoc}
*/
public function dump(Data $data, $output = null)
{
$result = parent::dump($data, $output);

if (!$this->htmlDumpEnabled) {
return $result;
}

// prevent infinite loop and segmentation fault because HtmlDumper extends CliDumper
if (CliDumper::class !== get_called_class()) {
return $result;
}

if (null === $this->htmlDumpDirectoryPath) {
throw new \RuntimeException('The directory path used to store html dumps was not set.');
}

if (!is_dir($this->htmlDumpDirectoryPath)) {
throw new \RuntimeException('The directory path used to store html dumps does not exist.');
}

if (null === $this->htmlDumpBaseUrl) {
throw new \RuntimeException('The base url used to view html dumps was not set.');
}

$dumpFilename = sprintf('%s.html', uniqid('dump_'));
if (false === file_put_contents(sprintf('%s/%s', $this->htmlDumpDirectoryPath, $dumpFilename), $this->htmlDumper->dump($data, true))) {
throw new \RuntimeException(sprintf('The html dump could not be saved in the "%s" directory path.', $this->htmlDumpDirectoryPath));
}

$this->line = sprintf("\n\033[38m%s\033[m", sprintf("HTML dump url :\n%s/%s\n", $this->htmlDumpBaseUrl, $dumpFilename));
parent::dumpLine(0);

return $result;
}

/**
* @param string|null $htmlDumpDirectoryPath
* @param string|null $htmlDumpBaseUrl
*/
public function enableHtmlDump($htmlDumpDirectoryPath = null, $htmlDumpBaseUrl = null)
{
$this->htmlDumpEnabled = true;

if (null !== $htmlDumpDirectoryPath) {
$this->setHtmlDumpDirectoryPath($htmlDumpDirectoryPath);
}

if (null !== $htmlDumpBaseUrl) {
$this->setHtmlDumpBaseUrl($htmlDumpBaseUrl);
}
}

public function disableHtmlDump()
{
$this->htmlDumpEnabled = false;
}

/**
* @param string $htmlDumpDirectoryPath
*/
public function setHtmlDumpDirectoryPath($htmlDumpDirectoryPath)
{
if (!is_string($htmlDumpDirectoryPath)) {
throw new \InvalidArgumentException('The html dump directory path must be a string.');
}

$this->htmlDumpDirectoryPath = $htmlDumpDirectoryPath;
}

/**
* @param string $htmlDumpBaseUrl
*/
public function setHtmlDumpBaseUrl($htmlDumpBaseUrl)
{
if (!is_string($htmlDumpBaseUrl)) {
throw new \InvalidArgumentException('The html dump base url must be a string.');
}

$this->htmlDumpBaseUrl = $htmlDumpBaseUrl;
}

/**
* Dumps an ellipsis for cut children.
*
Expand Down