Skip to content

Commit aafdde5

Browse files
committed
refactor: collect only specific env vars in EnvConfigLoader
- EnvConfigLoader::loadDatabaseConfig() now collects only database-related PDODB_* variables instead of all PDODB_* variables - BaseCliCommand::loadCacheConfig() now collects only PDODB_CACHE_* variables instead of all PDODB_* variables - This prevents environment variable pollution from other tests - Fixes Oracle tests failing due to PDODB_CHARSET=utf8mb4 from MySQL tests
1 parent 60e0066 commit aafdde5

File tree

3 files changed

+30
-45
lines changed

3 files changed

+30
-45
lines changed

src/cli/BaseCliCommand.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -257,29 +257,9 @@ protected static function buildConfigFromEnv(string $driver): ?array
257257
*/
258258
protected static function loadCacheConfig(array $dbConfig): array
259259
{
260-
// Collect environment variables
260+
// Collect only cache-related environment variables
261261
// Always use getenv() to ensure we get variables set via putenv()
262262
// $_ENV may not be updated when putenv() is called
263-
$envVars = [];
264-
// Check $_ENV first (most reliable)
265-
foreach ($_ENV as $key => $value) {
266-
if (is_string($key) && str_starts_with($key, 'PDODB_')) {
267-
$envValue = getenv($key);
268-
if ($envValue !== false) {
269-
$envVars[$key] = $envValue;
270-
}
271-
}
272-
}
273-
// Also check $_SERVER for variables that might not be in $_ENV
274-
foreach ($_SERVER as $key => $value) {
275-
if (is_string($key) && str_starts_with($key, 'PDODB_') && !isset($envVars[$key])) {
276-
$envValue = getenv($key);
277-
if ($envValue !== false) {
278-
$envVars[$key] = $envValue;
279-
}
280-
}
281-
}
282-
// Also check common PDODB_CACHE_* variables directly via getenv() to catch putenv() calls
283263
$cacheEnvVars = [
284264
'PDODB_CACHE_ENABLED',
285265
'PDODB_CACHE_TYPE',
@@ -293,12 +273,12 @@ protected static function loadCacheConfig(array $dbConfig): array
293273
'PDODB_CACHE_MEMCACHED_SERVERS',
294274
'PDODB_CACHE_NAMESPACE',
295275
];
276+
277+
$envVars = [];
296278
foreach ($cacheEnvVars as $key) {
297-
if (!isset($envVars[$key])) {
298-
$envValue = getenv($key);
299-
if ($envValue !== false) {
300-
$envVars[$key] = $envValue;
301-
}
279+
$envValue = getenv($key);
280+
if ($envValue !== false) {
281+
$envVars[$key] = $envValue;
302282
}
303283
}
304284

src/connection/EnvConfigLoader.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,30 @@ public static function loadDatabaseConfig(?string $envPath = null): array
3838
// Validate that required extension is available
3939
\tommyknocker\pdodb\connection\ExtensionChecker::validate($driver);
4040

41-
// Collect all PDODB_* environment variables
42-
$envVars = [];
41+
// Collect only database-related PDODB_* environment variables
4342
// Always use getenv() to ensure we get variables set via putenv()
4443
// $_ENV may not be updated when putenv() is called
45-
// Collect all PDODB_* variables from environment
46-
// Check $_ENV first (most reliable)
47-
foreach ($_ENV as $key => $value) {
48-
if (is_string($key) && str_starts_with($key, 'PDODB_')) {
49-
$envValue = getenv($key);
50-
if ($envValue !== false) {
51-
$envVars[$key] = $envValue;
52-
}
53-
}
54-
}
55-
// Also check $_SERVER for variables that might not be in $_ENV
56-
foreach ($_SERVER as $key => $value) {
57-
if (is_string($key) && str_starts_with($key, 'PDODB_') && !isset($envVars[$key])) {
58-
$envValue = getenv($key);
59-
if ($envValue !== false) {
60-
$envVars[$key] = $envValue;
61-
}
44+
$dbEnvVars = [
45+
'PDODB_DRIVER',
46+
'PDODB_HOST',
47+
'PDODB_PORT',
48+
'PDODB_DATABASE',
49+
'PDODB_DB', // Alternative name
50+
'PDODB_USERNAME',
51+
'PDODB_USER', // Alternative name
52+
'PDODB_PASSWORD',
53+
'PDODB_CHARSET',
54+
'PDODB_PREFIX',
55+
'PDODB_SERVICE_NAME',
56+
'PDODB_SID',
57+
'PDODB_PATH', // For SQLite
58+
];
59+
60+
$envVars = [];
61+
foreach ($dbEnvVars as $key) {
62+
$envValue = getenv($key);
63+
if ($envValue !== false) {
64+
$envVars[$key] = $envValue;
6265
}
6366
}
6467

tests/shared/InitCommandCliTests.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ protected function tearDown(): void
3434
putenv('PDODB_NON_INTERACTIVE');
3535
putenv('PDODB_DRIVER');
3636
putenv('PDODB_PATH');
37+
putenv('PDODB_CHARSET');
38+
unset($_ENV['PDODB_CHARSET']);
3739
if (is_dir($this->tempDir)) {
3840
$this->removeDirectory($this->tempDir);
3941
}

0 commit comments

Comments
 (0)