1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Console\Helper; 13 14use Symfony\Component\Console\Output\OutputInterface; 15use Symfony\Component\VarDumper\Cloner\ClonerInterface; 16use Symfony\Component\VarDumper\Cloner\VarCloner; 17use Symfony\Component\VarDumper\Dumper\CliDumper; 18 19/** 20 * @author Roland Franssen <franssen.roland@gmail.com> 21 */ 22final class Dumper 23{ 24 private $output; 25 private $dumper; 26 private $cloner; 27 private $handler; 28 29 public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null) 30 { 31 $this->output = $output; 32 $this->dumper = $dumper; 33 $this->cloner = $cloner; 34 35 if (class_exists(CliDumper::class)) { 36 $this->handler = function ($var): string { 37 $dumper = $this->dumper ?? $this->dumper = new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR); 38 $dumper->setColors($this->output->isDecorated()); 39 40 return rtrim($dumper->dump(($this->cloner ?? $this->cloner = new VarCloner())->cloneVar($var)->withRefHandles(false), true)); 41 }; 42 } else { 43 $this->handler = function ($var): string { 44 switch (true) { 45 case null === $var: 46 return 'null'; 47 case true === $var: 48 return 'true'; 49 case false === $var: 50 return 'false'; 51 case \is_string($var): 52 return '"'.$var.'"'; 53 default: 54 return rtrim(print_r($var, true)); 55 } 56 }; 57 } 58 } 59 60 public function __invoke($var): string 61 { 62 return ($this->handler)($var); 63 } 64} 65