diff --git a/src/Client/GRPC/BaseClient.php b/src/Client/GRPC/BaseClient.php index 50e63b77..a495960c 100644 --- a/src/Client/GRPC/BaseClient.php +++ b/src/Client/GRPC/BaseClient.php @@ -166,6 +166,30 @@ public static function createSSL( return new static(static fn(): WorkflowServiceClient => new WorkflowServiceClient($address, $options)); } + /** + * Create a new connection with settings from environment variables. + * It automatically detects if SSL connection is required. + * + * @psalm-suppress RiskyTruthyFalsyComparison + */ + public static function createDefaultConnection(): static + { + $address = \getenv('TEMPORAL_ADDRESS') ?: '127.0.0.1:7233'; + $authKey = \getenv('TEMPORAL_AUTH_KEY') ?: ''; + $sslPrivateKey = \getenv('TEMPORAL_SSL_PRIVATE_KEY') ?: null; + $sslCertChain = \getenv('TEMPORAL_SSL_CERTIFICATE_CHAIN') ?: null; + + // Check SSL connection is required + $useSsl = $authKey !== '' || $sslPrivateKey !== null || $sslCertChain !== null; + + if (!$useSsl) { + return static::create($address); + } + + return static::createSSL($address, clientKey: $sslPrivateKey, clientPem: $sslCertChain) + ->withAuthKey($authKey); + } + /** * @param null|Pipeline $pipeline *