1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
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 Ekino\Drupal\Debug\Action\DisplayDumpLocation; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter; |
15
|
|
|
use Symfony\Component\VarDumper\Cloner\VarCloner; |
16
|
|
|
use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
17
|
|
|
use Symfony\Component\VarDumper\VarDumper; |
18
|
|
|
use Twig\Template; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Tries to provide context from sources (class name, file, line, code excerpt, ...). |
22
|
|
|
* |
23
|
|
|
* @author Nicolas Grekas <[email protected]> |
24
|
|
|
* @author Maxime Steinhausser <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class SourceContextProvider |
27
|
|
|
{ |
28
|
|
|
private $limit; |
29
|
|
|
private $charset; |
30
|
|
|
private $projectDir; |
31
|
|
|
private $fileLinkFormatter; |
32
|
|
|
|
33
|
|
|
public function __construct(string $charset = null, string $projectDir = null, FileLinkFormatter $fileLinkFormatter = null, int $limit = 9) |
34
|
|
|
{ |
35
|
|
|
$this->charset = $charset; |
36
|
|
|
$this->projectDir = $projectDir; |
37
|
|
|
$this->fileLinkFormatter = $fileLinkFormatter; |
38
|
|
|
$this->limit = $limit; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getContext(): ?array |
42
|
|
|
{ |
43
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, $this->limit); |
44
|
|
|
|
45
|
|
|
$file = $trace[1]['file']; |
46
|
|
|
$line = $trace[1]['line']; |
47
|
|
|
$name = false; |
48
|
|
|
$fileExcerpt = false; |
49
|
|
|
|
50
|
|
|
for ($i = 2; $i < $this->limit; ++$i) { |
51
|
|
|
if (isset($trace[$i]['class'], $trace[$i]['function']) |
52
|
|
|
&& 'dump' === $trace[$i]['function'] |
53
|
|
|
&& VarDumper::class === $trace[$i]['class'] |
54
|
|
|
) { |
55
|
|
|
$file = $trace[$i]['file']; |
56
|
|
|
$line = $trace[$i]['line']; |
57
|
|
|
|
58
|
|
|
while (++$i < $this->limit) { |
59
|
|
|
if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) { |
60
|
|
|
$file = $trace[$i]['file']; |
61
|
|
|
$line = $trace[$i]['line']; |
62
|
|
|
|
63
|
|
|
break; |
64
|
|
|
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) { |
65
|
|
|
$template = $trace[$i]['object']; |
66
|
|
|
$name = $template->getTemplateName(); |
67
|
|
|
$src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false); |
68
|
|
|
$info = $template->getDebugInfo(); |
69
|
|
|
if (isset($info[$trace[$i - 1]['line']])) { |
70
|
|
|
$line = $info[$trace[$i - 1]['line']]; |
71
|
|
|
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null; |
72
|
|
|
|
73
|
|
|
if ($src) { |
74
|
|
|
$src = explode("\n", $src); |
75
|
|
|
$fileExcerpt = []; |
76
|
|
|
|
77
|
|
|
for ($i = max($line - 3, 1), $max = min($line + 3, \count($src)); $i <= $max; ++$i) { |
78
|
|
|
$fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>'; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (false === $name) { |
92
|
|
|
$name = str_replace('\\', '/', $file); |
93
|
|
|
$name = substr($name, strrpos($name, '/') + 1); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$context = ['name' => $name, 'file' => $file, 'line' => $line]; |
97
|
|
|
$context['file_excerpt'] = $fileExcerpt; |
98
|
|
|
|
99
|
|
|
if (null !== $this->projectDir) { |
100
|
|
|
$context['project_dir'] = $this->projectDir; |
101
|
|
|
if (0 === strpos($file, $this->projectDir)) { |
102
|
|
|
$context['file_relative'] = ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ($this->fileLinkFormatter && $fileLink = $this->fileLinkFormatter->format($context['file'], $context['line'])) { |
107
|
|
|
$context['file_link'] = $fileLink; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $context; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function htmlEncode(string $s): string |
114
|
|
|
{ |
115
|
|
|
$html = ''; |
116
|
|
|
|
117
|
|
|
$dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset); |
118
|
|
|
$dumper->setDumpHeader(''); |
119
|
|
|
$dumper->setDumpBoundaries('', ''); |
120
|
|
|
|
121
|
|
|
$cloner = new VarCloner(); |
122
|
|
|
$dumper->dump($cloner->cloneVar($s)); |
123
|
|
|
|
124
|
|
|
return substr(strip_tags($html), 1, -1); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|