mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-18 21:24:52 +08:00
44 lines
1.0 KiB
PHP
44 lines
1.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace ZM\Bootstrap;
|
|
|
|
use Dotenv\Dotenv;
|
|
use ZM\Config\Environment;
|
|
use ZM\Config\EnvironmentInterface;
|
|
use ZM\Config\ZMConfig;
|
|
use ZM\Kernel;
|
|
|
|
class LoadConfiguration implements Bootstrapper
|
|
{
|
|
public function bootstrap(Kernel $kernel): void
|
|
{
|
|
// TODO: 重新思考容器绑定的加载方式,从而在此处使用 interface
|
|
$env = resolve(Environment::class);
|
|
$this->loadEnvVariables($env);
|
|
|
|
new ZMConfig([
|
|
'source' => [
|
|
'paths' => [$kernel->getConfigDir()],
|
|
],
|
|
]);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|