Skip to content

Commit 0a22ea9

Browse files
authored
Merge 22d699e into a60d0ad
2 parents a60d0ad + 22d699e commit 0a22ea9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+570
-397
lines changed

phpstan.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ parameters:
1212
excludePaths:
1313
- tests/TraceContext/W3CTestService
1414
ignoreErrors:
15-
-
16-
message: "#Call to an undefined method .*#"
17-
paths:
18-
- tests/Unit/SDK/Common/Configuration/Resolver/PhpIniResolverTest.php
19-
- tests/Unit/SDK/Common/Configuration/Resolver/CompositeResolverTest.php
2015
-
2116
message: "#Call to an undefined method .*:allows.*#"
2217
paths:

src/SDK/Common/Configuration/Configuration.php renamed to src/API/Configuration.php

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration;
5+
namespace OpenTelemetry\API;
66

77
use InvalidArgumentException;
88
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
9-
use OpenTelemetry\SDK\Common\Configuration\Parser\BooleanParser;
10-
use OpenTelemetry\SDK\Common\Configuration\Parser\ListParser;
11-
use OpenTelemetry\SDK\Common\Configuration\Parser\MapParser;
12-
use OpenTelemetry\SDK\Common\Configuration\Parser\RatioParser;
13-
use OpenTelemetry\SDK\Common\Configuration\Resolver\CompositeResolver;
14-
use OpenTelemetry\SDK\Common\Util\ClassConstantAccessor;
9+
use OpenTelemetry\API\Configuration\Parser\BooleanParser;
10+
use OpenTelemetry\API\Configuration\Parser\ListParser;
11+
use OpenTelemetry\API\Configuration\Parser\MapParser;
12+
use OpenTelemetry\API\Configuration\Parser\RatioParser;
13+
use OpenTelemetry\API\Configuration\Resolver\CompositeResolver;
14+
use OpenTelemetry\API\Configuration\VariableTypes;
1515
use UnexpectedValueException;
1616

1717
/**
1818
* Configuration can come from one or more of the following sources (from highest to lowest priority):
19-
* - values defined in php.ini
20-
* - environment variable ($_SERVER)
21-
* - configuration file (todo)
19+
* - values defined in `php.ini`
20+
* - environment variable (`$_SERVER` or `getenv`)
21+
* - `.env` file
22+
*
23+
* @psalm-internal \OpenTelemetry
2224
*/
2325
class Configuration
2426
{
@@ -33,8 +35,8 @@ public static function getInt(string $key, int $default = null): int
3335
{
3436
return (int) self::validateVariableValue(
3537
CompositeResolver::instance()->resolve(
36-
self::validateVariableType($key, VariableTypes::INTEGER),
37-
$default
38+
static::validateVariableType($key, VariableTypes::INTEGER),
39+
static::getDefault($key, $default)
3840
),
3941
FILTER_VALIDATE_INT
4042
);
@@ -44,18 +46,21 @@ public static function getString(string $key, string $default = null): string
4446
{
4547
return (string) self::validateVariableValue(
4648
CompositeResolver::instance()->resolve(
47-
self::validateVariableType($key, VariableTypes::STRING),
48-
$default
49+
static::validateVariableType($key, VariableTypes::STRING),
50+
static::getDefault($key, $default)
4951
)
5052
);
5153
}
5254

5355
public static function getBoolean(string $key, bool $default = null): bool
5456
{
57+
if ($default !== null) {
58+
$default = $default ? 'true' : 'false';
59+
}
5560
$resolved = self::validateVariableValue(
5661
CompositeResolver::instance()->resolve(
57-
self::validateVariableType($key, VariableTypes::BOOL),
58-
null === $default ? $default : ($default ? 'true' : 'false')
62+
static::validateVariableType($key, VariableTypes::BOOL),
63+
static::getDefault($key, $default)
5964
)
6065
);
6166

@@ -73,7 +78,7 @@ public static function getMixed(string $key, $default = null)
7378
return self::validateVariableValue(
7479
CompositeResolver::instance()->resolve(
7580
$key,
76-
$default
81+
static::getDefault($key, $default)
7782
)
7883
);
7984
}
@@ -82,8 +87,8 @@ public static function getMap(string $key, array $default = null): array
8287
{
8388
return MapParser::parse(
8489
CompositeResolver::instance()->resolve(
85-
self::validateVariableType($key, VariableTypes::MAP),
86-
$default
90+
static::validateVariableType($key, VariableTypes::MAP),
91+
static::getDefault($key, $default)
8792
)
8893
);
8994
}
@@ -92,8 +97,8 @@ public static function getList(string $key, array $default = null): array
9297
{
9398
return ListParser::parse(
9499
CompositeResolver::instance()->resolve(
95-
self::validateVariableType($key, VariableTypes::LIST),
96-
$default
100+
static::validateVariableType($key, VariableTypes::LIST),
101+
static::getDefault($key, $default)
97102
)
98103
);
99104
}
@@ -102,8 +107,8 @@ public static function getEnum(string $key, string $default = null): string
102107
{
103108
return (string) self::validateVariableValue(
104109
CompositeResolver::instance()->resolve(
105-
self::validateVariableType($key, VariableTypes::ENUM),
106-
$default
110+
static::validateVariableType($key, VariableTypes::ENUM),
111+
static::getDefault($key, $default)
107112
)
108113
);
109114
}
@@ -112,8 +117,8 @@ public static function getFloat(string $key, float $default = null): float
112117
{
113118
return (float) self::validateVariableValue(
114119
CompositeResolver::instance()->resolve(
115-
self::validateVariableType($key, VariableTypes::FLOAT),
116-
$default
120+
static::validateVariableType($key, VariableTypes::FLOAT),
121+
static::getDefault($key, $default)
117122
),
118123
FILTER_VALIDATE_FLOAT
119124
);
@@ -124,26 +129,21 @@ public static function getRatio(string $key, float $default = null): float
124129
return RatioParser::parse(
125130
self::validateVariableValue(
126131
CompositeResolver::instance()->resolve(
127-
self::validateVariableType($key, VariableTypes::RATIO),
128-
$default
132+
static::validateVariableType($key, VariableTypes::RATIO),
133+
static::getDefault($key, $default)
129134
)
130135
)
131136
);
132137
}
133138

134-
public static function getKnownValues(string $variableName): ?array
139+
public static function getDefault(string $key, $default)
135140
{
136-
return ClassConstantAccessor::getValue(KnownValues::class, $variableName);
137-
}
138-
139-
public static function getDefault(string $variableName)
140-
{
141-
return ClassConstantAccessor::getValue(Defaults::class, $variableName);
141+
return $default;
142142
}
143143

144144
public static function getType(string $variableName): ?string
145145
{
146-
return ClassConstantAccessor::getValue(ValueTypes::class, $variableName);
146+
return VariableTypes::MIXED;
147147
}
148148

149149
public static function isEmpty($value): bool
@@ -152,16 +152,8 @@ public static function isEmpty($value): bool
152152
return $value === null || $value === '';
153153
}
154154

155-
private static function validateVariableType(string $variableName, string $type): string
155+
protected static function validateVariableType(string $variableName, string $type): string
156156
{
157-
$variableType = self::getType($variableName);
158-
159-
if ($variableType !== null && $variableType !== $type && $variableType !== VariableTypes::MIXED) {
160-
throw new UnexpectedValueException(
161-
sprintf('Variable "%s" is not supposed to be of type "%s" but type "%s"', $variableName, $type, $variableType)
162-
);
163-
}
164-
165157
return $variableName;
166158
}
167159

src/SDK/Common/Util/ClassConstantAccessor.php renamed to src/API/Configuration/ClassConstantAccessor.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Util;
5+
namespace OpenTelemetry\API\Configuration;
66

77
use LogicException;
88

9+
/**
10+
* @psalm-internal \OpenTelemetry
11+
*/
912
class ClassConstantAccessor
1013
{
1114
public static function requireValue(string $className, string $constantName)

src/SDK/Common/Configuration/Parser/BooleanParser.php renamed to src/API/Configuration/Parser/BooleanParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration\Parser;
5+
namespace OpenTelemetry\API\Configuration\Parser;
66

77
use InvalidArgumentException;
88

src/SDK/Common/Configuration/Parser/ListParser.php renamed to src/API/Configuration/Parser/ListParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration\Parser;
5+
namespace OpenTelemetry\API\Configuration\Parser;
66

77
class ListParser
88
{

src/SDK/Common/Configuration/Parser/MapParser.php renamed to src/API/Configuration/Parser/MapParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration\Parser;
5+
namespace OpenTelemetry\API\Configuration\Parser;
66

77
use InvalidArgumentException;
88

src/SDK/Common/Configuration/Parser/RatioParser.php renamed to src/API/Configuration/Parser/RatioParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration\Parser;
5+
namespace OpenTelemetry\API\Configuration\Parser;
66

77
use InvalidArgumentException;
88
use RangeException;

src/SDK/Common/Configuration/Resolver/CompositeResolver.php renamed to src/API/Configuration/Resolver/CompositeResolver.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration\Resolver;
6-
7-
use OpenTelemetry\SDK\Common\Configuration\Configuration;
5+
namespace OpenTelemetry\API\Configuration\Resolver;
86

97
/**
10-
* @interal
8+
* @psalm-internal \OpenTelemetry\API\Configuration
119
*/
1210
class CompositeResolver
1311
{
@@ -20,6 +18,7 @@ public static function instance(): self
2018
$instance ??= new self([
2119
new PhpIniResolver(),
2220
new EnvironmentResolver(),
21+
new DotEnvResolver(),
2322
]);
2423

2524
return $instance;
@@ -50,9 +49,7 @@ public function resolve(string $variableName, $default = '')
5049
}
5150
}
5251

53-
return Configuration::isEmpty($default)
54-
? Configuration::getDefault($variableName)
55-
: $default;
52+
return $default;
5653
}
5754

5855
public function hasVariable(string $variableName): bool
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\API\Configuration\Resolver;
6+
7+
/**
8+
* Resolve variables from a `.env` file
9+
* @psalm-internal \OpenTelemetry\API\Configuration
10+
*/
11+
class DotEnvResolver implements ResolverInterface
12+
{
13+
private array $values = [];
14+
15+
public function __construct(string $path = null)
16+
{
17+
$path ??= getcwd();
18+
$filename = "{$path}/.env";
19+
if (file_exists($filename)) {
20+
$fp = fopen($filename, 'r');
21+
while (($buffer = fgets($fp, 4096)) !== false) {
22+
if (preg_match('/^ *#.*/', $buffer)) {
23+
continue;
24+
}
25+
[$key, $value] = $this->split($buffer);
26+
if ($key && $value) {
27+
$this->values[$key] = $value;
28+
}
29+
}
30+
}
31+
}
32+
33+
public function retrieveValue(string $variableName)
34+
{
35+
return $this->values[$variableName];
36+
}
37+
38+
public function hasVariable(string $variableName): bool
39+
{
40+
return array_key_exists($variableName, $this->values);
41+
}
42+
43+
private function split(string $line): array
44+
{
45+
$pos = strpos($line, '#'); //start of comment
46+
if ($pos !== false) {
47+
$line = substr($line, 0, $pos);
48+
}
49+
$parts = explode('=', $line, 2);
50+
if (count($parts) !== 2) {
51+
return [false, false];
52+
}
53+
54+
return array_map('trim', $parts);
55+
}
56+
}

src/SDK/Common/Configuration/Resolver/EnvironmentResolver.php renamed to src/API/Configuration/Resolver/EnvironmentResolver.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace OpenTelemetry\SDK\Common\Configuration\Resolver;
5+
namespace OpenTelemetry\API\Configuration\Resolver;
66

7-
use OpenTelemetry\SDK\Common\Configuration\Configuration;
7+
use OpenTelemetry\API\Configuration;
88

99
/**
10-
* @internal
10+
* Resolve variables from environment, via $_SERVER then `getenv()`
11+
* @psalm-internal \OpenTelemetry\API\Configuration
1112
*/
1213
class EnvironmentResolver implements ResolverInterface
1314
{

0 commit comments

Comments
 (0)