4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\VarDumper;
14 use Symfony\Component\Console\Formatter\OutputFormatter;
15 use Symfony\Component\VarDumper\Caster\Caster;
16 use Symfony\Component\VarDumper\Cloner\Stub;
19 * A Presenter service.
27 private $exceptionsImportants = array(
32 "\0Exception\0previous",
34 private $styles = array(
42 'protected' => 'protected',
43 'private' => 'private',
49 public function __construct(OutputFormatter $formatter)
51 $this->dumper = new Dumper($formatter);
52 $this->dumper->setStyles($this->styles);
54 $this->cloner = new Cloner();
55 $this->cloner->addCasters(array('*' => function ($obj, array $a, Stub $stub, $isNested, $filter = 0) {
56 if ($filter || $isNested) {
57 if ($obj instanceof \Exception) {
58 $a = Caster::filter($a, Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_EMPTY, $this->exceptionsImportants);
60 $a = Caster::filter($a, Caster::EXCLUDE_PROTECTED | Caster::EXCLUDE_PRIVATE);
71 * @see http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
73 * @param callable[] $casters A map of casters
75 public function addCasters(array $casters)
77 $this->cloner->addCasters($casters);
81 * Present a reference to the value.
87 public function presentRef($value)
89 return $this->present($value, 0);
93 * Present a full representation of the value.
95 * If $depth is 0, the value will be presented as a ref instead.
98 * @param int $depth (default: null)
99 * @param int $options One of Presenter constants
103 public function present($value, $depth = null, $options = 0)
105 $data = $this->cloner->cloneVar($value, !($options & self::VERBOSE) ? Caster::EXCLUDE_VERBOSE : 0);
107 if (null !== $depth) {
108 $data = $data->withMaxDepth($depth);
112 $this->dumper->dump($data, function ($line, $depth) use (&$output) {
114 if ('' !== $output) {
117 $output .= str_repeat(' ', $depth) . $line;
121 return OutputFormatter::escape($output);