Skip to content

Commit

Permalink
Rename Kint::$app_root_dirs to Utils::$path_aliases
Browse files Browse the repository at this point in the history
Call Utils::shortenPath now instead of Kint::shortenPath
  • Loading branch information
jnvsor committed Nov 3, 2024
1 parent 5603698 commit ecbe3f6
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 131 deletions.
Binary file modified build/kint.phar
Binary file not shown.
5 changes: 3 additions & 2 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@
AbstractRenderer::$file_link_format = \ini_get('xdebug.file_link_format');
}
if (isset($_SERVER['DOCUMENT_ROOT']) && false === \strpos($_SERVER['DOCUMENT_ROOT'], "\0")) {
Kint::$app_root_dirs = [
Utils::$path_aliases = [
$_SERVER['DOCUMENT_ROOT'] => '<ROOT>',
];

// Suppressed for unreadable document roots (related to open_basedir)
if (false !== @\realpath($_SERVER['DOCUMENT_ROOT'])) {
Kint::$app_root_dirs[\realpath($_SERVER['DOCUMENT_ROOT'])] = '<ROOT>';
/** @psalm-suppress PropertyTypeCoercion */
Utils::$path_aliases[\realpath($_SERVER['DOCUMENT_ROOT'])] = '<ROOT>';
}
}

Expand Down
61 changes: 0 additions & 61 deletions src/Kint.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,6 @@ class Kint implements FacadeInterface
*/
public static bool $cli_detection = true;

/**
* @var array base directories of your application that will be displayed instead of the full path.
*
* Keys are paths, values are replacement strings
*
* Example for laravel:
*
* Kint::$app_root_dirs = [
* base_path() => '<BASE>',
* app_path() => '<APP>',
* base_path().'/vendor' => '<VENDOR>',
* ];
*
* Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>']
*/
public static array $app_root_dirs = [];

/**
* @var bool Return output instead of echoing
*/
Expand Down Expand Up @@ -308,7 +291,6 @@ public static function getStatics(): array
{
return [
'aliases' => static::$aliases,
'app_root_dirs' => static::$app_root_dirs,
'cli_detection' => static::$cli_detection,
'depth_limit' => static::$depth_limit,
'display_called_from' => static::$display_called_from,
Expand Down Expand Up @@ -611,49 +593,6 @@ public static function dump(...$args)
return 0;
}

/**
* generic path display callback, can be configured in app_root_dirs; purpose is
* to show relevant path info and hide as much of the path as possible.
*/
public static function shortenPath(string $file): string
{
$file = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $file)), 'strlen'));

$longest_match = 0;
$match = '/';

foreach (static::$app_root_dirs as $path => $alias) {
/** @psalm-var string $path */
if (empty($path)) {
continue;
}

$path = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', $path)), 'strlen'));

if (\array_slice($file, 0, \count($path)) === $path && \count($path) > $longest_match) {
$longest_match = \count($path);
$match = $alias;
}
}

if ($longest_match) {
$file = [$match, ...\array_slice($file, $longest_match)];

return \implode('/', $file);
}

// fallback to find common path with Kint dir
$kint = \array_values(\array_filter(\explode('/', \str_replace('\\', '/', KINT_DIR)), 'strlen'));

foreach ($file as $i => $part) {
if (!isset($kint[$i]) || $kint[$i] !== $part) {
return ($i ? '.../' : '/').\implode('/', \array_slice($file, $i));
}
}

return '/'.\implode('/', $file);
}

/**
* Returns specific function call info from a stack trace frame, or null if no match could be found.
*
Expand Down
3 changes: 1 addition & 2 deletions src/Renderer/PlainRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace Kint\Renderer;

use Kint\Kint;
use Kint\Utils;
use Kint\Value\AbstractValue;

Expand Down Expand Up @@ -175,7 +174,7 @@ public function postRender(): string

public function ideLink(string $file, int $line): string
{
$path = $this->escape(Kint::shortenPath($file)).':'.$line;
$path = $this->escape(Utils::shortenPath($file)).':'.$line;
$ideLink = self::getFileLink($file, $line);

if (null === $ideLink) {
Expand Down
4 changes: 2 additions & 2 deletions src/Renderer/Rich/CallableDefinitionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace Kint\Renderer\Rich;

use Kint\Kint;
use Kint\Utils;
use Kint\Value\Representation\CallableDefinitionRepresentation;
use Kint\Value\Representation\RepresentationInterface;

Expand All @@ -44,7 +44,7 @@ public function renderTab(RepresentationInterface $r): ?string
$docstring[] = 'Inherited from '.$this->renderer->escape($class);
}

$docstring[] = 'Defined in '.$this->renderer->escape(Kint::shortenPath($r->getFileName())).':'.$r->getLine();
$docstring[] = 'Defined in '.$this->renderer->escape(Utils::shortenPath($r->getFileName())).':'.$r->getLine();

$docstring = '<small>'.\implode("\n", $docstring).'</small>';

Expand Down
3 changes: 1 addition & 2 deletions src/Renderer/RichRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace Kint\Renderer;

use Kint\Kint;
use Kint\Renderer\Rich\TabPluginInterface;
use Kint\Renderer\Rich\ValuePluginInterface;
use Kint\Utils;
Expand Down Expand Up @@ -477,7 +476,7 @@ public function escape(string $string, $encoding = false): string

public function ideLink(string $file, int $line): string
{
$path = $this->escape(Kint::shortenPath($file)).':'.$line;
$path = $this->escape(Utils::shortenPath($file)).':'.$line;
$ideLink = self::getFileLink($file, $line);

if (null === $ideLink) {
Expand Down
3 changes: 1 addition & 2 deletions src/Renderer/TextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

namespace Kint\Renderer;

use Kint\Kint;
use Kint\Parser;
use Kint\Parser\PluginInterface as ParserPluginInterface;
use Kint\Renderer\Text\PluginInterface;
Expand Down Expand Up @@ -329,7 +328,7 @@ public function filterParserPlugins(array $plugins): array

public function ideLink(string $file, int $line): string
{
return $this->escape(Kint::shortenPath($file)).':'.$line;
return $this->escape(Utils::shortenPath($file)).':'.$line;
}

/**
Expand Down
78 changes: 78 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ final class Utils
*/
public static array $legacy_encodings = [];

/**
* @var array Path aliases that will be displayed instead of the full path.
*
* Keys are paths, values are replacement strings
*
* Example for laravel:
*
* Utils::$path_aliases = [
* base_path() => '<BASE>',
* app_path() => '<APP>',
* base_path().'/vendor' => '<VENDOR>',
* ];
*
* Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>']
*
* @psalm-var array<non-empty-string, string>
*/
public static array $path_aliases = [];

/**
* @codeCoverageIgnore
*
Expand Down Expand Up @@ -419,6 +438,65 @@ public static function substr(string $string, int $start, ?int $length = null, $
return \substr($string, $start, $length ?? PHP_INT_MAX);
}

public static function shortenPath(string $file): string
{
$split = \explode('/', \str_replace('\\', '/', $file));

$longest_match = 0;
$match = '';

foreach (self::$path_aliases as $path => $alias) {
$path = \explode('/', \str_replace('\\', '/', $path));

if (\count($path) < 2) {
continue;
}

if (\array_slice($split, 0, \count($path)) === $path && \count($path) > $longest_match) {
$longest_match = \count($path);
$match = $alias;
}
}

if ($longest_match) {
$suffix = \implode('/', \array_slice($split, $longest_match));

if (\preg_match('%^/*$%', $suffix)) {
return $match;
}

return $match.'/'.$suffix;
}

// fallback to find common path with Kint dir
$kint = \explode('/', \str_replace('\\', '/', KINT_DIR));
$had_real_path_part = false;

foreach ($split as $i => $part) {
if (!isset($kint[$i]) || $kint[$i] !== $part) {
if (!$had_real_path_part) {
break;
}

$suffix = \implode('/', \array_slice($split, $i));

if (\preg_match('%^/*$%', $suffix)) {
break;
}

$prefix = $i > 1 ? '.../' : '/';

return $prefix.$suffix;
}

if ($i > 0 && \strlen($kint[$i])) {
$had_real_path_part = true;
}
}

return $file;
}

public static function composerGetExtras(string $key = 'kint'): array
{
if (0 === \strpos(KINT_DIR, 'phar://')) {
Expand Down
4 changes: 2 additions & 2 deletions src/Value/ClosureValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace Kint\Value;

use Closure;
use Kint\Kint;
use Kint\Utils;
use Kint\Value\Context\BaseContext;
use Kint\Value\Context\ContextInterface;
use ReflectionFunction;
Expand Down Expand Up @@ -112,7 +112,7 @@ public function getDisplayName(): string
public function getDisplayValue(): ?string
{
if (null !== $this->filename && null !== $this->startline) {
return Kint::shortenPath($this->filename).':'.$this->startline;
return Utils::shortenPath($this->filename).':'.$this->startline;
}

return parent::getDisplayValue();
Expand Down
4 changes: 2 additions & 2 deletions src/Value/StreamValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

namespace Kint\Value;

use Kint\Kint;
use Kint\Utils;
use Kint\Value\Context\ContextInterface;
use Kint\Value\Representation\ContainerRepresentation;

Expand Down Expand Up @@ -66,7 +66,7 @@ public function getDisplayValue(): ?string
}

if ('/' === $this->uri[0] && \stream_is_local($this->uri)) {
return Kint::shortenPath($this->uri);
return Utils::shortenPath($this->uri);
}

return $this->uri;
Expand Down
2 changes: 1 addition & 1 deletion tests/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ function (&$var, $v) use (&$p1_triggered) {
[
'$value',
'stream resource',
Kint::shortenPath(__FILE__),
Utils::shortenPath(__FILE__),
],
$d2
);
Expand Down
55 changes: 0 additions & 55 deletions tests/KintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1066,59 +1066,4 @@ public function testGetCallInfo($aliases, $trace, $args, $expect)
{
$this->assertSame($expect, Kint::getCallInfo($aliases, $trace, $args));
}

public function pathProvider()
{
return [
'standard file' => [
'path' => __FILE__,
'expect' => '<tests>/KintTest.php',
],
'standard dir' => [
'path' => __DIR__,
'expect' => '<tests>',
],
'parent dir' => [
'path' => KINT_DIR,
'expect' => '<kint>',
],
'sub file' => [
'path' => KINT_DIR.'/src//test',
'expect' => '<kint>/src/test',
],
'common path' => [
'path' => \dirname(KINT_DIR).'/test/',
'expect' => '.../test',
],
'root path' => [
'path' => '/',
'expect' => '/',
],
'no common path' => [
'path' => '/asdfasdf/test/',
'expect' => '/asdfasdf/test',
],
];
}

/**
* @dataProvider pathProvider
*
* @covers \Kint\Kint::shortenPath
*
* @param string $path
* @param string $expect
*/
public function testShortenPath($path, $expect)
{
Kint::$app_root_dirs = [
KINT_DIR => '<kint>',
KINT_DIR.'/test' => '<test>',
'' => '<Nothing!>',
__DIR__ => '<tests>',
KINT_DIR.'/tes' => '<tes>',
];

$this->assertSame($expect, Kint::shortenPath($path));
}
}
3 changes: 3 additions & 0 deletions tests/KintTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class KintTestCase extends TestCase
protected $rich_statics;
protected $char_encodings;
protected $legacy_encodings;
protected $path_aliases;
protected $file_link_format;
protected $text_decorations;
protected $text_plugin_whitelist;
Expand Down Expand Up @@ -86,6 +87,7 @@ protected function setUp(): void
];
$this->char_encodings = Utils::$char_encodings;
$this->legacy_encodings = Utils::$legacy_encodings;
$this->path_aliases = Utils::$path_aliases;
$this->file_link_format = AbstractRenderer::$file_link_format;
$this->text_decorations = TextRenderer::$decorations;
$this->text_plugin_whitelist = TextRenderer::$parser_plugin_whitelist;
Expand Down Expand Up @@ -122,6 +124,7 @@ protected function tearDown(): void

Utils::$char_encodings = $this->char_encodings;
Utils::$legacy_encodings = $this->legacy_encodings;
Utils::$path_aliases = $this->path_aliases;
AbstractRenderer::$file_link_format = $this->file_link_format;
TextRenderer::$decorations = $this->text_decorations;
TextRenderer::$parser_plugin_whitelist = $this->text_plugin_whitelist;
Expand Down
Loading

0 comments on commit ecbe3f6

Please sign in to comment.