Skip to content

Commit 01f32d3

Browse files
Configuration\CompositeResolver and SPI discovery (#1523)
* Configuration\CompositeResolver can discover additional configuration resolvers via SPI. * Apply suggestions from code review Co-authored-by: Tobias Bachert <[email protected]> * Fix imports. * Porting EnvSourceProvider changes from Nevay. * Moved VlucasPhpdotenvProvider to sdk-configuration. * Added SdkConfigurationResolver. * Added SymfonyDotenvProvider. * Removed vlucas/phpdotenv dev package. * make style 😎 * Update deptrac for dotenv providers. * Suppress Psalm errors for optional packages. * Added phpstan ignoreErrors rules for optional dependency behavior. * Added test for SPI-backed configuration loading via EnvSourceProvider * Restructure SPI based test for EnvSourceProvider. * Bumped SPI dependency. * Added implied mixed return type to ResolverInterface::retrieveValue --------- Co-authored-by: Tobias Bachert <[email protected]>
1 parent 107a5ea commit 01f32d3

File tree

6 files changed

+57
-13
lines changed

6 files changed

+57
-13
lines changed

Common/Configuration/Resolver/CompositeResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace OpenTelemetry\SDK\Common\Configuration\Resolver;
66

7+
use Nevay\SPI\ServiceLoader;
78
use OpenTelemetry\SDK\Common\Configuration\Configuration;
89

910
/**
@@ -18,6 +19,7 @@ public static function instance(): self
1819
{
1920
static $instance;
2021
$instance ??= new self([
22+
...ServiceLoader::load(ResolverInterface::class),
2123
new EnvironmentResolver(),
2224
new PhpIniResolver(),
2325
]);

Common/Configuration/Resolver/EnvironmentResolver.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ public function hasVariable(string $variableName): bool
2525
return !Configuration::isEmpty($env);
2626
}
2727

28-
/**
29-
* @psalm-suppress InvalidReturnStatement
30-
* @psalm-suppress InvalidReturnType
31-
*/
32-
#[\Override]
33-
public function retrieveValue(string $variableName)
28+
public function retrieveValue(string $variableName): mixed
3429
{
3530
$value = getenv($variableName);
3631
if ($value === false) {

Common/Configuration/Resolver/PhpIniResolver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct(private readonly PhpIniAccessor $accessor = new PhpI
1717
}
1818

1919
#[\Override]
20-
public function retrieveValue(string $variableName)
20+
public function retrieveValue(string $variableName): mixed
2121
{
2222
$value = $this->accessor->get($variableName) ?: '';
2323
if (is_array($value)) {
@@ -27,7 +27,6 @@ public function retrieveValue(string $variableName)
2727
return $value;
2828
}
2929

30-
#[\Override]
3130
public function hasVariable(string $variableName): bool
3231
{
3332
$value = $this->accessor->get($variableName);

Common/Configuration/Resolver/ResolverInterface.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
interface ResolverInterface
88
{
9-
/**
10-
* @return mixed
11-
*/
12-
public function retrieveValue(string $variableName);
9+
public function retrieveValue(string $variableName): mixed;
1310

1411
public function hasVariable(string $variableName): bool;
1512
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\SDK\Common\Configuration\Resolver;
6+
7+
use Nevay\SPI\ServiceLoader;
8+
use Nevay\SPI\ServiceProviderDependency\PackageDependency;
9+
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider;
10+
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceReader;
11+
use OpenTelemetry\Config\SDK\Configuration\Environment\LazyEnvSource;
12+
use OpenTelemetry\Config\SDK\Configuration\Environment\PhpIniEnvSource;
13+
use OpenTelemetry\Config\SDK\Configuration\Environment\ServerEnvSource;
14+
use OpenTelemetry\SDK\Common\Configuration\Configuration;
15+
16+
/**
17+
* @internal
18+
*/
19+
#[PackageDependency('open-telemetry/sdk-configuration', '*')]
20+
class SdkConfigurationResolver implements ResolverInterface
21+
{
22+
private readonly EnvSourceReader $reader;
23+
24+
public function __construct()
25+
{
26+
$envSources = [];
27+
28+
/** @var EnvSourceProvider $envSourceProvider */
29+
foreach (ServiceLoader::load(EnvSourceProvider::class) as $envSourceProvider) {
30+
$envSources[] = new LazyEnvSource($envSourceProvider->getEnvSource(...));
31+
}
32+
33+
$envSources[] = new ServerEnvSource();
34+
$envSources[] = new PhpIniEnvSource();
35+
36+
$this->reader = new EnvSourceReader($envSources);
37+
}
38+
39+
public function retrieveValue(string $variableName): mixed
40+
{
41+
return $this->reader->read($variableName);
42+
}
43+
44+
public function hasVariable(string $variableName): bool
45+
{
46+
return !Configuration::isEmpty($this->reader->read($variableName));
47+
}
48+
}

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"ramsey/uuid": "^3.0 || ^4.0",
3333
"symfony/polyfill-mbstring": "^1.23",
3434
"symfony/polyfill-php82": "^1.26",
35-
"tbachert/spi": "^1.0.1"
35+
"tbachert/spi": "^1.0.5"
3636
},
3737
"autoload": {
3838
"psr-4": {
@@ -64,6 +64,9 @@
6464
"OpenTelemetry\\API\\Configuration\\ConfigEnv\\EnvComponentLoader": [
6565
"OpenTelemetry\\API\\Instrumentation\\Configuration\\General\\ConfigEnv\\EnvComponentLoaderHttpConfig",
6666
"OpenTelemetry\\API\\Instrumentation\\Configuration\\General\\ConfigEnv\\EnvComponentLoaderPeerConfig"
67+
],
68+
"OpenTelemetry\\SDK\\Common\\Configuration\\Resolver\\ResolverInterface": [
69+
"OpenTelemetry\\SDK\\Common\\Configuration\\Resolver\\SdkConfigurationResolver"
6770
]
6871
}
6972
}

0 commit comments

Comments
 (0)