-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextStorage.php
62 lines (50 loc) · 1.45 KB
/
ContextStorage.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
<?php
declare(strict_types=1);
namespace OpenTelemetry\Context;
/**
* @internal
*/
final class ContextStorage implements ContextStorageInterface, ContextStorageHeadAware, ExecutionContextAwareInterface
{
private ContextStorageHead $current;
private ContextStorageHead $main;
/** @var array<int|string, ContextStorageHead> */
private array $forks = [];
public function __construct()
{
$this->current = $this->main = new ContextStorageHead($this);
}
public function fork(int|string $id): void
{
$this->forks[$id] = clone $this->current;
}
public function switch(int|string $id): void
{
$this->current = $this->forks[$id] ?? $this->main;
}
public function destroy(int|string $id): void
{
unset($this->forks[$id]);
}
public function head(): ContextStorageHead
{
return $this->current;
}
public function scope(): ?ContextStorageScopeInterface
{
return ($this->current->node->head ?? null) === $this->current
? $this->current->node
: null;
}
public function current(): ContextInterface
{
return $this->current->node->context ?? Context::getRoot();
}
public function attach(ContextInterface $context): ContextStorageScopeInterface
{
return $this->current->node = new ContextStorageNode($context, $this->current, $this->current->node);
}
private function __clone()
{
}
}