split runtime info into runtime preferences

This commit is contained in:
sunxyw
2023-03-09 22:36:20 +08:00
parent 772288b517
commit 510bb8dc30
17 changed files with 126 additions and 134 deletions

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace ZM\Config;
class RuntimePreferences
{
protected string $environment = 'development';
protected bool $debug_mode = false;
protected string $log_level = 'info';
protected string $config_dir = SOURCE_ROOT_DIR . '/config';
public function environment(...$environments): string|bool
{
if (empty($environments)) {
return $this->environment;
}
return in_array($this->environment, $environments, true);
}
public function withEnvironment(string $environment): self
{
$copy = clone $this;
$copy->environment = $environment;
return $copy;
}
public function isDebugMode(): bool
{
return $this->debug_mode;
}
public function enableDebugMode(bool $debug_mode): self
{
$copy = clone $this;
$copy->debug_mode = $debug_mode;
return $copy;
}
public function getLogLevel(): string
{
return $this->isDebugMode() ? 'debug' : $this->log_level;
}
public function withLogLevel(string $log_level): self
{
$copy = clone $this;
$copy->log_level = $log_level;
return $copy;
}
public function getConfigDir(): string
{
return $this->config_dir;
}
public function withConfigDir(string $config_dir): self
{
$copy = clone $this;
$copy->config_dir = $config_dir;
return $copy;
}
public function runningInInteractiveTerminal(): bool
{
if (function_exists('posix_isatty')) {
return posix_isatty(STDIN) && posix_isatty(STDOUT);
}
// fallback to stream_isatty() if posix_isatty() is not available (e.g. on Windows)
return function_exists('stream_isatty') && stream_isatty(STDIN) && stream_isatty(STDOUT);
}
public function runningUnitTests(): bool
{
return defined('PHPUNIT_RUNNING') && constant('PHPUNIT_RUNNING');
}
}

View File

@@ -302,7 +302,7 @@ class ZMConfig
}
if ($type === 'environment') {
$name_and_env = explode('.', $name);
if (Framework::getInstance()->environment($name_and_env[1])) {
if (Framework::getInstance()->runtime_preferences->environment($name_and_env[1])) {
return true;
}
}