bug #12365 [VarDumper] polymorphic and stateless output selection (nicolas-grekas)
This PR was merged into the 2.6-dev branch. Discussion ---------- [VarDumper] polymorphic and stateless output selection | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | https://github.com/symfony/symfony-docs/pull/4243 While writing the documentation for the VarDumper component, it came to me that selecting the output destination was a state-full process for streams. This PR fixes that by allowing not only callbacks but also streams on the second argument of `AbstractDumper::dump()`. I also updated some interfaces to make the feature/names more straightforward. Seec578fe7962
for the related documentation update. Commits -------9b9fb5a
[VarDumper] polymorphic and stateless output selection
This commit is contained in:
commit
14d6587895
@ -21,7 +21,7 @@ use Symfony\Component\VarDumper\Cloner\DumperInterface;
|
||||
*/
|
||||
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
|
||||
{
|
||||
public static $defaultOutputStream = 'php://output';
|
||||
public static $defaultOutput = 'php://output';
|
||||
|
||||
protected $line = '';
|
||||
protected $lineDumper;
|
||||
@ -30,37 +30,39 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
|
||||
protected $indentPad = ' ';
|
||||
|
||||
/**
|
||||
* @param callable|resource|string|null $outputStream A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutputStream.
|
||||
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput.
|
||||
*/
|
||||
public function __construct($outputStream = null)
|
||||
public function __construct($output = null)
|
||||
{
|
||||
$this->decimalPoint = (string) 0.5;
|
||||
$this->decimalPoint = $this->decimalPoint[1];
|
||||
if (is_callable($outputStream)) {
|
||||
$this->setLineDumper($outputStream);
|
||||
} else {
|
||||
if (null === $outputStream) {
|
||||
$outputStream =& static::$defaultOutputStream;
|
||||
}
|
||||
if (is_string($outputStream)) {
|
||||
$outputStream = fopen($outputStream, 'wb');
|
||||
}
|
||||
$this->outputStream = $outputStream;
|
||||
$this->setLineDumper(array($this, 'echoLine'));
|
||||
$this->setOutput($output ?: static::$defaultOutput);
|
||||
if (!$output && is_string(static::$defaultOutput)) {
|
||||
static::$defaultOutput = $this->outputStream;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a line dumper callback.
|
||||
* Sets the output destination of the dumps.
|
||||
*
|
||||
* @param callable $callback A callback responsible for writing the dump, one line at a time.
|
||||
* @param callable|resource|string $output A line dumper callable, an opened stream or an output path.
|
||||
*
|
||||
* @return callable|null The previous line dumper.
|
||||
* @return callable|resource|string The previous output destination.
|
||||
*/
|
||||
public function setLineDumper($callback)
|
||||
public function setOutput($output)
|
||||
{
|
||||
$prev = $this->lineDumper;
|
||||
$this->lineDumper = $callback;
|
||||
$prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
|
||||
|
||||
if (is_callable($output)) {
|
||||
$this->outputStream = null;
|
||||
$this->lineDumper = $output;
|
||||
} else {
|
||||
if (is_string($output)) {
|
||||
$output = fopen($output, 'wb');
|
||||
}
|
||||
$this->outputStream = $output;
|
||||
$this->lineDumper = array($this, 'echoLine');
|
||||
}
|
||||
|
||||
return $prev;
|
||||
}
|
||||
@ -84,13 +86,13 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
|
||||
* Dumps a Data object.
|
||||
*
|
||||
* @param Data $data A Data object.
|
||||
* @param callable|null $lineDumper A callback for writing dump's lines.
|
||||
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path.
|
||||
*/
|
||||
public function dump(Data $data, $lineDumper = null)
|
||||
public function dump(Data $data, $output = null)
|
||||
{
|
||||
$exception = null;
|
||||
if ($lineDumper) {
|
||||
$prevLineDumper = $this->setLineDumper($lineDumper);
|
||||
if ($output) {
|
||||
$prevOutput = $this->setOutput($output);
|
||||
}
|
||||
try {
|
||||
$data->dump($this);
|
||||
@ -98,8 +100,8 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
|
||||
} catch (\Exception $exception) {
|
||||
// Re-thrown below
|
||||
}
|
||||
if ($lineDumper) {
|
||||
$this->setLineDumper($prevLineDumper);
|
||||
if ($output) {
|
||||
$this->setOutput($prevOutput);
|
||||
}
|
||||
if (null !== $exception) {
|
||||
throw $exception;
|
||||
|
@ -22,7 +22,7 @@ use Symfony\Component\VarDumper\Cloner\Cursor;
|
||||
class CliDumper extends AbstractDumper
|
||||
{
|
||||
public static $defaultColors;
|
||||
public static $defaultOutputStream = 'php://stdout';
|
||||
public static $defaultOutput = 'php://stdout';
|
||||
|
||||
protected $colors;
|
||||
protected $maxStringWidth = 0;
|
||||
@ -335,7 +335,7 @@ class CliDumper extends AbstractDumper
|
||||
*/
|
||||
protected function supportsColors()
|
||||
{
|
||||
if ($this->outputStream !== static::$defaultOutputStream) {
|
||||
if ($this->outputStream !== static::$defaultOutput) {
|
||||
return @(is_resource($this->outputStream) && function_exists('posix_isatty') && posix_isatty($this->outputStream));
|
||||
}
|
||||
if (null !== static::$defaultColors) {
|
||||
|
@ -21,7 +21,7 @@ use Symfony\Component\VarDumper\Cloner\Data;
|
||||
*/
|
||||
class HtmlDumper extends CliDumper
|
||||
{
|
||||
public static $defaultOutputStream = 'php://output';
|
||||
public static $defaultOutput = 'php://output';
|
||||
|
||||
protected $dumpHeader;
|
||||
protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
|
||||
@ -47,11 +47,11 @@ class HtmlDumper extends CliDumper
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setLineDumper($callback)
|
||||
public function setOutput($output)
|
||||
{
|
||||
$this->headerIsDumped = false;
|
||||
|
||||
return parent::setLineDumper($callback);
|
||||
return parent::setOutput($output);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,10 +88,10 @@ class HtmlDumper extends CliDumper
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function dump(Data $data, $lineDumper = null)
|
||||
public function dump(Data $data, $output = null)
|
||||
{
|
||||
$this->dumpId = 'sf-dump-'.mt_rand();
|
||||
parent::dump($data, $lineDumper);
|
||||
parent::dump($data, $output);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user