Completed
Push — master ( fa856c...54ad29 )
by Greg
04:32
created

VarDumpFormatter::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 10
nc 2
nop 3
1
<?php
2
3
namespace Consolidation\OutputFormatters\Formatters;
4
5
use Consolidation\OutputFormatters\Options\FormatterOptions;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Output\StreamOutput;
8
use Symfony\Component\VarDumper\Cloner\VarCloner;
9
use Symfony\Component\VarDumper\Dumper\CliDumper;
10
11
/**
12
 * Var_dump formatter
13
 *
14
 * Run provided data through Symfony VarDumper component.
15
 */
16
class VarDumpFormatter implements FormatterInterface
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function write(OutputInterface $output, $data, FormatterOptions $options)
22
    {
23
        $dumper = new CliDumper();
24
        $cloned_data = (new VarCloner())->cloneVar($data);
25
26
        if ($output instanceof StreamOutput) {
27
            // When stream output is used the dumper is smart enough to
28
            // determine whether or not to apply colors to the dump.
29
            // @see Symfony\Component\VarDumper\Dumper\CliDumper::supportsColors
30
            $dumper->dump($cloned_data, $output->getStream());
31
        } else {
32
            // @todo Use dumper return value to get output once we stop support
33
            // VarDumper v2.
34
            $stream = fopen('php://memory', 'r+b');
35
            $dumper->dump($cloned_data, $stream);
36
            $output->writeln(stream_get_contents($stream, -1, 0));
37
            fclose($stream);
38
        }
39
    }
40
}
41