zhamao-framework/src/ZM/Bootstrap/LoadConfiguration.php

44 lines
1.0 KiB
PHP
Raw Normal View History

2022-11-08 17:28:07 +08:00
<?php
2022-11-08 17:33:25 +08:00
declare(strict_types=1);
2022-11-08 17:28:07 +08:00
namespace ZM\Bootstrap;
use Dotenv\Dotenv;
use ZM\Config\Environment;
use ZM\Config\EnvironmentInterface;
2022-11-08 17:28:07 +08:00
use ZM\Config\ZMConfig;
2023-02-24 16:49:24 +08:00
use ZM\Kernel;
2022-11-08 17:28:07 +08:00
2023-02-24 16:49:24 +08:00
class LoadConfiguration implements Bootstrapper
2022-11-08 17:28:07 +08:00
{
2023-02-24 16:49:24 +08:00
public function bootstrap(Kernel $kernel): void
2022-11-08 17:28:07 +08:00
{
// TODO: 重新思考容器绑定的加载方式,从而在此处使用 interface
$env = resolve(Environment::class);
$this->loadEnvVariables($env);
2023-02-24 16:49:24 +08:00
new ZMConfig([
'source' => [
'paths' => [$kernel->getConfigDir()],
],
]);
2022-11-08 17:28:07 +08:00
}
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);
}
}
2022-11-08 17:28:07 +08:00
}