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, $forceArrayIndexes = false)
51 // Work around https://github.com/symfony/symfony/issues/23572
52 $oldLocale = setlocale(LC_NUMERIC, 0);
53 setlocale(LC_NUMERIC, 'C');
55 $this->dumper = new Dumper($formatter, $forceArrayIndexes);
56 $this->dumper->setStyles($this->styles);
58 // Now put the locale back
59 setlocale(LC_NUMERIC, $oldLocale);
61 $this->cloner = new Cloner();
62 $this->cloner->addCasters(array('*' => function ($obj, array $a, Stub $stub, $isNested, $filter = 0) {
63 if ($filter || $isNested) {
64 if ($obj instanceof \Exception) {
65 $a = Caster::filter($a, Caster::EXCLUDE_NOT_IMPORTANT | Caster::EXCLUDE_EMPTY, $this->exceptionsImportants);
67 $a = Caster::filter($a, Caster::EXCLUDE_PROTECTED | Caster::EXCLUDE_PRIVATE);
78 * @see http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
80 * @param callable[] $casters A map of casters
82 public function addCasters(array $casters)
84 $this->cloner->addCasters($casters);
88 * Present a reference to the value.
94 public function presentRef($value)
96 return $this->present($value, 0);
100 * Present a full representation of the value.
102 * If $depth is 0, the value will be presented as a ref instead.
104 * @param mixed $value
105 * @param int $depth (default: null)
106 * @param int $options One of Presenter constants
110 public function present($value, $depth = null, $options = 0)
112 $data = $this->cloner->cloneVar($value, !($options & self::VERBOSE) ? Caster::EXCLUDE_VERBOSE : 0);
114 if (null !== $depth) {
115 $data = $data->withMaxDepth($depth);
118 // Work around https://github.com/symfony/symfony/issues/23572
119 $oldLocale = setlocale(LC_NUMERIC, 0);
120 setlocale(LC_NUMERIC, 'C');
123 $this->dumper->dump($data, function ($line, $depth) use (&$output) {
125 if ('' !== $output) {
128 $output .= str_repeat(' ', $depth) . $line;
132 // Now put the locale back
133 setlocale(LC_NUMERIC, $oldLocale);
135 return OutputFormatter::escape($output);