summaryrefslogtreecommitdiff
path: root/framework
diff options
context:
space:
mode:
authorxue <>2005-12-28 13:42:44 +0000
committerxue <>2005-12-28 13:42:44 +0000
commitc3bdc43f1b03cbea1849beb58c28d653cc435fe0 (patch)
tree49dfec8c0e308448efc8488cd281a6793f8dad6c /framework
parent6ea993425cc0982ecef765d4bfc6b75b7206416d (diff)
Refactored application state persister.
Diffstat (limited to 'framework')
-rw-r--r--framework/TApplication.php117
-rw-r--r--framework/Web/Services/TPageService.php8
-rw-r--r--framework/Web/UI/TPage.php2
-rw-r--r--framework/Web/UI/TPageStatePersister.php2
-rw-r--r--framework/core.php7
5 files changed, 127 insertions, 9 deletions
diff --git a/framework/TApplication.php b/framework/TApplication.php
index 104ff795..e2743c6d 100644
--- a/framework/TApplication.php
+++ b/framework/TApplication.php
@@ -231,6 +231,10 @@ class TApplication extends TComponent
*/
private $_cache=null;
/**
+ * @var IStatePersister application state persister
+ */
+ private $_statePersister=null;
+ /**
* @var IUser user instance, could be null
*/
private $_user=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);
}
+ */
}
/**
@@ -626,6 +636,27 @@ class TApplication extends TComponent
}
/**
+ * @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
*/
public function getCache()
@@ -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 <qiang.xue@gmail.com>
+ * @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 @@
<?php
-class TPageStatePersister extends TModule implements IPageStatePersister
+class TPageStatePersister extends TModule implements IStatePersister
{
private $_application;
private $_privateKey=null;
diff --git a/framework/core.php b/framework/core.php
index 772cd384..8288f82f 100644
--- a/framework/core.php
+++ b/framework/core.php
@@ -287,16 +287,17 @@ interface IUser
}
/**
- * IPageStatePersister class.
+ * IStatePersister class.
*
- * This interface must be implemented by all page state persister classes.
+ * This interface must be implemented by all state persister classes (such as
+ * {@link TPageStatePersister}, {@link TApplicationStatePersister}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Revision: $ $Date: $
* @package System
* @since 3.0
*/
-interface IPageStatePersister
+interface IStatePersister
{
/**
* Loads state from a persistent storage.