Skip to content

Commit 0bceb35

Browse files
committed
refactor(ConfigEnv): replace EnvProvider with array for environment variables
1 parent e20e7f6 commit 0bceb35

File tree

7 files changed

+167
-320
lines changed

7 files changed

+167
-320
lines changed

src/Common/EnvConfig/Client/ConfigEnv.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Temporal\Common\EnvConfig\Client;
66

7-
use Temporal\Common\EnvConfig\EnvProvider;
8-
97
/**
108
* Environment variable configuration parser for Temporal client.
119
*
@@ -74,26 +72,26 @@ private function __construct(
7472
$this->configFile = $configFile === '' ? null : $configFile;
7573
}
7674

77-
public static function fromEnvProvider(EnvProvider $env): self
75+
public static function fromEnv(array $env): self
7876
{
7977
return new self(
8078
new ConfigProfile(
81-
address: $env->get('TEMPORAL_ADDRESS'),
82-
namespace: $env->get('TEMPORAL_NAMESPACE'),
83-
apiKey: $env->get('TEMPORAL_API_KEY'),
79+
address: $env['TEMPORAL_ADDRESS'] ?? null,
80+
namespace: $env['TEMPORAL_NAMESPACE'] ?? null,
81+
apiKey: $env['TEMPORAL_API_KEY'] ?? null,
8482
tlsConfig: self::fetchTlsConfig($env),
8583
grpcMeta: self::fetchGrpcMeta($env),
8684
codecConfig: self::fetchCodecConfig($env),
8785
),
88-
$env->get('TEMPORAL_PROFILE'),
89-
$env->get('TEMPORAL_CONFIG_FILE'),
86+
$env['TEMPORAL_PROFILE'] ?? null,
87+
$env['TEMPORAL_CONFIG_FILE'] ?? null,
9088
);
9189
}
9290

93-
private static function fetchTlsConfig(EnvProvider $env): ?ConfigTls
91+
private static function fetchTlsConfig(array $env): ?ConfigTls
9492
{
95-
$tls = $env->get('TEMPORAL_TLS');
96-
$tlsVars = $env->getByPrefix('TEMPORAL_TLS_', stripPrefix: true);
93+
$tls = $env['TEMPORAL_TLS'] ?? null;
94+
$tlsVars = self::getByPrefix($env, 'TEMPORAL_TLS_', stripPrefix: true);
9795

9896
// If no TLS-related variables are set, return null
9997
if ($tls === null && $tlsVars === []) {
@@ -143,9 +141,9 @@ private static function fetchTlsConfig(EnvProvider $env): ?ConfigTls
143141
*
144142
* @return array<non-empty-string, string>
145143
*/
146-
private static function fetchGrpcMeta(EnvProvider $env): array
144+
private static function fetchGrpcMeta(array $env): array
147145
{
148-
$meta = $env->getByPrefix('TEMPORAL_GRPC_META_', stripPrefix: true);
146+
$meta = self::getByPrefix($env, 'TEMPORAL_GRPC_META_', stripPrefix: true);
149147
$result = [];
150148

151149
foreach ($meta as $key => $value) {
@@ -165,10 +163,10 @@ private static function fetchGrpcMeta(EnvProvider $env): array
165163
*
166164
* @return ConfigCodec|null Codec configuration or null if no codec env vars are set
167165
*/
168-
private static function fetchCodecConfig(EnvProvider $env): ?ConfigCodec
166+
private static function fetchCodecConfig(array $env): ?ConfigCodec
169167
{
170-
$endpoint = $env->get('TEMPORAL_CODEC_ENDPOINT');
171-
$auth = $env->get('TEMPORAL_CODEC_AUTH');
168+
$endpoint = $env['TEMPORAL_CODEC_ENDPOINT'] ?? null;
169+
$auth = $env['TEMPORAL_CODEC_AUTH'] ?? null;
172170

173171
// Return null if both are not set
174172
if ($endpoint === null && $auth === null) {
@@ -180,4 +178,27 @@ private static function fetchCodecConfig(EnvProvider $env): ?ConfigCodec
180178
auth: $auth,
181179
);
182180
}
181+
182+
/**
183+
* Get environment variables by prefix.
184+
*
185+
* @param array $env Environment variables array
186+
* @param string $prefix Prefix to filter by
187+
* @param bool $stripPrefix Whether to strip the prefix from result keys
188+
* @return array<string, string>
189+
*/
190+
private static function getByPrefix(array $env, string $prefix, bool $stripPrefix = false): array
191+
{
192+
$result = [];
193+
$prefixLen = \strlen($prefix);
194+
195+
foreach ($env as $key => $value) {
196+
if (\str_starts_with($key, $prefix)) {
197+
$resultKey = $stripPrefix ? \substr($key, $prefixLen) : $key;
198+
$result[$resultKey] = $value;
199+
}
200+
}
201+
202+
return $result;
203+
}
183204
}

src/Common/EnvConfig/ConfigClient.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,42 +48,42 @@ private function __construct(
4848
* Loading order (later overrides earlier):
4949
* 1. Profile from TOML file (if $configFile provided, or TEMPORAL_CONFIG_FILE is set,
5050
* or file exists at default platform-specific location)
51-
* 2. Environment variable overrides (if $envProvider provided)
51+
* 2. Environment variable overrides (if $env provided)
5252
*
5353
* @param non-empty-string|null $profileName Profile name to load. If null, uses TEMPORAL_PROFILE
5454
* environment variable or 'default' as fallback.
5555
* @param non-empty-string|null $configFile Path to TOML config file or TOML content string.
5656
* If null, checks TEMPORAL_CONFIG_FILE env var, then checks if file exists at
5757
* default platform-specific location.
58-
* @param EnvProvider $envProvider Environment variable provider for overrides.
59-
* @param bool $strict Whether to use strict TOML parsing (validates mutual exclusivity, etc.)
58+
* @param array $env Environment variables array for overrides.
6059
*
6160
* @throws ProfileNotFoundException If the requested profile is not found
6261
* @throws InvalidConfigException If configuration file is invalid
6362
*/
6463
public static function load(
6564
?string $profileName = null,
6665
?string $configFile = null,
67-
EnvProvider $envProvider = new SystemEnvProvider(),
68-
bool $strict = true,
66+
array $env = [],
6967
): self {
68+
$env = $env ?: \getenv();
69+
7070
// Load environment config first to get profile name if not specified
71-
$envConfig = ConfigEnv::fromEnvProvider($envProvider);
71+
$envConfig = ConfigEnv::fromEnv($env);
7272
$profileName ??= $envConfig->currentProfile ?? 'default';
7373
$profileNameLower = \strtolower($profileName);
7474

7575
// Determine config file path: explicit > env var > default location
7676
$configFile ??= $envConfig->configFile;
7777
if ($configFile === null) {
78-
$configFile = self::getDefaultConfigPath($envProvider);
78+
$configFile = self::getDefaultConfigPath($env);
7979
$configFile === null or \file_exists($configFile) or $configFile = null;
8080
}
8181

8282
// Load from file if it exists
8383
$profile = null;
8484
$profiles = [];
8585
if ($configFile !== null) {
86-
$profiles = self::loadFromToml($configFile, $strict)->profiles;
86+
$profiles = self::loadFromToml($configFile)->profiles;
8787
$profile = $profiles[$profileNameLower] ?? null;
8888
}
8989

@@ -109,11 +109,13 @@ public static function load(
109109
*
110110
* Uses TEMPORAL_* environment variables to construct configuration.
111111
*
112-
* @param EnvProvider $envProvider Environment variable provider.
112+
* @param array $env Environment variables array.
113113
*/
114-
public static function loadFromEnv(EnvProvider $envProvider = new SystemEnvProvider()): self
114+
public static function loadFromEnv(array $env = []): self
115115
{
116-
$envConfig = ConfigEnv::fromEnvProvider($envProvider);
116+
$env = $env ?: \getenv();
117+
118+
$envConfig = ConfigEnv::fromEnv($env);
117119
$profileName = $envConfig->currentProfile ?? 'default';
118120

119121
return new self([$profileName => $envConfig->profile]);
@@ -214,20 +216,20 @@ private static function normalizeProfileNames(array $profiles): array
214216
* Note: This method returns the expected path regardless of whether the file exists.
215217
* The caller is responsible for checking file existence.
216218
*
217-
* @param EnvProvider $envProvider Environment variable provider
219+
* @param array $env Environment variables array
218220
* @return non-empty-string|null Path to default config file, or null if home directory cannot be determined
219221
*/
220-
private static function getDefaultConfigPath(EnvProvider $envProvider): ?string
222+
private static function getDefaultConfigPath(array $env): ?string
221223
{
222-
$home = $envProvider->get('HOME') ?? $envProvider->get('USERPROFILE');
224+
$home = $env['HOME'] ?? $env['USERPROFILE'] ?? null;
223225
if ($home === null) {
224226
return null;
225227
}
226228

227229
$configDir = match (\PHP_OS_FAMILY) {
228-
'Windows' => $envProvider->get('APPDATA') ?? ($home . '\\AppData\\Roaming'),
230+
'Windows' => $env['APPDATA'] ?? ($home . '\\AppData\\Roaming'),
229231
'Darwin' => $home . '/Library/Application Support',
230-
default => $envProvider->get('XDG_CONFIG_HOME') ?? ($home . '/.config'),
232+
default => $env['XDG_CONFIG_HOME'] ?? ($home . '/.config'),
231233
};
232234

233235
return $configDir . \DIRECTORY_SEPARATOR . 'temporalio' . \DIRECTORY_SEPARATOR . 'temporal.toml';

src/Common/EnvConfig/EnvProvider.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Common/EnvConfig/SystemEnvProvider.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)