Skip to content

Environment Variable Support for Client Configuration #1

@neo2ns

Description

@neo2ns

Summary

Add support for configuring the Hlquery client using standard environment variables (e.g., HLQUERY_BASE_URL, HLQUERY_TOKEN). This allows for zero-config instantiation in containerized environments and simplified integration in CI/CD pipelines.

Context

Currently, the Client class defaults to http://localhost:9200 via hardcoded constants in Config.php. While parameters can be passed to the constructor, modern infrastructure (Docker, Kubernetes) relies heavily on environment variables for service discovery and configuration. Adding this "very basic" feature brings the client in line with standard Twelve-Factor App practices and improves the developer experience by removing the need to manually pass configuration in environments where it's already defined.

Proposed Implementation

Modify Hlquery\Utils\Config::mergeDefaults() to check for environment variables using getenv() before falling back to the hardcoded defaults.

public static function mergeDefaults($userOptions = []) {
    return array_merge([
        'timeout'     => self::DEFAULT_TIMEOUT,
        'base_url'    => getenv('HLQUERY_BASE_URL') ?: self::DEFAULT_BASE_URL,
        'auth_method' => getenv('HLQUERY_AUTH_METHOD') ?: self::DEFAULT_AUTH_METHOD,
        'token'       => getenv('HLQUERY_TOKEN') ?: null
    ], $userOptions);
}

This change is non-breaking as user-provided $userOptions will still take precedence due to array_merge.

Impact

  • Improved Developer Experience: Users can simply call new Client() in their applications without worrying about passing parameters if their environment is correctly configured.
  • Easier Deployment: Simplifies deployment across different environments (dev, staging, prod) by using standard environment-based configuration.
  • Standardization: Aligns the PHP client with common patterns found in other major search engine and database clients.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions