Автор Тема: Configuration loader class  (Прочетена 4585 пъти)

0 Потребители и 1 Гост преглежда(т) тази тема.

HD

  • Administrator
  • Hero Member
  • *****
  • Благодарности
  • -Казани: 208
  • -Получени: 165
  • Публикации: 3077
Configuration loader class
« -: 25 Юли 2012, 10:32:13 »
Днес нещо ме удари музата и за около час написах един сравнително лесен за използване клас. Целта му е да зарежда конфигурационни файлове и по този начин да се отървем от използването на include и require.
Код: PHP
  1. <?php
  2.  
  3. /*
  4.  * Simple loader for config files/Singleton/
  5.  *
  6.  * Developed by HD 2012
  7.  */
  8.  
  9. class ConfigLoader {
  10.    
  11.    /*
  12.     * current instance of the class
  13.     */
  14.     private static $_instance=NULL;
  15.    
  16.     public $_options=array();
  17.    
  18.     //the constructor
  19.    
  20.     private function __construct() {}
  21.    
  22.     //singleton's could not be cloned
  23.    
  24.     private function __clone() {}
  25.  
  26.     /*
  27.      * check the instance of the class.
  28.      * $filepath-път до файла
  29.      * $type-вид на файла
  30.      */
  31.    
  32.     public static function checkInstance($filepath, $type)
  33.     {
  34.         //check the instance of the class
  35.        
  36.         if(self::$_instance==NULL)
  37.         {
  38.             self::$_instance=new self($filepath, $type);
  39.         }
  40.         else
  41.         {
  42.             //return the current instance of the class
  43.            
  44.             return self::$_instance;
  45.         }
  46.     }
  47.     /*
  48.      * Loading the configurations
  49.      */
  50.    
  51.     private function load($filepath, $type)
  52.     {
  53.         switch ($type)
  54.         {
  55.             case "ARRAY":
  56.                 $this->options= include $filepath;
  57.                 break;
  58.            
  59.             case "INI":
  60.                 $this->_options= parse_ini($filepath, true);
  61.                
  62.                 break;
  63.            
  64.             case "JSON":
  65.                 $this->_options= json_decode(file_get_contents($$filepath));
  66.                
  67.                 break;
  68.                
  69.             case "XML":
  70.                 //checking if the file exist
  71.                 if(file_exists($filepath))
  72.                 {
  73.                     simplexml_load_file($filepath);
  74.                 }
  75.                 else
  76.                 {
  77.                     trigger_error("Failed to load the file", E_USER_NOTICE);
  78.                 }
  79.            
  80.         }
  81.     }
  82.  
  83. }
  84.  
  85. ?>
  86.  

Пример на това как се използва

Код: PHP
  1. $config = Config::getInstance(PATH TO FILE, FILE TYPE);
  2. echo $config->ip;
  3. echo $config->db['host'];
  4.  
  5.  
  6. example array file
  7. <?php
  8. return array(
  9.     'db' => array(
  10.         'host' => 'mysite.com',
  11.         'user' => 'root',
  12.         'pass' => 'blalalalallala'),
  13.  
  14.     'ip' => '127.0.0.1',
  15.  
Надявам се да е полезно.. Забележки ако имате, коментари или просто искате да ми теглите една, моля под темата :)