diff --git a/src/app/ProfileManager.php b/src/app/ProfileManager.php deleted file mode 100644 index eae9822..0000000 --- a/src/app/ProfileManager.php +++ /dev/null @@ -1,50 +0,0 @@ - true, - "test" => true, - ]; - const PROFILE_MAP = null; - - function __construct(?array $params=null) { - $name = $params["name"] ?? static::NAME; - if ($name !== null) $name .= "_"; - $prefixes = [strtoupper($name)]; - $app = $params["app"] ?? false; - if ($app) $prefixes[] = "APP_"; - $this->envPrefixes = $prefixes; - } - - protected array $envPrefixes; - - function getEnvProfile(): ?string { - foreach ($this->envPrefixes as $prefix) { - $profile = getenv($prefix."PROFILE"); - if ($profile !== false) return $profile; - } - return null; - } - - protected ?string $defaultProfile = null; - - function getDefaultProfile(): ?string { - return $this->defaultProfile; - } - - function setDefaultProfile(?string $profile): void { - $this->defaultProfile = $profile; - } - - function getProfile(?bool &$productionMode=null): string { - } - - function setProfile(?string $profile=null, ?bool $productionMode=null): void { - } -} diff --git a/src/app/config.php b/src/app/config.php index 9713d28..5c2ac91 100644 --- a/src/app/config.php +++ b/src/app/config.php @@ -1,8 +1,30 @@ true, + "name" => $appcode, + ]); + } + + static function init_configurator($configurators): void { + self::$config->addConfigurators(cl::with($configurators)); + } + + static function configure(?array $params=null): void { + self::$config->configure($params); + } } diff --git a/src/app/config/ConfigManager.php b/src/app/config/ConfigManager.php new file mode 100644 index 0000000..cee9fcb --- /dev/null +++ b/src/app/config/ConfigManager.php @@ -0,0 +1,5 @@ + true, + "test" => true, + ]; + const PROFILE_MAP = null; + + function __construct(?array $params=null) { + $this->profiles = static::PROFILES; + $this->production_modes = static::PRODUCTION_MODES; + $appProfile = $params["app"] ?? false; + $name = $params["name"] ?? static::NAME; + $configKey = "${name}_profile"; + $envKey = strtoupper($configKey); + if ($appProfile) { + $this->envKeys = [$envKey, "APP_PROFILE"]; + } else { + $this->configKey = $configKey; + $this->envKeys = [$envKey]; + } + } + + protected ?array $profiles; + + protected ?array $productionModes; + + function mapProfile(?string $profile): ?string { + return static::PROFILE_MAP[$profile] ?? $profile; + } + + protected ?string $configKey = null; + + function getConfigProfile(): ?string { + if ($this->configKey === null) return null; + $profile = config::k($this->configKey); + $profile = $this->mapProfile($profile); + return $profile; + } + + protected array $envKeys = []; + + function getEnvProfile(): ?string { + foreach ($this->envKeys as $envKey) { + $profile = getenv($envKey); + if ($profile !== false) return $profile; + } + return null; + } + + protected ?string $defaultProfile = null; + + function getDefaultProfile(): ?string { + return $this->defaultProfile; + } + + function setDefaultProfile(?string $profile): void { + $this->defaultProfile = $profile; + } + + protected ?string $profile = null; + + protected bool $productionMode = false; + + protected function resolveProfile(): void { + $profile ??= $this->getenvProfile(); + $profile ??= $this->getConfigProfile(); + $profile ??= $this->getDefaultProfile(); + $profile ??= $this->profiles[0] ?? "prod"; + $this->profile = $profile; + $this->productionMode = $this->productionModes[$profile] ?? false; + } + + function getProfile(?bool &$productionMode=null): string { + if ($this->profile === null) $this->resolveProfile(); + $productionMode = $this->productionMode; + return $this->profile; + } + + function setProfile(?string $profile=null, ?bool $productionMode=null): void { + if ($profile === null) $this->profile = null; + $profile ??= $this->getProfile($productionMode); + $productionMode ??= $this->productionModes[$profile] ?? false; + $this->profile = $profile; + $this->productionMode = $productionMode; + } +}