Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Foundation\Console;
4
 
5
use Illuminate\Foundation\Concerns\ResolvesDumpSource;
6
use Symfony\Component\Console\Output\ConsoleOutput;
7
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
8
use Symfony\Component\VarDumper\Cloner\Data;
9
use Symfony\Component\VarDumper\Cloner\VarCloner;
10
use Symfony\Component\VarDumper\Dumper\CliDumper as BaseCliDumper;
11
use Symfony\Component\VarDumper\VarDumper;
12
 
13
class CliDumper extends BaseCliDumper
14
{
15
    use ResolvesDumpSource;
16
 
17
    /**
18
     * The base path of the application.
19
     *
20
     * @var string
21
     */
22
    protected $basePath;
23
 
24
    /**
25
     * The output instance.
26
     *
27
     * @var \Symfony\Component\Console\Output\OutputInterface
28
     */
29
    protected $output;
30
 
31
    /**
32
     * The compiled view path for the application.
33
     *
34
     * @var string
35
     */
36
    protected $compiledViewPath;
37
 
38
    /**
39
     * If the dumper is currently dumping.
40
     *
41
     * @var bool
42
     */
43
    protected $dumping = false;
44
 
45
    /**
46
     * Create a new CLI dumper instance.
47
     *
48
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
49
     * @param  string  $basePath
50
     * @param  string  $compiledViewPath
51
     * @return void
52
     */
53
    public function __construct($output, $basePath, $compiledViewPath)
54
    {
55
        parent::__construct();
56
 
57
        $this->basePath = $basePath;
58
        $this->output = $output;
59
        $this->compiledViewPath = $compiledViewPath;
60
    }
61
 
62
    /**
63
     * Create a new CLI dumper instance and register it as the default dumper.
64
     *
65
     * @param  string  $basePath
66
     * @param  string  $compiledViewPath
67
     * @return void
68
     */
69
    public static function register($basePath, $compiledViewPath)
70
    {
71
        $cloner = tap(new VarCloner())->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
72
 
73
        $dumper = new static(new ConsoleOutput(), $basePath, $compiledViewPath);
74
 
75
        VarDumper::setHandler(fn ($value) => $dumper->dumpWithSource($cloner->cloneVar($value)));
76
    }
77
 
78
    /**
79
     * Dump a variable with its source file / line.
80
     *
81
     * @param  \Symfony\Component\VarDumper\Cloner\Data  $data
82
     * @return void
83
     */
84
    public function dumpWithSource(Data $data)
85
    {
86
        if ($this->dumping) {
87
            $this->dump($data);
88
 
89
            return;
90
        }
91
 
92
        $this->dumping = true;
93
 
94
        $output = (string) $this->dump($data, true);
95
        $lines = explode("\n", $output);
96
 
97
        $lines[0] .= $this->getDumpSourceContent();
98
 
99
        $this->output->write(implode("\n", $lines));
100
 
101
        $this->dumping = false;
102
    }
103
 
104
    /**
105
     * Get the dump's source console content.
106
     *
107
     * @return string
108
     */
109
    protected function getDumpSourceContent()
110
    {
111
        if (is_null($dumpSource = $this->resolveDumpSource())) {
112
            return '';
113
        }
114
 
115
        [$file, $relativeFile, $line] = $dumpSource;
116
 
117
        $href = $this->resolveSourceHref($file, $line);
118
 
119
        return sprintf(
120
            ' <fg=gray>// <fg=gray%s>%s%s</></>',
121
            is_null($href) ? '' : ";href=$href",
122
            $relativeFile,
123
            is_null($line) ? '' : ":$line"
124
        );
125
    }
126
 
127
    /**
128
     * {@inheritDoc}
129
     */
130
    protected function supportsColors(): bool
131
    {
132
        return $this->output->isDecorated();
133
    }
134
}