Guest User

Config.php

a guest
Sep 22nd, 2011
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.39 KB | None | 0 0
  1. <?php
  2.  
  3. class Config {
  4.  
  5.     private static $store = array();
  6.  
  7.     public static function get($index, $default = null) {
  8.         $index = explode('.', $index);
  9.  
  10.         if($index[0] == 'app'){
  11.             if(!isset(self::$store['app'][$index[1]])){
  12.                 if(file_exists(DIR_APP_CONFIG . $index[1] . EXT)){
  13.                     self::$store['app'][$index[1]] = include_once DIR_APP_CONFIG . $index[1] . EXT;
  14.                 } else {
  15.                     throw new Edge_Exception('No such config file :file in :in', array(":file" => $index[1] . EXT, ":in" => DIR_APP_CONFIG));
  16.                 }
  17.             }
  18.             $cfg = self::$store['app'][$index[1]];
  19.         }
  20.  
  21.         try {
  22.             $return = self::getValue($index, self::$store);
  23.         } catch (Edge_Exception $e) {
  24.             if (!is_null($default)) {
  25.                 $return = $default;
  26.             } else {
  27.                 throw $e;
  28.             }
  29.         }
  30.         return $return;
  31.     }
  32.  
  33.     protected static function getValue($index, $value) {
  34.         if (is_array($index) and count($index)) {
  35.             $current_index = array_shift($index);
  36.         }
  37.         if (is_array($index) and count($index) and isset($value[$current_index]) and is_array($value[$current_index]) and count($value[$current_index])) {
  38.             return self::getValue($index, $value[$current_index]);
  39.         } elseif (isset($value[$current_index])) {
  40.             return $value[$current_index];
  41.         } else {
  42.             throw new Edge_Exception("Attempt to access missing configuration variable: :current_index",
  43.                 array(":current_index" => $current_index)
  44.             );
  45.         }
  46.     }
  47.  
  48. }
  49.  
  50. //End Config
Advertisement
Add Comment
Please, Sign In to add comment