From c3bdc43f1b03cbea1849beb58c28d653cc435fe0 Mon Sep 17 00:00:00 2001 From: xue <> Date: Wed, 28 Dec 2005 13:42:44 +0000 Subject: Refactored application state persister. --- framework/TApplication.php | 117 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) (limited to 'framework/TApplication.php') diff --git a/framework/TApplication.php b/framework/TApplication.php index 104ff795..e2743c6d 100644 --- a/framework/TApplication.php +++ b/framework/TApplication.php @@ -230,6 +230,10 @@ class TApplication extends TComponent * @var ICache cache module, could be null */ private $_cache=null; + /** + * @var IStatePersister application state persister + */ + private $_statePersister=null; /** * @var IUser user instance, could be null */ @@ -371,6 +375,8 @@ class TApplication extends TComponent */ protected function loadGlobals() { + $this->_globals=$this->getApplicationStatePersister()->load(); + /* if(($cache=$this->getCache())!==null && ($value=$cache->get('prado:globals'))!==false) $this->_globals=unserialize($value); else @@ -378,6 +384,7 @@ class TApplication extends TComponent if(($content=@file_get_contents($this->getRuntimePath().'/'.self::GLOBAL_FILE))!==false) $this->_globals=unserialize($content); } + */ } /** @@ -388,6 +395,8 @@ class TApplication extends TComponent { if(!$this->_stateChanged) return; + $this->getApplicationStatePersister()->save($this->_globals); + /* $content=serialize($this->_globals); $saveFile=true; if(($cache=$this->getCache())!==null) @@ -405,6 +414,7 @@ class TApplication extends TComponent else file_put_contents($fileName,$content); } + */ } /** @@ -625,6 +635,27 @@ class TApplication extends TComponent $this->_errorHandler=$handler; } + /** + * @return IStatePersister application state persister + */ + public function getApplicationStatePersister() + { + if(!$this->_statePersister) + { + $this->_statePersister=new TApplicationStatePersister; + $this->_statePersister->init($this,null); + } + return $this->_statePersister; + } + + /** + * @param IStatePersister application state persister + */ + public function setApplicationStatePersister(IStatePersister $persister) + { + $this->_statePersister=$persister; + } + /** * @return ICache the cache module, null if cache module is not installed */ @@ -1106,4 +1137,90 @@ class TApplicationConfiguration extends TComponent } } +/** + * TApplicationStatePersister class. + * TApplicationStatePersister provides a file-based persistent storage + * for application state. Application state, when serialized, is stored + * in a file named 'global.cache' under the 'runtime' directory of the application. + * Cache will be exploited if it is enabled. + * + * @author Qiang Xue + * @version $Revision: $ $Date: $ + * @package System + * @since 3.0 + */ +class TApplicationStatePersister extends TModule implements IStatePersister +{ + /** + * Name of the value stored in cache + */ + const CACHE_NAME='prado:appstate'; + /** + * @var TApplication application instance + */ + private $_application; + + /** + * Initializes module. + * @param TApplication application instance + * @param TXmlElement module configuration (may be null) + */ + public function init($application,$config) + { + parent::init($application,$config); + $this->_application=$application; + $application->setApplicationStatePersister($this); + } + + /** + * @return string the file path storing the application state + */ + protected function getStateFilePath() + { + return $this->_application->getRuntimePath().'/global.cache'; + } + + /** + * Loads application state from persistent storage. + * @return mixed application state + */ + public function load() + { + if(($cache=$this->_application->getCache())!==null && ($value=$cache->get(self::CACHE_NAME))!==false) + return unserialize($value); + else + { + if(($content=@file_get_contents($this->getStateFilePath()))!==false) + return unserialize($content); + else + return null; + } + } + + /** + * Saves application state in persistent storage. + * @param mixed application state + */ + public function save($state) + { + $content=serialize($state); + $saveFile=true; + if(($cache=$this->_application->getCache())!==null) + { + if($cache->get(self::CACHE_NAME)===$content) + $saveFile=false; + else + $cache->set(self::CACHE_NAME,$content); + } + if($saveFile) + { + $fileName=$this->getStateFilePath(); + if(version_compare(phpversion(),'5.1.0','>=')) + file_put_contents($fileName,$content,LOCK_EX); + else + file_put_contents($fileName,$content); + } + } + +} ?> \ No newline at end of file -- cgit v1.2.3