diff options
Diffstat (limited to 'vendor/open-telemetry/sdk/Common/Configuration/Resolver')
5 files changed, 182 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Common/Configuration/Resolver/CompositeResolver.php b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/CompositeResolver.php new file mode 100644 index 000000000..b72400b01 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/CompositeResolver.php @@ -0,0 +1,68 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Configuration\Resolver; + +use OpenTelemetry\SDK\Common\Configuration\Configuration; + +/** + * @interal + */ +class CompositeResolver +{ + // @var array<ResolverInterface> + private array $resolvers = []; + + public static function instance(): self + { + static $instance; + $instance ??= new self([ + new PhpIniResolver(), + new EnvironmentResolver(), + ]); + + return $instance; + } + + public function __construct($resolvers) + { + foreach ($resolvers as $resolver) { + $this->addResolver($resolver); + } + } + + public function addResolver(ResolverInterface $resolver): void + { + $this->resolvers[] = $resolver; + } + + public function getResolvers(): array + { + return $this->resolvers; + } + + public function resolve(string $variableName, $default = '') + { + foreach ($this->resolvers as $resolver) { + if ($resolver->hasVariable($variableName)) { + return $resolver->retrieveValue($variableName); + } + } + + return Configuration::isEmpty($default) + ? Configuration::getDefault($variableName) + : $default; + } + + public function hasVariable(string $variableName): bool + { + foreach ($this->resolvers as $resolver) { + if ($resolver->hasVariable($variableName)) { + return true; + } + } + + return false; + } +} diff --git a/vendor/open-telemetry/sdk/Common/Configuration/Resolver/EnvironmentResolver.php b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/EnvironmentResolver.php new file mode 100644 index 000000000..453f98e39 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/EnvironmentResolver.php @@ -0,0 +1,40 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Configuration\Resolver; + +use OpenTelemetry\SDK\Common\Configuration\Configuration; + +/** + * @internal + */ +class EnvironmentResolver implements ResolverInterface +{ + public function hasVariable(string $variableName): bool + { + if (!Configuration::isEmpty($_SERVER[$variableName] ?? null)) { + return true; + } + $env = getenv($variableName); + if ($env === false) { + return false; + } + + return !Configuration::isEmpty($env); + } + + /** + * @psalm-suppress InvalidReturnStatement + * @psalm-suppress InvalidReturnType + */ + public function retrieveValue(string $variableName) + { + $value = getenv($variableName); + if ($value === false) { + $value = $_SERVER[$variableName] ?? null; + } + + return $value; + } +} diff --git a/vendor/open-telemetry/sdk/Common/Configuration/Resolver/PhpIniAccessor.php b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/PhpIniAccessor.php new file mode 100644 index 000000000..a12b507e8 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/PhpIniAccessor.php @@ -0,0 +1,18 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Configuration\Resolver; + +class PhpIniAccessor +{ + /** + * Mockable accessor for php.ini values + * @internal + * @return array|false|string + */ + public function get(string $variableName) + { + return get_cfg_var($variableName); + } +} diff --git a/vendor/open-telemetry/sdk/Common/Configuration/Resolver/PhpIniResolver.php b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/PhpIniResolver.php new file mode 100644 index 000000000..c9a8f3b4e --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/PhpIniResolver.php @@ -0,0 +1,41 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Configuration\Resolver; + +use OpenTelemetry\SDK\Common\Configuration\Configuration; + +/** + * @interal + * @psalm-suppress TypeDoesNotContainType + */ +class PhpIniResolver implements ResolverInterface +{ + private PhpIniAccessor $accessor; + + public function __construct(?PhpIniAccessor $accessor = null) + { + $this->accessor = $accessor ?? new PhpIniAccessor(); + } + + public function retrieveValue(string $variableName) + { + $value = $this->accessor->get($variableName) ?: ''; + if (is_array($value)) { + return implode(',', $value); + } + + return $value; + } + + public function hasVariable(string $variableName): bool + { + $value = $this->accessor->get($variableName); + if ($value === []) { + return false; + } + + return $value !== false && !Configuration::isEmpty($value); + } +} diff --git a/vendor/open-telemetry/sdk/Common/Configuration/Resolver/ResolverInterface.php b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/ResolverInterface.php new file mode 100644 index 000000000..4e88f3ff6 --- /dev/null +++ b/vendor/open-telemetry/sdk/Common/Configuration/Resolver/ResolverInterface.php @@ -0,0 +1,15 @@ +<?php + +declare(strict_types=1); + +namespace OpenTelemetry\SDK\Common\Configuration\Resolver; + +interface ResolverInterface +{ + /** + * @return mixed + */ + public function retrieveValue(string $variableName); + + public function hasVariable(string $variableName): bool; +} |