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

@@ -7,6 +7,7 @@ use OneBot\Driver\Coroutine\CoroutineInterface;
use OneBot\Driver\Process\ExecutionResult;
use OneBot\V12\Object\MessageSegment;
use Psr\Log\LoggerInterface;
use ZM\Config\Environment;
use ZM\Config\ZMConfig;
use ZM\Container\ContainerHolder;
use ZM\Logger\ConsoleLogger;
@@ -268,3 +269,12 @@ function kv(string $name = ''): Psr\SimpleCache\CacheInterface
/* @phpstan-ignore-next-line */
return is_a($kv_class, KVInterface::class, true) ? $kv_class::open($name) : new $kv_class($name);
}
/**
* 获取环境变量
*/
function env(string $key, mixed $default = null): mixed
{
// TODO: 重新思考容器绑定的加载方式,从而在此处使用 interface
return resolve(Environment::class)->get($key, $default);
}

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

View File

@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);
namespace ZM\Config;
class Environment implements EnvironmentInterface
{
private const VALUE_MAP = [
'true' => true,
'(true)' => true,
'false' => false,
'(false)' => false,
'null' => null,
'(null)' => null,
'empty' => '',
];
private array $values;
/**
* @param array $values 额外的环境变量,优先级高于系统环境变量
* @param bool $overwrite 是否允许后续 set() 覆盖已有的环境变量
*/
public function __construct(
array $values = [],
private bool $overwrite = false
) {
$this->values = $values + $_ENV + $_SERVER;
}
/**
* {@inheritdoc}
*/
public function set(string $name, mixed $value): self
{
if (array_key_exists($name, $this->values) && !$this->overwrite) {
// 如不允许覆盖已有的环境变量,则不做任何操作
return $this;
}
$this->values[$name] = $_ENV[$name] = $value;
putenv("{$name}={$value}");
return $this;
}
/**
* {@inheritdoc}
*/
public function get(string $name, mixed $default = null): mixed
{
if (isset($this->values[$name])) {
return $this->normalize($this->values[$name]);
}
return $default;
}
/**
* {@inheritdoc}
*/
public function getAll(): array
{
$result = [];
foreach ($this->values as $key => $value) {
$result[$key] = $this->normalize($value);
}
return $result;
}
protected function normalize(mixed $value): mixed
{
if (!is_string($value)) {
return $value;
}
return self::VALUE_MAP[strtolower($value)] ?? $value;
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace ZM\Config;
interface EnvironmentInterface
{
/**
* 设置环境变量
*/
public function set(string $name, mixed $value): self;
/**
* 获取环境变量
*/
public function get(string $name, mixed $default = null): mixed;
/**
* 获取所有环境变量
*/
public function getAll(): array;
}