| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Foundation\Concerns;
|
|
|
4 |
|
|
|
5 |
use Throwable;
|
|
|
6 |
|
|
|
7 |
trait ResolvesDumpSource
|
|
|
8 |
{
|
|
|
9 |
/**
|
|
|
10 |
* All of the href formats for common editors.
|
|
|
11 |
*
|
|
|
12 |
* @var array<string, string>
|
|
|
13 |
*/
|
|
|
14 |
protected $editorHrefs = [
|
|
|
15 |
'atom' => 'atom://core/open/file?filename={file}&line={line}',
|
|
|
16 |
'emacs' => 'emacs://open?url=file://{file}&line={line}',
|
|
|
17 |
'idea' => 'idea://open?file={file}&line={line}',
|
|
|
18 |
'macvim' => 'mvim://open/?url=file://{file}&line={line}',
|
|
|
19 |
'netbeans' => 'netbeans://open/?f={file}:{line}',
|
|
|
20 |
'nova' => 'nova://core/open/file?filename={file}&line={line}',
|
|
|
21 |
'phpstorm' => 'phpstorm://open?file={file}&line={line}',
|
|
|
22 |
'sublime' => 'subl://open?url=file://{file}&line={line}',
|
|
|
23 |
'textmate' => 'txmt://open?url=file://{file}&line={line}',
|
|
|
24 |
'vscode' => 'vscode://file/{file}:{line}',
|
|
|
25 |
'vscode-insiders' => 'vscode-insiders://file/{file}:{line}',
|
|
|
26 |
'vscode-insiders-remote' => 'vscode-insiders://vscode-remote/{file}:{line}',
|
|
|
27 |
'vscode-remote' => 'vscode://vscode-remote/{file}:{line}',
|
|
|
28 |
'vscodium' => 'vscodium://file/{file}:{line}',
|
|
|
29 |
'xdebug' => 'xdebug://{file}@{line}',
|
|
|
30 |
];
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Files that require special trace handling and their levels.
|
|
|
34 |
*
|
|
|
35 |
* @var array<string, int>
|
|
|
36 |
*/
|
|
|
37 |
protected static $adjustableTraces = [
|
|
|
38 |
'symfony/var-dumper/Resources/functions/dump.php' => 1,
|
|
|
39 |
'Illuminate/Collections/Traits/EnumeratesValues.php' => 4,
|
|
|
40 |
];
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The source resolver.
|
|
|
44 |
*
|
|
|
45 |
* @var (callable(): (array{0: string, 1: string, 2: int|null}|null))|null|false
|
|
|
46 |
*/
|
|
|
47 |
protected static $dumpSourceResolver;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Resolve the source of the dump call.
|
|
|
51 |
*
|
|
|
52 |
* @return array{0: string, 1: string, 2: int|null}|null
|
|
|
53 |
*/
|
|
|
54 |
public function resolveDumpSource()
|
|
|
55 |
{
|
|
|
56 |
if (static::$dumpSourceResolver === false) {
|
|
|
57 |
return null;
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
if (static::$dumpSourceResolver) {
|
|
|
61 |
return call_user_func(static::$dumpSourceResolver);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20);
|
|
|
65 |
|
|
|
66 |
$sourceKey = null;
|
|
|
67 |
|
|
|
68 |
foreach ($trace as $traceKey => $traceFile) {
|
|
|
69 |
if (! isset($traceFile['file'])) {
|
|
|
70 |
continue;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
foreach (self::$adjustableTraces as $name => $key) {
|
|
|
74 |
if (str_ends_with(
|
|
|
75 |
$traceFile['file'],
|
|
|
76 |
str_replace('/', DIRECTORY_SEPARATOR, $name)
|
|
|
77 |
)) {
|
|
|
78 |
$sourceKey = $traceKey + $key;
|
|
|
79 |
break;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
if (! is_null($sourceKey)) {
|
|
|
84 |
break;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if (is_null($sourceKey)) {
|
|
|
89 |
return;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
$file = $trace[$sourceKey]['file'] ?? null;
|
|
|
93 |
$line = $trace[$sourceKey]['line'] ?? null;
|
|
|
94 |
|
|
|
95 |
if (is_null($file) || is_null($line)) {
|
|
|
96 |
return;
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
$relativeFile = $file;
|
|
|
100 |
|
|
|
101 |
if ($this->isCompiledViewFile($file)) {
|
|
|
102 |
$file = $this->getOriginalFileForCompiledView($file);
|
|
|
103 |
$line = null;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
if (str_starts_with($file, $this->basePath)) {
|
|
|
107 |
$relativeFile = substr($file, strlen($this->basePath) + 1);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
return [$file, $relativeFile, $line];
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
/**
|
|
|
114 |
* Determine if the given file is a view compiled.
|
|
|
115 |
*
|
|
|
116 |
* @param string $file
|
|
|
117 |
* @return bool
|
|
|
118 |
*/
|
|
|
119 |
protected function isCompiledViewFile($file)
|
|
|
120 |
{
|
|
|
121 |
return str_starts_with($file, $this->compiledViewPath);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Get the original view compiled file by the given compiled file.
|
|
|
126 |
*
|
|
|
127 |
* @param string $file
|
|
|
128 |
* @return string
|
|
|
129 |
*/
|
|
|
130 |
protected function getOriginalFileForCompiledView($file)
|
|
|
131 |
{
|
|
|
132 |
preg_match('/\/\*\*PATH\s(.*)\sENDPATH/', file_get_contents($file), $matches);
|
|
|
133 |
|
|
|
134 |
if (isset($matches[1])) {
|
|
|
135 |
$file = $matches[1];
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return $file;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Resolve the source href, if possible.
|
|
|
143 |
*
|
|
|
144 |
* @param string $file
|
|
|
145 |
* @param int|null $line
|
|
|
146 |
* @return string|null
|
|
|
147 |
*/
|
|
|
148 |
protected function resolveSourceHref($file, $line)
|
|
|
149 |
{
|
|
|
150 |
try {
|
|
|
151 |
$editor = config('app.editor');
|
|
|
152 |
} catch (Throwable $e) {
|
|
|
153 |
// ..
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
if (! isset($editor)) {
|
|
|
157 |
return;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
$href = is_array($editor) && isset($editor['href'])
|
|
|
161 |
? $editor['href']
|
|
|
162 |
: ($this->editorHrefs[$editor['name'] ?? $editor] ?? sprintf('%s://open?file={file}&line={line}', $editor['name'] ?? $editor));
|
|
|
163 |
|
|
|
164 |
if ($basePath = $editor['base_path'] ?? false) {
|
|
|
165 |
$file = str_replace($this->basePath, $basePath, $file);
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
$href = str_replace(
|
|
|
169 |
['{file}', '{line}'],
|
|
|
170 |
[$file, is_null($line) ? 1 : $line],
|
|
|
171 |
$href,
|
|
|
172 |
);
|
|
|
173 |
|
|
|
174 |
return $href;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
/**
|
|
|
178 |
* Set the resolver that resolves the source of the dump call.
|
|
|
179 |
*
|
|
|
180 |
* @param (callable(): (array{0: string, 1: string, 2: int|null}|null))|null $callable
|
|
|
181 |
* @return void
|
|
|
182 |
*/
|
|
|
183 |
public static function resolveDumpSourceUsing($callable)
|
|
|
184 |
{
|
|
|
185 |
static::$dumpSourceResolver = $callable;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Don't include the location / file of the dump in dumps.
|
|
|
190 |
*
|
|
|
191 |
* @return void
|
|
|
192 |
*/
|
|
|
193 |
public static function dontIncludeSource()
|
|
|
194 |
{
|
|
|
195 |
static::$dumpSourceResolver = false;
|
|
|
196 |
}
|
|
|
197 |
}
|