Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TSessionPageStatePersister class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TSessionPageStatePersister.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Web.UI
11
 */
12
 
13
/**
14
 * TSessionPageStatePersister class
15
 *
16
 * TSessionPageStatePersister implements a page state persistent method based on
17
 * sessions. Page state are stored in user sessions and therefore, this persister
18
 * requires session to be started and available.
19
 *
20
 * TSessionPageStatePersister keeps limited number of history states in session,
21
 * mainly to preserve the precious server storage. The number is specified
22
 * by {@link setHistorySize HistorySize}, which defaults to 10.
23
 *
24
 * There are a couple of ways to use TSessionPageStatePersister.
25
 * One can override the page's {@link TPage::getStatePersister()} method and
26
 * create a TSessionPageStatePersister instance there.
27
 * Or one can configure the pages to use TSessionPageStatePersister in page configurations
28
 * as follows,
29
 * <code>
30
 *   <pages StatePersisterClass="System.Web.UI.TSessionPageStatePersister" />
31
 * </code>
32
 * The above configuration will affect the pages under the directory containing
33
 * this configuration and all its subdirectories.
34
 * To configure individual pages to use TSessionPageStatePersister, use
35
 * <code>
36
 *   <pages>
37
 *     <page id="PageID" StatePersisterClass="System.Web.UI.TSessionPageStatePersister" />
38
 *   </pages>
39
 * </code>
40
 *
41
 * @author Qiang Xue <qiang.xue@gmail.com>
42
 * @version $Id: TSessionPageStatePersister.php 2541 2008-10-21 15:05:13Z qiang.xue $
43
 * @package System.Web.UI
44
 * @since 3.1
45
 */
46
class TSessionPageStatePersister extends TComponent implements IPageStatePersister
47
{
48
	const STATE_SESSION_KEY='PRADO_SESSION_PAGESTATE';
49
	const QUEUE_SESSION_KEY='PRADO_SESSION_STATEQUEUE';
50
 
51
	private $_page;
52
	private $_historySize=10;
53
 
54
	/**
55
	 * @param TPage the page that this persister works for
56
	 */
57
	public function getPage()
58
	{
59
		return $this->_page;
60
	}
61
 
62
	/**
63
	 * @param TPage the page that this persister works for.
64
	 */
65
	public function setPage(TPage $page)
66
	{
67
		$this->_page=$page;
68
	}
69
 
70
	/**
71
	 * @return integer maximum number of page states that should be kept in session. Defaults to 10.
72
	 */
73
	public function getHistorySize()
74
	{
75
		return $this->_historySize;
76
	}
77
 
78
	/**
79
	 * @param integer maximum number of page states that should be kept in session
80
	 * @throws TInvalidDataValueException if the number is smaller than 1.
81
	 */
82
	public function setHistorySize($value)
83
	{
84
		if(($value=TPropertyValue::ensureInteger($value))>0)
85
			$this->_historySize=$value;
86
		else
87
			throw new TInvalidDataValueException('sessionpagestatepersister_historysize_invalid');
88
	}
89
	/**
90
	 * Saves state in session.
91
	 * @param mixed state to be stored
92
	 */
93
	public function save($state)
94
	{
95
		$session=$this->_page->getSession();
96
		$session->open();
97
		$data=serialize($state);
98
		$timestamp=(string)microtime(true);
99
		$key=self::STATE_SESSION_KEY.$timestamp;
100
		$session->add($key,$data);
101
		if(($queue=$session->itemAt(self::QUEUE_SESSION_KEY))===null)
102
			$queue=array();
103
		$queue[]=$key;
104
		if(count($queue)>$this->getHistorySize())
105
		{
106
			$expiredKey=array_shift($queue);
107
			$session->remove($expiredKey);
108
		}
109
		$session->add(self::QUEUE_SESSION_KEY,$queue);
110
		$this->_page->setClientState(TPageStateFormatter::serialize($this->_page,$timestamp));
111
	}
112
 
113
	/**
114
	 * Loads page state from session.
115
	 * @return mixed the restored state
116
	 * @throws THttpException if page state is corrupted
117
	 */
118
	public function load()
119
	{
120
		if(($timestamp=TPageStateFormatter::unserialize($this->_page,$this->_page->getRequestClientState()))!==null)
121
		{
122
			$session=$this->_page->getSession();
123
			$session->open();
124
			$key=self::STATE_SESSION_KEY.$timestamp;
125
			if(($data=$session->itemAt($key))!==null)
126
				return unserialize($data);
127
		}
128
		throw new THttpException(400,'sessionpagestatepersister_pagestate_corrupted');
129
	}
130
}
131