-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
49 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Buggregator\Trap\Client\TrapHandle\ContextProvider; | ||
|
||
use Buggregator\Trap\Client\TrapHandle\StackTrace; | ||
|
@@ -18,6 +16,11 @@ | |
* @author Nicolas Grekas <[email protected]> | ||
* @author Maxime Steinhausser <[email protected]> | ||
* | ||
* @link https://github.com/symfony/var-dumper/blob/7.0/Dumper/ContextProvider/SourceContextProvider.php | ||
* @link https://github.com/symfony/var-dumper/blob/6.3/Dumper/ContextProvider/SourceContextProvider.php | ||
* | ||
* @psalm-suppress all | ||
* | ||
* todo: rewrite and decompose | ||
*/ | ||
final class Source implements ContextProviderInterface | ||
|
@@ -27,6 +30,9 @@ final class Source implements ContextProviderInterface | |
private ?string $projectDir; | ||
private ?FileLinkFormatter $fileLinkFormatter; | ||
|
||
/** | ||
* @psalm-suppress UndefinedClass | ||
*/ | ||
public function __construct(string $charset = null, string $projectDir = null, FileLinkFormatter $fileLinkFormatter = null, int $limit = 9) | ||
{ | ||
$this->charset = $charset; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,33 +22,35 @@ | |
/** | ||
* @internal | ||
* @psalm-internal Buggregator\Trap\Client | ||
* @psalm-type DumperHandler = Closure(mixed $var, string|int|null $label, int $depth): void | ||
*/ | ||
final class Dumper | ||
{ | ||
/** @var DumperHandler|null */ | ||
/** @var null|Closure(mixed, string|null, int): mixed */ | ||
private static ?Closure $handler; | ||
|
||
public static function dump(mixed $var, string|int|null $label = null, int $depth = 0) | ||
public static function dump(mixed $var, string|int|null $label = null, int $depth = 0): mixed | ||
{ | ||
return (self::$handler ??= self::registerHandler())($var, $label, $depth); | ||
return (self::$handler ??= self::registerHandler())($var, empty($label) ? null : (string)$label, $depth); | ||
} | ||
|
||
/** | ||
* @return DumperHandler|null The previous handler if any | ||
* @return null|callable(mixed, string|null, int): mixed | ||
* @psalm-suppress MixedInferredReturnType, MixedPropertyTypeCoercion, MismatchingDocblockReturnType | ||
*/ | ||
public static function setHandler(callable $callable = null): ?Closure | ||
{ | ||
return ([$callable, self::$handler] = [self::$handler, $callable(...)])[0]; | ||
return ([$callable, self::$handler] = [self::$handler, $callable === null ? null : $callable(...)])[0]; | ||
} | ||
|
||
/** | ||
* @return DumperHandler | ||
* @return Closure(mixed, string|null, int): mixed | ||
* | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
private static function registerHandler(): Closure | ||
{ | ||
$cloner = new VarCloner(); | ||
/** @psalm-suppress InvalidArgument */ | ||
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO); | ||
|
||
$format = $_SERVER['VAR_DUMPER_FORMAT'] ?? null; | ||
|
@@ -73,31 +75,38 @@ private static function registerHandler(): Closure | |
$dumper = new ContextualizedDumper($dumper, [new SourceContextProvider()]); | ||
} | ||
|
||
return self::$handler = static function ($var, string|int|null $label = null, int $depth = 0) use ($cloner, $dumper): void { | ||
return self::$handler = static function (mixed $var, string|null $label = null, int $depth = 0) | ||
use ($cloner, $dumper): ?string { | ||
$var = $cloner->cloneVar($var); | ||
|
||
$label === null or $var = $var->withContext(['label' => $label]); | ||
$depth > 0 and $var = $var->withMaxDepth($depth); | ||
|
||
$dumper->dump($var); | ||
return $dumper->dump($var); | ||
}; | ||
} | ||
|
||
/** | ||
* @return array<array-key, ContextProviderInterface> The context providers | ||
* @author Nicolas Grekas <[email protected]> | ||
* | ||
* @psalm-suppress UndefinedClass | ||
*/ | ||
private static function getContextProviders(): array | ||
{ | ||
$contextProviders = []; | ||
|
||
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) && \class_exists(Request::class)) { | ||
$requestStack = new RequestStack(); | ||
/** @psalm-suppress MixedMethodCall */ | ||
$requestStack->push(Request::createFromGlobals()); | ||
$contextProviders['request'] = new RequestContextProvider($requestStack); | ||
} | ||
|
||
$fileLinkFormatter = \class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null) : null; | ||
/** @var null|FileLinkFormatter $fileLinkFormatter */ | ||
$fileLinkFormatter = \class_exists(FileLinkFormatter::class) | ||
? new FileLinkFormatter(null, $requestStack ?? null) | ||
: null; | ||
|
||
return $contextProviders + [ | ||
'cli' => new CliContextProvider(), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters