add environment variables support (#255)

This commit is contained in:
sunxyw
2023-01-15 21:30:44 +08:00
committed by GitHub
parent a420c3ab23
commit ca1d2a1ed8
7 changed files with 147 additions and 5 deletions

View File

@@ -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);
}
}
}