summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Configuration/Configuration.php
blob: 58673fd9804d1b7cb77d34ba81f07fb821481db7 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php

declare(strict_types=1);

namespace OpenTelemetry\SDK\Common\Configuration;

use InvalidArgumentException;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\SDK\Common\Configuration\Parser\BooleanParser;
use OpenTelemetry\SDK\Common\Configuration\Parser\ListParser;
use OpenTelemetry\SDK\Common\Configuration\Parser\MapParser;
use OpenTelemetry\SDK\Common\Configuration\Parser\RatioParser;
use OpenTelemetry\SDK\Common\Configuration\Resolver\CompositeResolver;
use OpenTelemetry\SDK\Common\Util\ClassConstantAccessor;
use UnexpectedValueException;

/**
 * Configuration can come from one or more of the following sources (from highest to lowest priority):
 * - values defined in php.ini
 * - environment variable ($_SERVER)
 * - configuration file (todo)
 */
class Configuration
{
    use LogsMessagesTrait;

    public static function has(string $name): bool
    {
        return CompositeResolver::instance()->hasVariable($name);
    }

    public static function getInt(string $key, int $default = null): int
    {
        return (int) self::validateVariableValue(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::INTEGER),
                $default
            ),
            FILTER_VALIDATE_INT
        );
    }

    public static function getString(string $key, string $default = null): string
    {
        return (string) self::validateVariableValue(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::STRING),
                $default
            )
        );
    }

    public static function getBoolean(string $key, bool $default = null): bool
    {
        $resolved = self::validateVariableValue(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::BOOL),
                null === $default ? $default : ($default ? 'true' : 'false')
            )
        );

        try {
            return BooleanParser::parse($resolved);
        } catch (InvalidArgumentException $e) {
            self::logWarning(sprintf('Invalid boolean value "%s" interpreted as "false" for %s', $resolved, $key));

            return false;
        }
    }

    public static function getMixed(string $key, $default = null)
    {
        return self::validateVariableValue(
            CompositeResolver::instance()->resolve(
                $key,
                $default
            )
        );
    }

    public static function getMap(string $key, array $default = null): array
    {
        return MapParser::parse(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::MAP),
                $default
            )
        );
    }

    public static function getList(string $key, array $default = null): array
    {
        return ListParser::parse(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::LIST),
                $default
            )
        );
    }

    public static function getEnum(string $key, string $default = null): string
    {
        return (string) self::validateVariableValue(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::ENUM),
                $default
            )
        );
    }

    public static function getFloat(string $key, float $default = null): float
    {
        return (float) self::validateVariableValue(
            CompositeResolver::instance()->resolve(
                self::validateVariableType($key, VariableTypes::FLOAT),
                $default
            ),
            FILTER_VALIDATE_FLOAT
        );
    }

    public static function getRatio(string $key, float $default = null): float
    {
        return RatioParser::parse(
            self::validateVariableValue(
                CompositeResolver::instance()->resolve(
                    self::validateVariableType($key, VariableTypes::RATIO),
                    $default
                )
            )
        );
    }

    public static function getKnownValues(string $variableName): ?array
    {
        return ClassConstantAccessor::getValue(KnownValues::class, $variableName);
    }

    public static function getDefault(string $variableName)
    {
        return ClassConstantAccessor::getValue(Defaults::class, $variableName);
    }

    public static function getType(string $variableName): ?string
    {
        return ClassConstantAccessor::getValue(ValueTypes::class, $variableName);
    }

    public static function isEmpty($value): bool
    {
        // don't use 'empty()', since '0' is not considered to be empty
        return $value === null || $value === '';
    }

    private static function validateVariableType(string $variableName, string $type): string
    {
        $variableType = self::getType($variableName);

        if ($variableType !== null && $variableType !== $type && $variableType !== VariableTypes::MIXED) {
            throw new UnexpectedValueException(
                sprintf('Variable "%s" is not supposed to be of type "%s" but type "%s"', $variableName, $type, $variableType)
            );
        }

        return $variableName;
    }

    private static function validateVariableValue($value, ?int $filterType = null)
    {
        if ($filterType !== null && filter_var($value, $filterType) === false) {
            throw new UnexpectedValueException(sprintf('Value has invalid type "%s"', gettype($value)));
        }

        if ($value === null || $value === '') {
            throw new UnexpectedValueException(
                'Variable must not be null or empty'
            );
        }

        return $value;
    }
}