48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			48 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| namespace nur\config;
 | |
| 
 | |
| use nur\A;
 | |
| use nur\b\ValueException;
 | |
| use nur\yaml;
 | |
| 
 | |
| /**
 | |
|  * Class YamlConfig: une configuration stockée dans un fichier yaml
 | |
|  *
 | |
|  * Lors de la création de l'objet, il faut préciser le cas échéant dans quels
 | |
|  * profils sont valides cette configuration.
 | |
|  */
 | |
| class YamlConfig extends DynConfig {
 | |
|   /**
 | |
|    * @var array liste des profils dans lesquels cette configuration est valide.
 | |
|    *
 | |
|    * un tableau vide signifie que la configuration est valide dans tous les
 | |
|    * profils
 | |
|    */
 | |
|   protected $profiles;
 | |
| 
 | |
|   /** @var array */
 | |
|   protected $config;
 | |
| 
 | |
|   function __construct(string $input=null, string ...$profiles) {
 | |
|     $this->profiles = $profiles;
 | |
|     $this->config = yaml::load($input);
 | |
|   }
 | |
| 
 | |
|   function has(string $pkey, string $profile): bool {
 | |
|     if ($this->profiles && !in_array($profile, $this->profiles)) return false;
 | |
|     return A::phas_s($this->config, $pkey);
 | |
|   }
 | |
| 
 | |
|   function get(string $pkey, string $profile) {
 | |
|     if ($this->profiles && !in_array($profile, $this->profiles)) return null;
 | |
|     return A::pget_s($this->config, $pkey);
 | |
|   }
 | |
| 
 | |
|   function set(string $pkey, $value, string $profile): void {
 | |
|     if ($this->profiles && !in_array($profile, $this->profiles)) {
 | |
|       throw new ValueException("$profile: unauthorized profile");
 | |
|     }
 | |
|     A::pset_s($this->config, $pkey, $value);
 | |
|   }
 | |
| }
 |