modifs.mineures sans commentaires
This commit is contained in:
parent
0d0c29aab8
commit
bed23c9425
@ -1,31 +1,49 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace nulib\app\config;
|
namespace nulib\app\config;
|
||||||
|
|
||||||
|
use nulib\app\app;
|
||||||
use nulib\app\config;
|
use nulib\app\config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ProfileManager: gestionnaire de profils
|
* Class ProfileManager: gestionnaire de profils
|
||||||
*/
|
*/
|
||||||
class ProfileManager {
|
class ProfileManager {
|
||||||
/** @var string code du système dont on gère le profil */
|
/**
|
||||||
|
* @var string code du système dont on gère le profil
|
||||||
|
*
|
||||||
|
* ce code est utilisé pour dériver le nom du paramètre dans la configuration
|
||||||
|
* ainsi que la variable d'environnement depuis laquelle est chargée la valeur
|
||||||
|
* du profil
|
||||||
|
*/
|
||||||
const NAME = null;
|
const NAME = null;
|
||||||
|
|
||||||
/** @var array|null liste des profils valides */
|
/** @var array|null liste des profils valides */
|
||||||
const PROFILES = null;
|
const PROFILES = null;
|
||||||
|
|
||||||
/** @var array profils dont le mode production doit être actif */
|
/** @var array profils dont le mode production doit être actif */
|
||||||
const PRODUCTION_MODES = [
|
const PRODUCTION_MODES = [
|
||||||
"prod" => true,
|
"prod" => true,
|
||||||
"test" => 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;
|
const PROFILE_MAP = null;
|
||||||
|
|
||||||
function __construct(?array $params=null) {
|
function __construct(?array $params=null) {
|
||||||
|
$this->isAppProfile = $params["app"] ?? false;
|
||||||
$this->profiles = static::PROFILES;
|
$this->profiles = static::PROFILES;
|
||||||
$this->production_modes = static::PRODUCTION_MODES;
|
$this->productionModes = static::PRODUCTION_MODES;
|
||||||
$appProfile = $params["app"] ?? false;
|
$this->profileMap = static::PROFILE_MAP;
|
||||||
$name = $params["name"] ?? static::NAME;
|
$name = $params["name"] ?? static::NAME;
|
||||||
$configKey = "${name}_profile";
|
$configKey = "${name}_profile";
|
||||||
$envKey = strtoupper($configKey);
|
$envKey = strtoupper($configKey);
|
||||||
if ($appProfile) {
|
if ($this->isAppProfile) {
|
||||||
$this->envKeys = [$envKey, "APP_PROFILE"];
|
$this->envKeys = [$envKey, "APP_PROFILE"];
|
||||||
} else {
|
} else {
|
||||||
$this->configKey = $configKey;
|
$this->configKey = $configKey;
|
||||||
@ -33,21 +51,26 @@ class ProfileManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var bool cet objet est-il utilisé pour gérer le profil de l'application?
|
||||||
|
*/
|
||||||
|
protected bool $isAppProfile;
|
||||||
|
|
||||||
protected ?array $profiles;
|
protected ?array $profiles;
|
||||||
|
|
||||||
protected ?array $productionModes;
|
protected ?array $productionModes;
|
||||||
|
|
||||||
function mapProfile(?string $profile): ?string {
|
protected ?array $profileMap;
|
||||||
return static::PROFILE_MAP[$profile] ?? $profile;
|
|
||||||
|
protected function mapProfile(?string $profile): ?string {
|
||||||
|
return $this->profileMap[$profile] ?? $profile;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ?string $configKey = null;
|
protected ?string $configKey = null;
|
||||||
|
|
||||||
function getConfigProfile(): ?string {
|
function getConfigProfile(): ?string {
|
||||||
if ($this->configKey === null) return null;
|
if ($this->configKey === null) return null;
|
||||||
$profile = config::k($this->configKey);
|
return config::k($this->configKey);
|
||||||
$profile = $this->mapProfile($profile);
|
|
||||||
return $profile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected array $envKeys = [];
|
protected array $envKeys = [];
|
||||||
@ -78,7 +101,11 @@ class ProfileManager {
|
|||||||
$profile ??= $this->getenvProfile();
|
$profile ??= $this->getenvProfile();
|
||||||
$profile ??= $this->getConfigProfile();
|
$profile ??= $this->getConfigProfile();
|
||||||
$profile ??= $this->getDefaultProfile();
|
$profile ??= $this->getDefaultProfile();
|
||||||
|
if ($this->isAppProfile) {
|
||||||
$profile ??= $this->profiles[0] ?? "prod";
|
$profile ??= $this->profiles[0] ?? "prod";
|
||||||
|
} else {
|
||||||
|
$profile ??= $this->mapProfile(app::get_profile());
|
||||||
|
}
|
||||||
$this->profile = $profile;
|
$this->profile = $profile;
|
||||||
$this->productionMode = $this->productionModes[$profile] ?? false;
|
$this->productionMode = $this->productionModes[$profile] ?? false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user