Loader.php

00001 <?php
00009 class Dsao_Config_Loader
00010 {
00014   protected $_config = null;
00015 
00019   protected $_dirCache = null;
00020 
00024   protected $_filenameCache = null;
00025 
00029   protected $_filenameConfig = null;
00030 
00034   protected $_section = null;
00035 
00044   public function __construct($filenameConfig, $dirCache, $section = null)
00045   {
00046     // Parameter übernehmen
00047     $this->_dirCache        = $dirCache;
00048 
00049     // Falls Caching-Verzeichnis nicht les- oder schreibbar
00050     if (!is_readable($this->_dirCache) || !is_writable($this->_dirCache))
00051     {
00052       throw new Dsao_Exception('dsao_config_cache_directory');
00053     }
00054 
00055     $this->_filenameCache   = $dirCache.'config';
00056     $this->_filenameConfig  = $filenameConfig;
00057 
00058     // Falls Konfigurationsdatei nicht lesbar
00059     if (!is_readable($this->_filenameConfig))
00060     {
00061       throw new Dsao_Exception('dsao_config_file_not_readable');
00062     }
00063 
00064     $this->_section         = $section;
00065   }
00066 
00073   public function __get($property)
00074   {
00075     return $this->getConfig()->$property;
00076   }
00077 
00083   protected function _cacheConfig()
00084   {
00085     // Falls Konfiguration noch nicht initialisiert wurde, abbrechen
00086     if (null == $this->_config)
00087     {
00088       return false;
00089     }
00090 
00091     // Serialisiertes Objekt in Datei schreiben
00092     return (bool) @file_put_contents
00093       ($this->_filenameCache, serialize($this->_config));
00094   }
00095 
00101   protected function _loadCachedConfig()
00102   {
00103     // Falls Datei vorhanden, Inhalt laden und deserialisieren
00104     if (is_file($this->_filenameCache))
00105     {
00106       $config =  unserialize($content = file_get_contents($this->_filenameCache));
00107 
00108       // Falls md5 übereinstimmen
00109       if (md5($content) == $config->config->md5)
00110       {
00111         return $config;
00112       }
00113       // ansonsten Konfigurationsdatei löschen (damit es nicht zu Problemen
00114       // kommt, wenn das Caching deaktiviert wurde)
00115       else
00116       {
00117         unlink($this->_filenameCache);
00118       }
00119     }
00120 
00121     return null;
00122   }
00123 
00129   public function getConfig()
00130   {
00131     // Falls Konfiguration noch nicht initalisiert wurde
00132     if (null === $this->_config)
00133     {
00134       // Falls Konfiguration nicht zwischengespeichert worden ist
00135       if (!$this->_config = $this->_loadCachedConfig())
00136       {
00137         // Standardkonfiguration laden
00138         $this->_config = new Dsao_Config_Ini
00139           ($this->_filenameConfig.'.dist', $this->_section, true);
00140         // Mit lokaler Konfiguration überschreiben
00141         $this->_config->merge(new Dsao_Config_Ini
00142           ($this->_filenameConfig, $this->_section));
00143 
00144         // Temporaeres Verzeichnis festlegen
00145         $this->_config->tempDir = TEMP_DIR;
00146 
00147         // MD5 der Konfigurationsdatei
00148         $this->_config->config->md5 = md5
00149           (file_get_contents($this->_filenameConfig));
00150 
00151         // Konfiguration vor Änderungen schützen
00152         $this->_config->setReadOnly();
00153 
00154         // Falls Konfiguration zwischengespeichert werden soll
00155         if ($this->_config->config->caching)
00156         {
00157           $this->_cacheConfig();
00158         }
00159       }
00160     }
00161 
00162     return $this->_config;
00163   }
00164 }

Erzeugt am Fri Sep 18 19:04:11 2009 für DSA online - Morgendaemmerung von  doxygen 1.5.7.1