Add getEnvVariable utility method to fix Laravel config caching issue#293
Conversation
There was a problem hiding this comment.
PR Summary
Introduces a new environment variable handling utility in WorkOS PHP SDK to fix Laravel config caching compatibility issues. The change improves reliability of configuration retrieval.
- Added new private static
getEnvVariable()method inlib/WorkOS.phpwith cascading fallback (getenv -> $_ENV -> $_SERVER) - Modified
getApiKey()andgetClientId()methods to use the new utility for consistent environment variable handling - Added comprehensive test coverage in
tests/WorkOS/WorkOSTest.phpverifying Laravel config cache compatibility - Ensures compatibility with Laravel's config caching by properly handling $_ENV superglobal access
2 files reviewed, 4 comments
Edit PR Review Bot Settings | Greptile
| * Get environment variable with fallback to cached config sources | ||
| * | ||
| * @param string $key Environment variable name | ||
| * @return string|false The environment variable value or false if not found |
There was a problem hiding this comment.
style: Return type hint could be more specific: '@return string|false The value or false. Empty string is never returned.'
| putenv("WORKOS_API_KEY="); | ||
| putenv("WORKOS_CLIENT_ID="); | ||
|
|
||
| unset($_ENV['WORKOS_API_KEY']); | ||
| unset($_ENV['WORKOS_CLIENT_ID']); | ||
| unset($_SERVER['WORKOS_API_KEY']); | ||
| unset($_SERVER['WORKOS_CLIENT_ID']); |
There was a problem hiding this comment.
style: Redundant cleanup of environment variables across different sources. Consider extracting this to a reusable helper method to avoid duplication with tearDown
| putenv("WORKOS_API_KEY="); | |
| putenv("WORKOS_CLIENT_ID="); | |
| unset($_ENV['WORKOS_API_KEY']); | |
| unset($_ENV['WORKOS_CLIENT_ID']); | |
| unset($_SERVER['WORKOS_API_KEY']); | |
| unset($_SERVER['WORKOS_CLIENT_ID']); | |
| $this->cleanupWorkOSEnvironment(); |
| public function testLaravelConfigCachingScenario() | ||
| { | ||
| $_ENV['WORKOS_API_KEY'] = "pk_test_laravel_cached"; | ||
| $_ENV['WORKOS_CLIENT_ID'] = "client_test_laravel_cached"; | ||
|
|
||
| $this->assertEquals("pk_test_laravel_cached", WorkOS::getApiKey()); | ||
| $this->assertEquals("client_test_laravel_cached", WorkOS::getClientId()); | ||
| } |
There was a problem hiding this comment.
style: Test could be split into two separate test cases for API key and client ID to follow single responsibility principle
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Summary
getEnvVariablemethod with fallback priority:getenv()->$_ENV->$_SERVERgetApiKeyandgetClientIdmethods to use the utilitygetenv()from accessing .env values