-
Notifications
You must be signed in to change notification settings - Fork 214
PoC: Configuration\CompositeResolver and SPI discovery #1523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
507c2b1
Configuration\CompositeResolver can discover additional configuration…
ChrisLightfootWild b905d69
Apply suggestions from code review
ChrisLightfootWild 436598c
Fix imports.
ChrisLightfootWild 1cc833d
Porting EnvSourceProvider changes from Nevay.
ChrisLightfootWild e7e3377
Moved VlucasPhpdotenvProvider to sdk-configuration.
ChrisLightfootWild cfc5075
Added SdkConfigurationResolver.
ChrisLightfootWild 179be68
Added SymfonyDotenvProvider.
ChrisLightfootWild 670c45b
Removed vlucas/phpdotenv dev package.
ChrisLightfootWild a0e7a7e
make style 😎
ChrisLightfootWild 15e4258
Update deptrac for dotenv providers.
ChrisLightfootWild 1f255c9
Suppress Psalm errors for optional packages.
ChrisLightfootWild 43561b2
Added phpstan ignoreErrors rules for optional dependency behavior.
ChrisLightfootWild 40daf2c
Added test for SPI-backed configuration loading via EnvSourceProvider
ChrisLightfootWild a6abd70
Restructure SPI based test for EnvSourceProvider.
ChrisLightfootWild df8d847
Bumped SPI dependency.
ChrisLightfootWild 98b9786
Added implied mixed return type to ResolverInterface::retrieveValue
ChrisLightfootWild 40acb23
Merge remote-tracking branch 'upstream/main' into spi/dotenv
ChrisLightfootWild File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Config/SDK/Configuration/Environment/Adapter/SymfonyDotenvProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Config\SDK\Configuration\Environment\Adapter; | ||
|
|
||
| use function array_diff_key; | ||
| use Composer\InstalledVersions; | ||
| use Nevay\SPI\ServiceProviderDependency\PackageDependency; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\ArrayEnvSource; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSource; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider; | ||
| use Symfony\Component\Dotenv\Dotenv; | ||
| use Symfony\Component\Dotenv\Exception\PathException; | ||
|
|
||
| #[PackageDependency('symfony/dotenv', '^5.4 || ^6.4 || ^7.0')] | ||
| final class SymfonyDotenvProvider implements EnvSourceProvider | ||
| { | ||
| /** @psalm-suppress UndefinedClass */ | ||
| public function getEnvSource(): EnvSource | ||
| { | ||
| $installPath = InstalledVersions::getRootPackage()['install_path']; | ||
|
|
||
| $backup = [$_SERVER, $_ENV]; | ||
| $env = []; | ||
|
|
||
| try { | ||
| (new Dotenv())->bootEnv($installPath . '/.env'); | ||
| $env = $_SERVER; | ||
| } catch (PathException) { | ||
| } finally { | ||
| [$_SERVER, $_ENV] = $backup; | ||
| } | ||
|
|
||
| return new ArrayEnvSource(array_diff_key($env, $_SERVER)); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
src/Config/SDK/Configuration/Environment/Adapter/VlucasPhpdotenvProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Config\SDK\Configuration\Environment\Adapter; | ||
|
|
||
| use Composer\InstalledVersions; | ||
| use Dotenv\Dotenv; | ||
| use Dotenv\Exception\InvalidPathException; | ||
| use Nevay\SPI\ServiceProviderDependency\PackageDependency; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\ArrayEnvSource; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSource; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider; | ||
|
|
||
| #[PackageDependency('vlucas/phpdotenv', '^4.0 || ^5.0')] | ||
| final class VlucasPhpdotenvProvider implements EnvSourceProvider | ||
| { | ||
| /** @psalm-suppress UndefinedClass */ | ||
| public function getEnvSource(): EnvSource | ||
| { | ||
| $backup = [$_SERVER, $_ENV]; | ||
| $env = []; | ||
|
|
||
| try { | ||
| $env = Dotenv::createImmutable([InstalledVersions::getRootPackage()['install_path']])->load(); | ||
| } catch (InvalidPathException) { | ||
| } finally { | ||
| [$_SERVER, $_ENV] = $backup; | ||
| } | ||
|
|
||
| return new ArrayEnvSource(array_diff_key($env, $_SERVER)); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/Config/SDK/Configuration/Environment/EnvSourceProvider.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Config\SDK\Configuration\Environment; | ||
|
|
||
| interface EnvSourceProvider | ||
| { | ||
| public function getEnvSource(): EnvSource; | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/Config/SDK/Configuration/Environment/LazyEnvSource.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Config\SDK\Configuration\Environment; | ||
|
|
||
| use Closure; | ||
|
|
||
| final class LazyEnvSource implements EnvSource | ||
| { | ||
| /** | ||
| * @param Closure(): EnvSource|EnvSource $env | ||
brettmc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| public function __construct( | ||
| private Closure|EnvSource $env, | ||
| ) { | ||
| } | ||
|
|
||
| public function readRaw(string $name): mixed | ||
| { | ||
| if (!$this->env instanceof EnvSource) { | ||
| $this->env = ($this->env)(); | ||
| } | ||
|
|
||
| return $this->env->readRaw($name); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/SDK/Common/Configuration/Resolver/SdkConfigurationResolver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Common\Configuration\Resolver; | ||
|
|
||
| use Nevay\SPI\ServiceLoader; | ||
| use Nevay\SPI\ServiceProviderDependency\PackageDependency; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceReader; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\LazyEnvSource; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\PhpIniEnvSource; | ||
| use OpenTelemetry\Config\SDK\Configuration\Environment\ServerEnvSource; | ||
| use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[PackageDependency('open-telemetry/sdk-configuration', '*')] | ||
| class SdkConfigurationResolver implements ResolverInterface | ||
| { | ||
| private readonly EnvSourceReader $reader; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $envSources = []; | ||
| $envSources[] = new ServerEnvSource(); | ||
| $envSources[] = new PhpIniEnvSource(); | ||
|
|
||
| /** @var EnvSourceProvider $envSourceProvider */ | ||
| foreach (ServiceLoader::load(EnvSourceProvider::class) as $envSourceProvider) { | ||
| $envSources[] = new LazyEnvSource($envSourceProvider->getEnvSource(...)); | ||
brettmc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| $this->reader = new EnvSourceReader($envSources); | ||
| } | ||
|
|
||
| public function retrieveValue(string $variableName) | ||
| { | ||
| return $this->reader->read($variableName); | ||
| } | ||
|
|
||
| public function hasVariable(string $variableName): bool | ||
| { | ||
| return !Configuration::isEmpty($this->reader->read($variableName)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.