Skip to content

Commit

Permalink
优化文件地址
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwuxin committed Dec 4, 2024
1 parent c391236 commit f576753
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextualizedDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use think\Request;
Expand Down
106 changes: 106 additions & 0 deletions src/SourceContextProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace think\dumper;

use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Twig\Template;

final class SourceContextProvider implements ContextProviderInterface
{

public function __construct(
private ?string $charset = null,
private ?string $projectDir = null,
private int $limit = 9,
)
{
}

public function getContext(): ?array
{
$trace = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, $this->limit);

$file = $trace[1]['file'];
$line = $trace[1]['line'];
$name = '-' === $file || 'Standard input code' === $file ? 'Standard input code' : false;
$fileExcerpt = false;

for ($i = 2; $i < $this->limit; ++$i) {
if (isset($trace[$i]['class'], $trace[$i]['function'])
&& 'dump' === $trace[$i]['function']
&& Dumper::class === $trace[$i]['class']
) {
$file = $trace[$i]['file'] ?? $file;
$line = $trace[$i]['line'] ?? $line;

while (++$i < $this->limit) {
if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && !str_starts_with($trace[$i]['function'], 'call_user_func')) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];

break;
} elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) {
$template = $trace[$i]['object'];
$name = $template->getTemplateName();
$src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()
->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
$info = $template->getDebugInfo();
if (isset($info[$trace[$i - 1]['line']])) {
$line = $info[$trace[$i - 1]['line']];
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()
->getPath() : null;

if ($src) {
$src = explode("\n", $src);
$fileExcerpt = [];

for ($i = max($line - 3, 1), $max = min($line + 3, \count($src)); $i <= $max; ++$i) {
$fileExcerpt[] = '<li' . ($i === $line ? ' class="selected"' : '') . '><code>' . $this->htmlEncode($src[$i - 1]) . '</code></li>';
}

$fileExcerpt = '<ol start="' . max($line - 3, 1) . '">' . implode("\n", $fileExcerpt) . '</ol>';
}
}
break;
}
}
break;
}
}

if (false === $name) {
$name = str_replace('\\', '/', $file);
$name = substr($name, strrpos($name, '/') + 1);
}

$context = ['name' => $name, 'file' => $file, 'line' => $line];
$context['file_excerpt'] = $fileExcerpt;

if (null !== $this->projectDir) {
$context['project_dir'] = $this->projectDir;
if (str_starts_with($file, $this->projectDir)) {
$context['file_relative'] = ltrim(substr($file, \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
}
}

return $context;
}

private function htmlEncode(string $s): string
{
$html = '';

$dumper = new HtmlDumper(function ($line) use (&$html) {
$html .= $line;
}, $this->charset);
$dumper->setDumpHeader('');
$dumper->setDumpBoundaries('', '');

$cloner = new VarCloner();
$dumper->dump($cloner->cloneVar($s));

return substr(strip_tags($html), 1, -1);
}
}
1 change: 1 addition & 0 deletions tests/DumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public function testDump()
{
Dumper::dump('aa', 'cc');
d(['aa' => 'bbb', 'cc' => 'dd']);
d($this);
}
}

0 comments on commit f576753

Please sign in to comment.