modifs.mineures sans commentaires
This commit is contained in:
		
							parent
							
								
									d7f82a2fd9
								
							
						
					
					
						commit
						8c72c5c3eb
					
				| @ -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; | ||||
|  | ||||
| @ -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", | ||||
|       ], | ||||
|  | ||||
| @ -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); } | ||||
|  | ||||
| @ -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; | ||||
|   } | ||||
| 
 | ||||
|   /** | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user