|
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\XdebugTrace; |
|
9
|
|
|
use BEAR\Resource\SemanticLog\Profile\XHProfResult; |
|
10
|
|
|
use JsonSerializable; |
|
11
|
|
|
use Koriym\SemanticLogger\AbstractContext; |
|
12
|
|
|
use Override; |
|
13
|
|
|
|
|
14
|
|
|
use function spl_object_hash; |
|
15
|
|
|
use function strtolower; |
|
16
|
|
|
use function strtoupper; |
|
17
|
|
|
use function ucfirst; |
|
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
|
|
|
/** @var array{string, string} */ |
|
|
|
|
|
|
31
|
|
|
public readonly array $call; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array<string, string|null> */ |
|
34
|
|
|
private static array $xdebugIdMap = []; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(AbstractRequest $request) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->method = strtoupper($request->method); |
|
|
|
|
|
|
39
|
|
|
$this->uri = $request->toUri(); |
|
|
|
|
|
|
40
|
|
|
$this->call = [ |
|
|
|
|
|
|
41
|
|
|
$request->resourceObject::class, |
|
42
|
|
|
'on' . ucfirst(strtolower($request->method)) |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
// Start profiling (but don't capture data yet - that's for close) |
|
46
|
|
|
XHProfResult::start(); |
|
47
|
|
|
XdebugTrace::start(); |
|
48
|
|
|
|
|
49
|
|
|
// Generate an ID for profiling context |
|
50
|
|
|
$xdebugId = uniqid('profile_', true); |
|
51
|
|
|
self::$xdebugIdMap[spl_object_hash($this)] = $xdebugId; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public static function create(AbstractRequest $request): self |
|
55
|
|
|
{ |
|
56
|
|
|
return new self($request); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function getXdebugId(): ?string |
|
60
|
|
|
{ |
|
61
|
|
|
return self::$xdebugIdMap[spl_object_hash($this)] ?? null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** @return array<string, mixed> */ |
|
65
|
|
|
#[Override] |
|
66
|
|
|
public function jsonSerialize(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
'method' => $this->method, |
|
70
|
|
|
'uri' => $this->uri, |
|
71
|
|
|
'call' => $this->call, |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|