Skip to content

Commit 7fcb8ac

Browse files
committed
CHG environment vars reading to greater compatibility
1 parent 0a2872a commit 7fcb8ac

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/ConnectParams.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,20 @@ public function __construct(
3838
$this->instance = $instance;
3939
}
4040

41-
public static function fromEnv(string $alias, ?array $env = null)
41+
public static function fromEnv(string $alias)
4242
{
43-
$env = $env ?? getenv();
4443
$alias = strtoupper($alias);
44+
45+
$driverKey = "DATABASE_{$alias}_HOST";
46+
$driverVal = static::getenv($driverKey);
47+
if (empty($driverVal)) {
48+
throw new LogicException("Missing environment variable {$driverKey}.");
49+
}
50+
$driver = DriverManager::getDriver($driverVal);
51+
if (empty($driver)) {
52+
throw new LogicException("Unknown driver {$driverVal}.");
53+
}
4554

46-
$driver = DriverManager::fromEnv($alias, $env);
4755
$hostKey = "DATABASE_{$alias}_HOST";
4856
$portKey = "DATABASE_{$alias}_PORT";
4957
$userKey = "DATABASE_{$alias}_USER";
@@ -53,21 +61,26 @@ public static function fromEnv(string $alias, ?array $env = null)
5361
$pwflKey = "DATABASE_{$alias}_PSWD_FILE";
5462
$instKey = "DATABASE_{$alias}_INST";
5563

56-
$host = $env[$hostKey] ?? $driver->getDefaultHost();
57-
$port = $env[$portKey] ?? $driver->getDefaultPort();
58-
$user = $env[$userKey] ?? $driver->getDefaultUser();
59-
$name = $env[$nameKey] ?? $alias;
60-
$encd = $env[$encdKey] ?? null;
61-
$inst = $env[$instKey] ?? null;
64+
$host = static::getenv($hostKey) ?? $driver->getDefaultHost();
65+
$port = static::getenv($portKey) ?? $driver->getDefaultPort();
66+
$user = static::getenv($userKey) ?? $driver->getDefaultUser();
67+
$name = static::getenv($nameKey) ?? $alias;
68+
$encd = static::getenv($encdKey) ?? null;
69+
$inst = static::getenv($instKey) ?? null;
6270

63-
$pswd = $env[$pswdKey] ?? $driver->getDefaultPswd();
64-
if (!empty($env[$pwflKey]) && file_exists($env[$pwflKey])) {
65-
$pswd = file_get_contents($env[$pwflKey]);
71+
$pswd = static::getenv($pswdKey) ?? $driver->getDefaultPswd();
72+
if (!empty(static::getenv($pwflKey)) && file_exists(static::getenv($pwflKey))) {
73+
$pswd = file_get_contents(static::getenv($pwflKey));
6674
}
6775

6876
return new ConnectParams($driver, $host, $user, $pswd, $name, $port, $encd, $inst);
6977
}
7078

79+
private static function getenv(string $varname, bool $localOnly = false)
80+
{
81+
return getenv($varname, $localOnly) ?: $_ENV[$varname] ?? null;
82+
}
83+
7184
/**
7285
* @param UriInterface|string $uri
7386
* @return ConnectParams

src/DriverManager.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ public static function getDriverName(DriverInterface $driver): ?string
3939
return empty($name) ? null : reset($name);
4040
}
4141

42+
/**
43+
* @deprecated
44+
*
45+
* @param string $alias
46+
* @param array|null $env
47+
* @return DriverInterface
48+
*/
4249
public static function fromEnv(string $alias, ?array $env = null)
4350
{
51+
$driverKey = "DATABASE_{$alias}_DRIVER";
4452
$env = $env ?? getenv();
4553
$alias = strtoupper($alias);
4654

47-
$driverKey = "DATABASE_{$alias}_DRIVER";
4855
if (!array_key_exists($driverKey, $env)) {
4956
throw new LogicException("Missing {$driverKey} setting in `.env` file.");
5057
}

0 commit comments

Comments
 (0)