mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-21 07:35:34 +08:00
add environment variables support (#255)
This commit is contained in:
@@ -4,13 +4,20 @@ declare(strict_types=1);
|
||||
|
||||
namespace ZM\Bootstrap;
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
use OneBot\Driver\Workerman\Worker;
|
||||
use ZM\Config\Environment;
|
||||
use ZM\Config\EnvironmentInterface;
|
||||
use ZM\Config\ZMConfig;
|
||||
|
||||
class LoadConfiguration
|
||||
{
|
||||
public function bootstrap(array $config): void
|
||||
{
|
||||
// TODO: 重新思考容器绑定的加载方式,从而在此处使用 interface
|
||||
$env = resolve(Environment::class);
|
||||
$this->loadEnvVariables($env);
|
||||
|
||||
$config_i = config();
|
||||
$config_i->addConfigPath($this->getConfigDir($config));
|
||||
$config_i->setEnvironment($this->getConfigEnvironment($config));
|
||||
@@ -68,4 +75,20 @@ class LoadConfiguration
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadEnvVariables(EnvironmentInterface $env): void
|
||||
{
|
||||
$dotenv_path = $env->get('DOTENV_PATH', SOURCE_ROOT_DIR . '/.env');
|
||||
|
||||
if (!file_exists($dotenv_path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$path = dirname($dotenv_path);
|
||||
$file = basename($dotenv_path);
|
||||
|
||||
foreach (Dotenv::createImmutable($path, $file)->load() as $key => $value) {
|
||||
$env->set($key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user