modifs.mineures sans commentaires
This commit is contained in:
parent
e16581f849
commit
0d0c29aab8
@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace nulib\app;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ProfileManager: gestionnaire de profils
|
|
||||||
*/
|
|
||||||
class ProfileManager {
|
|
||||||
const NAME = null;
|
|
||||||
const PROFILES = ["prod", "test", "devel"];
|
|
||||||
const PRODUCTION_MODES = [
|
|
||||||
"prod" => 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 {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace nulib\app;
|
namespace nulib\app;
|
||||||
|
|
||||||
|
use nulib\app\config\ConfigManager;
|
||||||
|
use nulib\app\config\ProfileManager;
|
||||||
|
use nulib\cl;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class config: gestion de la configuration de l'application
|
* Class config: gestion de la configuration de l'application
|
||||||
*/
|
*/
|
||||||
class config {
|
class config {
|
||||||
|
protected static ConfigManager $config;
|
||||||
|
protected static ProfileManager $profile;
|
||||||
|
|
||||||
|
static function init(string $appcode): void {
|
||||||
|
self::$config = new ConfigManager();
|
||||||
|
self::$profile = new ProfileManager([
|
||||||
|
"app" => 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
5
src/app/config/ConfigManager.php
Normal file
5
src/app/config/ConfigManager.php
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?php
|
||||||
|
namespace nulib\app\config;
|
||||||
|
|
||||||
|
class ConfigManager {
|
||||||
|
}
|
99
src/app/config/ProfileManager.php
Normal file
99
src/app/config/ProfileManager.php
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
<?php
|
||||||
|
namespace nulib\app\config;
|
||||||
|
|
||||||
|
use nulib\app\config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ProfileManager: gestionnaire de profils
|
||||||
|
*/
|
||||||
|
class ProfileManager {
|
||||||
|
/** @var string code du système dont on gère le profil */
|
||||||
|
const NAME = null;
|
||||||
|
/** @var array|null liste des profils valides */
|
||||||
|
const PROFILES = null;
|
||||||
|
/** @var array profils dont le mode production doit être actif */
|
||||||
|
const PRODUCTION_MODES = [
|
||||||
|
"prod" => 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user