PluginProbe ʕ •ᴥ•ʔ
Kubio AI Page Builder / 2.8.6
Kubio AI Page Builder v2.8.6
2.9.0 2.8.6 2.8.5 2.8.4 2.8.3 2.8.2 2.8.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.9.0 2.0.0 2.1.1 2.1.2 2.1.3 2.2.0 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.5 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.6.2 2.6.3 2.6.5 2.6.6 2.6.7 2.7.0 2.7.1 2.7.2 2.7.3 2.8.0
kubio / vendor / symfony / var-dumper / Dumper / ContextProvider / SourceContextProvider.php
kubio / vendor / symfony / var-dumper / Dumper / ContextProvider Last commit date
CliContextProvider.php 4 years ago ContextProviderInterface.php 1 year ago RequestContextProvider.php 4 years ago SourceContextProvider.php 1 year ago
SourceContextProvider.php
127 lines
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\VarDumper\Dumper\ContextProvider;
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 <p@tchwork.com>
24 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
25 */
26 final class SourceContextProvider implements ContextProviderInterface
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 = '-' === $file || 'Standard input code' === $file ? 'Standard input code' : 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'] ?? $file;
56 $line = $trace[$i]['line'] ?? $line;
57
58 while (++$i < $this->limit) {
59 if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && !str_starts_with($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 (str_starts_with($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