-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathEchoHandler.php
35 lines (27 loc) · 975 Bytes
/
EchoHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
declare(strict_types=1);
namespace Yiisoft\VarDumper\Handler;
use Yiisoft\VarDumper\HandlerInterface;
use Yiisoft\VarDumper\VarDumper;
final class EchoHandler implements HandlerInterface
{
public function handle(mixed $variable, int $depth, bool $highlight = false): void
{
$varDumper = VarDumper::create($variable);
$output = $varDumper->asString($depth);
echo $highlight
? $this->highlight($output)
: $output;
}
private function highlight(string $string): string
{
$result = highlight_string("<?php\n" . $string, true);
$pattern = PHP_VERSION_ID >= 80300
? '~<span style="color: #0000BB"><\\?php\n</span>~'
: '~<span style="color: #0000BB"><\\?php<br \\/></span>~';
/**
* @var string We use correct pattern, so `preg_replace()` always returns a string.
*/
return preg_replace($pattern, '', $result, 1);
}
}