Version 1
[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)
50     {
51         $this->dumper = new Dumper($formatter);
52         $this->dumper->setStyles($this->styles);
53
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);
59                 } else {
60                     $a = Caster::filter($a, Caster::EXCLUDE_PROTECTED | Caster::EXCLUDE_PRIVATE);
61                 }
62             }
63
64             return $a;
65         }));
66     }
67
68     /**
69      * Register casters.
70      *
71      * @see http://symfony.com/doc/current/components/var_dumper/advanced.html#casters
72      *
73      * @param callable[] $casters A map of casters
74      */
75     public function addCasters(array $casters)
76     {
77         $this->cloner->addCasters($casters);
78     }
79
80     /**
81      * Present a reference to the value.
82      *
83      * @param mixed $value
84      *
85      * @return string
86      */
87     public function presentRef($value)
88     {
89         return $this->present($value, 0);
90     }
91
92     /**
93      * Present a full representation of the value.
94      *
95      * If $depth is 0, the value will be presented as a ref instead.
96      *
97      * @param mixed $value
98      * @param int   $depth   (default: null)
99      * @param int   $options One of Presenter constants
100      *
101      * @return string
102      */
103     public function present($value, $depth = null, $options = 0)
104     {
105         $data = $this->cloner->cloneVar($value, !($options & self::VERBOSE) ? Caster::EXCLUDE_VERBOSE : 0);
106
107         if (null !== $depth) {
108             $data = $data->withMaxDepth($depth);
109         }
110
111         $output = '';
112         $this->dumper->dump($data, function ($line, $depth) use (&$output) {
113             if ($depth >= 0) {
114                 if ('' !== $output) {
115                     $output .= PHP_EOL;
116                 }
117                 $output .= str_repeat('  ', $depth) . $line;
118             }
119         });
120
121         return OutputFormatter::escape($output);
122     }
123 }