-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiberBoundContextStorageExecutionAwareBC.php
74 lines (60 loc) · 1.94 KB
/
FiberBoundContextStorageExecutionAwareBC.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace OpenTelemetry\Context;
/**
* @internal
*/
final class FiberBoundContextStorageExecutionAwareBC implements ContextStorageInterface, ExecutionContextAwareInterface
{
private readonly FiberBoundContextStorage $storage;
private ?ContextStorage $bc = null;
public function __construct()
{
$this->storage = new FiberBoundContextStorage();
}
public function fork(int|string $id): void
{
$this->bcStorage()->fork($id);
}
public function switch(int|string $id): void
{
$this->bcStorage()->switch($id);
}
public function destroy(int|string $id): void
{
$this->bcStorage()->destroy($id);
}
private function bcStorage(): ContextStorage
{
if ($this->bc === null) {
$this->bc = new ContextStorage();
// Copy head into $this->bc storage to preserve already attached scopes
/** @psalm-suppress PossiblyNullFunctionCall */
$head = (static fn ($storage) => $storage->heads[$storage])
->bindTo(null, FiberBoundContextStorage::class)($this->storage);
$head->storage = $this->bc;
/** @psalm-suppress PossiblyNullFunctionCall */
(static fn ($storage) => $storage->current = $storage->main = $head)
->bindTo(null, ContextStorage::class)($this->bc);
}
return $this->bc;
}
public function scope(): ?ContextStorageScopeInterface
{
return $this->bc
? $this->bc->scope()
: $this->storage->scope();
}
public function current(): ContextInterface
{
return $this->bc
? $this->bc->current()
: $this->storage->current();
}
public function attach(ContextInterface $context): ContextStorageScopeInterface
{
return $this->bc
? $this->bc->attach($context)
: $this->storage->attach($context);
}
}