summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/api/Baggage/Baggage.php
blob: 06c70160566642243e3048b10cac97e3b0a806f3 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

declare(strict_types=1);

namespace OpenTelemetry\API\Baggage;

use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\Context\ContextKeys;
use OpenTelemetry\Context\ScopeInterface;

final class Baggage implements BaggageInterface
{
    private static ?self $emptyBaggage = null;

    /** @inheritDoc */
    public static function fromContext(ContextInterface $context): BaggageInterface
    {
        return $context->get(ContextKeys::baggage()) ?? self::getEmpty();
    }

    /** @inheritDoc */
    public static function getBuilder(): BaggageBuilderInterface
    {
        return new BaggageBuilder();
    }

    /** @inheritDoc */
    public static function getCurrent(): BaggageInterface
    {
        return self::fromContext(Context::getCurrent());
    }

    /** @inheritDoc */
    public static function getEmpty(): BaggageInterface
    {
        if (null === self::$emptyBaggage) {
            self::$emptyBaggage = new self();
        }

        return self::$emptyBaggage;
    }

    /** @var array<string, Entry> */
    private array $entries;

    /** @param array<string, Entry> $entries */
    public function __construct(array $entries = [])
    {
        $this->entries = $entries;
    }

    /** @inheritDoc */
    public function activate(): ScopeInterface
    {
        return Context::getCurrent()->withContextValue($this)->activate();
    }

    /** @inheritDoc */
    public function getEntry(string $key): ?Entry
    {
        return $this->entries[$key] ?? null;
    }

    /** @inheritDoc */
    public function getValue(string $key)
    {
        if (($entry = $this->getEntry($key)) !== null) {
            return $entry->getValue();
        }

        return null;
    }

    /** @inheritDoc */
    public function getAll(): iterable
    {
        foreach ($this->entries as $key => $entry) {
            yield $key => $entry;
        }
    }

    /** @inheritDoc */
    public function isEmpty(): bool
    {
        return $this->entries === [];
    }

    /** @inheritDoc */
    public function toBuilder(): BaggageBuilderInterface
    {
        return new BaggageBuilder($this->entries);
    }

    /** @inheritDoc */
    public function storeInContext(ContextInterface $context): ContextInterface
    {
        return $context->with(ContextKeys::baggage(), $this);
    }
}