Security update for Core, with self-updated composer
[yaffs-website] / vendor / psy / psysh / src / Psy / VarDumper / Presenter.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2017 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\VarDumper;
13
14 use Symfony\Component\Console\Formatter\OutputFormatter;
15 use Symfony\Component\VarDumper\Caster\Caster;
16 use Symfony\Component\VarDumper\Cloner\Stub;
17
18 /**
19  * A Presenter service.
20  */
21 class Presenter
22 {
23     const VERBOSE = 1;
24
25     private $cloner;
26     private $dumper;
27     private $exceptionsImportants = array(
28         "\0*\0message",
29         "\0*\0code",
30         "\0*\0file",
31         "\0*\0line",
32         "\0Exception\0previous",
33     );
34     private $styles = array(
35         'num'       => 'number',
36         'const'     => 'const',
37         'str'       => 'string',
38         'cchr'      => 'default',
39         'note'      => 'class',
40         'ref'       => 'default',
41         'public'    => 'public',
42         'protected' => 'protected',
43         'private'   => 'private',
44         'meta'      => 'comment',
45         'key'       => 'comment',
46         'index'     => 'number',
47     );
48
49     public function __construct(OutputFormatter $formatter, $forceArrayIndexes = false)
50     {
51         // Work around https://github.com/symfony/symfony/issues/23572
52         $oldLocale = setlocale(LC_NUMERIC, 0);
53         setlocale(LC_NUMERIC, 'C');
54
55         $this->dumper = new Dumper($formatter, $forceArrayIndexes);
56         $this->dumper->setStyles($this->styles);
57
58         // Now put the locale back
59         setlocale(LC_NUMERIC, $oldLocale);
60
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);
66                 } else {
67                     $a = Caster::filter($a, Caster::EXCLUDE_PROTECTED | Caster::EXCLUDE_PRIVATE);
68                 }
69             }
70
71             return $a;
72         }));
73     }
74
75     /**
76      * Register casters.
77      *
78      * @see http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
79      *
80      * @param callable[] $casters A map of casters
81      */
82     public function addCasters(array $casters)
83     {
84         $this->cloner->addCasters($casters);
85     }
86
87     /**
88      * Present a reference to the value.
89      *
90      * @param mixed $value
91      *
92      * @return string
93      */
94     public function presentRef($value)
95     {
96         return $this->present($value, 0);
97     }
98
99     /**
100      * Present a full representation of the value.
101      *
102      * If $depth is 0, the value will be presented as a ref instead.
103      *
104      * @param mixed $value
105      * @param int   $depth   (default: null)
106      * @param int   $options One of Presenter constants
107      *
108      * @return string
109      */
110     public function present($value, $depth = null, $options = 0)
111     {
112         $data = $this->cloner->cloneVar($value, !($options & self::VERBOSE) ? Caster::EXCLUDE_VERBOSE : 0);
113
114         if (null !== $depth) {
115             $data = $data->withMaxDepth($depth);
116         }
117
118         // Work around https://github.com/symfony/symfony/issues/23572
119         $oldLocale = setlocale(LC_NUMERIC, 0);
120         setlocale(LC_NUMERIC, 'C');
121
122         $output = '';
123         $this->dumper->dump($data, function ($line, $depth) use (&$output) {
124             if ($depth >= 0) {
125                 if ('' !== $output) {
126                     $output .= PHP_EOL;
127                 }
128                 $output .= str_repeat('  ', $depth) . $line;
129             }
130         });
131
132         // Now put the locale back
133         setlocale(LC_NUMERIC, $oldLocale);
134
135         return OutputFormatter::escape($output);
136     }
137 }