|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace BEAR\Resource\SemanticLog\Profile\Verbose; |
|
6
|
|
|
|
|
7
|
|
|
use BEAR\Resource\AbstractRequest; |
|
8
|
|
|
use BEAR\Resource\SemanticLog\Profile\PhpProfile; |
|
9
|
|
|
use BEAR\Resource\SemanticLog\Profile\Profile; |
|
10
|
|
|
use BEAR\Resource\SemanticLog\Profile\XdebugTrace; |
|
11
|
|
|
use BEAR\Resource\SemanticLog\Profile\XHProfResult; |
|
12
|
|
|
use JsonSerializable; |
|
13
|
|
|
use Koriym\SemanticLogger\AbstractContext; |
|
14
|
|
|
use Override; |
|
15
|
|
|
|
|
16
|
|
|
use function spl_object_hash; |
|
17
|
|
|
use function strtoupper; |
|
18
|
|
|
use function uniqid; |
|
19
|
|
|
|
|
20
|
|
|
final class OpenContext extends AbstractContext implements JsonSerializable |
|
21
|
|
|
{ |
|
22
|
|
|
/** @psalm-suppress InvalidClassConstantType */ |
|
23
|
|
|
public const TYPE = 'bear_resource_request'; |
|
24
|
|
|
|
|
25
|
|
|
/** @psalm-suppress InvalidClassConstantType */ |
|
26
|
|
|
public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/open-context.json'; |
|
27
|
|
|
|
|
28
|
|
|
public readonly string $method; |
|
29
|
|
|
public readonly string $uri; |
|
30
|
|
|
public readonly Profile $profile; |
|
31
|
|
|
|
|
32
|
|
|
/** @var array<string, string|null> */ |
|
33
|
|
|
private static array $xdebugIdMap = []; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(AbstractRequest $request) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->method = strtoupper($request->method); |
|
|
|
|
|
|
38
|
|
|
$this->uri = $request->toUri(); |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
// Start profiling and capture initial profile data |
|
41
|
|
|
$xhprofResult = XHProfResult::start(); |
|
42
|
|
|
$xdebugTrace = XdebugTrace::start(); |
|
43
|
|
|
$phpProfile = PhpProfile::capture(); |
|
44
|
|
|
|
|
45
|
|
|
$this->profile = new Profile( |
|
|
|
|
|
|
46
|
|
|
xhprof: $xhprofResult, |
|
47
|
|
|
xdebug: $xdebugTrace, |
|
48
|
|
|
php: $phpProfile, |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
|
|
// Always generate an ID for profiling context, regardless of Xdebug availability |
|
52
|
|
|
$xdebugId = uniqid('profile_', true); |
|
53
|
|
|
self::$xdebugIdMap[spl_object_hash($this)] = $xdebugId; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public static function create(AbstractRequest $request): self |
|
57
|
|
|
{ |
|
58
|
|
|
return new self($request); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getXdebugId(): ?string |
|
62
|
|
|
{ |
|
63
|
|
|
return self::$xdebugIdMap[spl_object_hash($this)] ?? null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @return array<string, mixed> */ |
|
67
|
|
|
#[Override] |
|
68
|
|
|
public function jsonSerialize(): array |
|
69
|
|
|
{ |
|
70
|
|
|
return [ |
|
71
|
|
|
'method' => $this->method, |
|
72
|
|
|
'uri' => $this->uri, |
|
73
|
|
|
'profile' => $this->profile, |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|