summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxue <>2006-04-11 00:59:17 +0000
committerxue <>2006-04-11 00:59:17 +0000
commit0d8044202bbd031e2a5e32166e568fe626263152 (patch)
tree209203cc13b482dc31929197fdb62b6127bed1a5
parent37f4d81a1ce6bdea84b00a38b0c8fc32f82f9e5d (diff)
Added TSessionPageStatePersister.
-rw-r--r--.gitattributes1
-rw-r--r--HISTORY2
-rw-r--r--framework/Exceptions/messages.txt4
-rw-r--r--framework/Web/UI/TSessionPageStatePersister.php132
4 files changed, 138 insertions, 1 deletions
diff --git a/.gitattributes b/.gitattributes
index a94f6e54..9d7ac97e 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -769,6 +769,7 @@ framework/Web/UI/TForm.php -text
framework/Web/UI/THtmlWriter.php -text
framework/Web/UI/TPage.php -text
framework/Web/UI/TPageStatePersister.php -text
+framework/Web/UI/TSessionPageStatePersister.php -text
framework/Web/UI/TTemplateControl.php -text
framework/Web/UI/TTemplateManager.php -text
framework/Web/UI/TThemeManager.php -text
diff --git a/HISTORY b/HISTORY
index ddb3e621..898b92fa 100644
--- a/HISTORY
+++ b/HISTORY
@@ -4,6 +4,8 @@ ENH: Format string in classes extending TDataGridColumn can now evaluate an expr
ENH: Format string in classes extending TListControl can now evaluate an expression (Qiang)
NEW: TOutputCache (Qiang)
NEW: TQueue (Qiang)
+NEW: TPageStateFormatter (Qiang)
+NEW: TSessionPageStatePersister (Qiang)
Version 3.0RC2 April 16, 2006
=============================
diff --git a/framework/Exceptions/messages.txt b/framework/Exceptions/messages.txt
index bb6436c3..e53255cd 100644
--- a/framework/Exceptions/messages.txt
+++ b/framework/Exceptions/messages.txt
@@ -198,7 +198,9 @@ tablestyle_cellpadding_invalid = TTableStyle.CellPadding must take an integer
tablestyle_cellspacing_invalid = TTableStyle.CellSpacing must take an integer equal to or greater than -1.
pagestatepersister_pagestate_corrupted = Page state is corrupted.
-pagestatepersister_privatekey_invalid = TPageStatePersister.PrivateKey must take a string no shorter than 8 characters.
+
+sessionpagestatepersister_pagestate_corrupted = Page state is corrupted.
+sessionpagestatepersister_historysize_invalid = TSessionPageStatePersister.History must be an integer greater than 0.
listitemcollection_item_invalid = TListItemCollection can only take strings or TListItem objects.
diff --git a/framework/Web/UI/TSessionPageStatePersister.php b/framework/Web/UI/TSessionPageStatePersister.php
new file mode 100644
index 00000000..b003c4bd
--- /dev/null
+++ b/framework/Web/UI/TSessionPageStatePersister.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * TSessionPageStatePersister class file
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.pradosoft.com/
+ * @copyright Copyright &copy; 2005 PradoSoft
+ * @license http://www.pradosoft.com/license/
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI
+ */
+
+/**
+ * TSessionPageStatePersister class
+ *
+ * TSessionPageStatePersister implements a page state persistent method based on
+ * sessions. Page state are stored in user sessions and therefore, this persister
+ * requires session to be started and available.
+ *
+ * TSessionPageStatePersister keeps limited number of history states in session,
+ * mainly to preserve the precious server storage. The number is specified
+ * by {@link setHistorySize HistorySize}, which defaults to 10.
+ *
+ * There are a couple of ways to use TSessionPageStatePersister.
+ * One can override the page's {@link TPage::getStatePersister()} method and
+ * create a TSessionPageStatePersister instance there.
+ * Or one can configure the pages to use TSessionPageStatePersister in page configurations
+ * as follows,
+ * <code>
+ * <pages StatePersisterClass="System.Web.UI.TSessionPageStatePersister" />
+ * </code>
+ * The above configuration will affect the pages under the directory containing
+ * this configuration and all its subdirectories.
+ * To configure individual pages to use TSessionPageStatePersister, use
+ * <code>
+ * <pages>
+ * <page id="PageID" StatePersisterClass="System.Web.UI.TSessionPageStatePersister" />
+ * </pages>
+ * </code>
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Revision: $ $Date: $
+ * @package System.Web.UI
+ * @since 3.1
+ */
+class TSessionPageStatePersister extends TComponent implements IPageStatePersister
+{
+ const STATE_SESSION_KEY='PRADO_SESSION_PAGESTATE';
+ const QUEUE_SESSION_KEY='PRADO_SESSION_STATEQUEUE';
+
+ private $_page;
+ private $_historySize=10;
+
+ /**
+ * @param TPage the page that this persister works for
+ */
+ public function getPage()
+ {
+ return $this->_page;
+ }
+
+ /**
+ * @param TPage the page that this persister works for.
+ */
+ public function setPage(TPage $page)
+ {
+ $this->_page=$page;
+ }
+
+ /**
+ * @return integer maximum number of page states that should be kept in session. Defaults to 10.
+ */
+ public function getHistorySize()
+ {
+ return $this->_historySize;
+ }
+
+ /**
+ * @param integer maximum number of page states that should be kept in session
+ * @throws TInvalidDataValueException if the number is smaller than 1.
+ */
+ public function setHistorySize($value)
+ {
+ if(($value=TPropertyValue::ensureInteger($value))>0)
+ $this->_historySize=$value;
+ else
+ throw new TInvalidDataValueException('sessionpagestatepersister_historysize_invalid');
+ }
+ /**
+ * Saves state in hidden fields.
+ * @param mixed state to be stored
+ */
+ public function save($state)
+ {
+ $session=$this->_page->getSession();
+ $session->open();
+ $data=serialize($state);
+ $timestamp=(string)microtime(true);
+ $key=self::STATE_SESSION_KEY.$timestamp;
+ $session->add($key,$data);
+ if(($queue=$session->itemAt(self::QUEUE_SESSION_KEY))===null)
+ $queue=array();
+ $queue[]=$key;
+ if(count($queue)>$this->getHistorySize())
+ {
+ $expiredKey=array_shift($queue);
+ $session->remove($expiredKey);
+ }
+ $session->add(self::QUEUE_SESSION_KEY,$queue);
+ $this->_page->setClientState(TPageStateFormatter::serialize($this->_page,$timestamp));
+ }
+
+ /**
+ * Loads page state from hidden fields.
+ * @return mixed the restored state
+ * @throws THttpException if page state is corrupted
+ */
+ public function load()
+ {
+ if(($timestamp=TPageStateFormatter::unserialize($this->_page,$this->_page->getRequestClientState()))!==null)
+ {
+ $session=$this->_page->getSession();
+ $session->open();
+ $key=self::STATE_SESSION_KEY.$timestamp;
+ if(($data=$session->itemAt($key))!==null)
+ return unserialize($data);
+ }
+ throw new THttpException(400,'sessionpagestatepersister_pagestate_corrupted');
+ }
+}
+
+?> \ No newline at end of file