diff --git a/php/src/app/app.php b/php/src/app/app.php index 9781202..b46595a 100644 --- a/php/src/app/app.php +++ b/php/src/app/app.php @@ -251,7 +251,6 @@ class app { $name = preg_replace('/.*\\\\/', "", $name); $name = str::camel2us($name, false, "-"); $name = str::without_suffix("-app", $name); - $name .= ".php"; } $this->appgroup = $appgroup; $this->name = $name; diff --git a/php/src/app/cli/Application.php b/php/src/app/cli/Application.php index 679eb09..330efdf 100644 --- a/php/src/app/cli/Application.php +++ b/php/src/app/cli/Application.php @@ -246,9 +246,14 @@ EOT); const PROFILE_SECTION = [ "title" => "PROFIL D'EXECUTION", "show" => false, + ["-c", "--config", "--app-config", + "args" => "file", "argsdesc" => "CONFIG.yml", + "action" => [config::class, "load_config"], + "help" => "spécifier un fichier de configuration", + ], ["group", - ["-p", "--profile", "--app-profile", - "args" => "profile", + ["-g", "--profile", "--app-profile", + "args" => 1, "argsdesc" => "PROFILE", "action" => [app::class, "set_profile"], "help" => "spécifier le profil d'exécution", ], diff --git a/php/src/app/config.php b/php/src/app/config.php index 08699f9..27469c4 100644 --- a/php/src/app/config.php +++ b/php/src/app/config.php @@ -2,7 +2,12 @@ namespace nulib\app; use nulib\app\config\ConfigManager; +use nulib\app\config\JsonConfig; +use nulib\app\config\YamlConfig; use nulib\cl; +use nulib\os\path; +use nulib\str; +use nulib\ValueException; /** * Class config: gestion de la configuration de l'application @@ -27,6 +32,18 @@ class config { } static final function add($config, string ...$profiles): void { self::$config->addConfig($config, $profiles); } + static final function load_config($file): void { + $ext = path::ext($file); + if ($ext === ".yml" || $ext === ".yaml") { + $config = new YamlConfig($file); + } elseif ($ext === ".json") { + $config = new JsonConfig($file); + } else { + throw ValueException::invalid_value($file, "config file"); + } + self::add($config); + } + static final function get(string $pkey, $default=null, ?string $profile=null) { return self::$config->getValue($pkey, $default, $profile); } static final function k(string $pkey, $default=null) { return self::$config->getValue("app.$pkey", $default); } static final function db(string $pkey, $default=null) { return self::$config->getValue("dbs.$pkey", $default); } diff --git a/php/src/app/config/ConfigManager.php b/php/src/app/config/ConfigManager.php index ef10881..c3ca147 100644 --- a/php/src/app/config/ConfigManager.php +++ b/php/src/app/config/ConfigManager.php @@ -76,7 +76,7 @@ class ConfigManager { * $config est un objet ou une classe qui définit une ou plusieurs des * constantes APP, DBS, MSGS, MAILS * - * si $inProfiles===null, la configuration est valide dans tous les profils + * si !$inProfiles, la configuration est valide dans tous les profils */ function addConfig($config, ?array $inProfiles=null): void { if (is_string($config)) { @@ -96,7 +96,7 @@ class ConfigManager { throw ValueException::invalid_type($config, "array|IConfig"); } - $inProfiles ??= [IConfig::PROFILE_ALL]; + if (!$inProfiles) $inProfiles = [IConfig::PROFILE_ALL]; foreach ($inProfiles as $profile) { $this->profileConfigs[$profile][] = $config; } @@ -107,18 +107,16 @@ class ConfigManager { function _getValue(string $pkey, $default, string $inProfile) { $profiles = [$inProfile]; if ($inProfile !== IConfig::PROFILE_ALL) $profiles[] = IConfig::PROFILE_ALL; - $value = $default; foreach ($profiles as $profile) { /** @var IConfig[] $configs */ $configs = $this->profileConfigs[$profile] ?? []; foreach (array_reverse($configs) as $config) { if ($config->has($pkey, $profile)) { - $value = $config->get($pkey, $profile); - break; + return $config->get($pkey, $profile); } } } - return $value; + return $default; } /**