true, "test" => true, ]; /** * @var array mapping profil d'application --> profil effectif * * ce mapping est utilisé quand il faut calculer le profil courant s'il n'a * pas été spécifié par l'utilisateur. il permet de faire correspondre le * profil courant de l'application avec le profil effectif à sélectionner */ const PROFILE_MAP = null; function __construct(?array $params=null) { $this->isAppProfile = $params["app"] ?? false; $this->profiles = static::PROFILES; $this->productionModes = static::PRODUCTION_MODES; $this->profileMap = static::PROFILE_MAP; $name = $params["name"] ?? static::NAME; if ($name === null) { $this->configKey = null; $this->envKeys = ["APP_PROFILE"]; } else { $configKey = "${name}_profile"; $envKey = strtoupper($configKey); if ($this->isAppProfile) { $this->configKey = null; $this->envKeys = [$envKey, "APP_PROFILE"]; } else { $this->configKey = $configKey; $this->envKeys = [$envKey]; } } $this->defaultProfile = $params["default_profile"] ?? null; $profile = $params["profile"] ?? null; $productionMode = $params["production_mode"] ?? null; $productionMode ??= $this->productionModes[$profile] ?? false; $this->profile = $profile; $this->productionMode = $productionMode; } /** * @var bool cet objet est-il utilisé pour gérer le profil de l'application? */ protected bool $isAppProfile; protected ?array $profiles; protected ?array $productionModes; protected ?array $profileMap; protected function mapProfile(?string $profile): ?string { return $this->profileMap[$profile] ?? $profile; } protected ?string $configKey; function getConfigProfile(): ?string { if ($this->configKey === null) return null; return config::k($this->configKey); } protected array $envKeys; function getEnvProfile(): ?string { foreach ($this->envKeys as $envKey) { $profile = getenv($envKey); if ($profile !== false) return $profile; } return null; } protected ?string $defaultProfile; function getDefaultProfile(): ?string { return $this->defaultProfile; } function setDefaultProfile(?string $profile): void { $this->defaultProfile = $profile; } protected ?string $profile; protected bool $productionMode; protected function resolveProfile(): void { $profile ??= $this->getenvProfile(); $profile ??= $this->getConfigProfile(); $profile ??= $this->getDefaultProfile(); if ($this->isAppProfile) { $profile ??= $this->profiles[0] ?? "prod"; } else { $profile ??= $this->mapProfile(app::get_profile()); } $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 isProductionMode(): bool { return $this->productionMode; } 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; } }