Advertisement
Guest User

Config class

a guest
Feb 9th, 2015
1,775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. <?php
  2.  
  3. namespace CodeRichard\Config;
  4.  
  5. use CodeRichard\Error\EnvironmentNotFoundException;
  6.  
  7. class Config
  8. {  
  9.     private static $loaded = false;
  10.    
  11.     private static $configEntries = [];
  12.    
  13.     public static function load($environment)
  14.     {
  15.         $base = __DIR__.'/../../config/';
  16.         $configFile = $base.$environment.'.php';
  17.        
  18.         if(!file_exists($configFile))
  19.         {
  20.             throw new EnvironmentNotFoundException($environment);
  21.         }
  22.        
  23.         if(!self::$loaded)
  24.         {
  25.             self::$configEntries = require $configFile;
  26.         }
  27.     }
  28.    
  29.     public static function get($path, $default = '')
  30.     {
  31.         $arr = self::$configEntries;
  32.         $parts = explode('.', $path);
  33.        
  34.         foreach($parts as $part)
  35.         {
  36.             if(isset($arr[$part]))
  37.             {
  38.                 $arr = $arr[$part];
  39.             }
  40.            
  41.             else
  42.             {
  43.                 return $default;
  44.             }
  45.         }
  46.        
  47.         return $arr;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement