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 +++++++++++++++++++++++++++++++ framework/Web/Services/TPageService.php | 8 +-- framework/Web/UI/TPage.php | 2 +- framework/Web/UI/TPageStatePersister.php | 2 +- framework/core.php | 7 +- 5 files changed, 127 insertions(+), 9 deletions(-) (limited to 'framework') 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 diff --git a/framework/Web/Services/TPageService.php b/framework/Web/Services/TPageService.php index 7ef3c4f0..19a744a4 100644 --- a/framework/Web/Services/TPageService.php +++ b/framework/Web/Services/TPageService.php @@ -139,7 +139,7 @@ class TPageService extends TService */ private $_templateManager=null; /** - * @var IPageStatePersister page state persister + * @var IStatePersister page state persister */ private $_pageStatePersister=null; @@ -349,7 +349,7 @@ class TPageService extends TService } /** - * @return IPageStatePersister page state persister + * @return IStatePersister page state persister */ public function getPageStatePersister() { @@ -362,9 +362,9 @@ class TPageService extends TService } /** - * @param IPageStatePersister page state persister + * @param IStatePersister page state persister */ - public function setPageStatePersister(IPageStatePersister $value) + public function setPageStatePersister(IStatePersister $value) { $this->_pageStatePersister=$value; } diff --git a/framework/Web/UI/TPage.php b/framework/Web/UI/TPage.php index 69b0cc31..7fe72434 100644 --- a/framework/Web/UI/TPage.php +++ b/framework/Web/UI/TPage.php @@ -495,7 +495,7 @@ class TPage extends TTemplateControl } /** - * @return IPageStatePersister page state persister + * @return IStatePersister page state persister */ protected function getPageStatePersister() { diff --git a/framework/Web/UI/TPageStatePersister.php b/framework/Web/UI/TPageStatePersister.php index 0718c492..07e1eba1 100644 --- a/framework/Web/UI/TPageStatePersister.php +++ b/framework/Web/UI/TPageStatePersister.php @@ -1,6 +1,6 @@ * @version $Revision: $ $Date: $ * @package System * @since 3.0 */ -interface IPageStatePersister +interface IStatePersister { /** * Loads state from a persistent storage. -- cgit v1.2.3