@@ -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
0 commit comments