forked from canni/YiiMongoDbSuite
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EMongoHttpSession.php
227 lines (212 loc) · 5.51 KB
/
EMongoHttpSession.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @author Ianaré Sévi (merge into EMongoDB)
* @author aoyagikouhei (original author)
* @license New BSD license
* @version 1.3
* @category ext
* @package ext.YiiMongoDbSuite
*/
/**
* EMongoHttpSession
*
* Example, in config/main.php:
* 'session' => array(
* 'class' => 'ext.EMongoDbHttpSession',
* 'collectionName' => 'yiisession',
* 'idColumn' => 'id',
* 'dataColumn' => 'data',
* 'expireColumn' => 'expire',
* ),
*
* Options:
* connectionID : mongo component name : default mongodb
* collectionName : collaction name : default yiisession
* idColumn : id column name : default id
* dataColumn : data column name : default dada
* expireColumn : expire column name : default expire
* fsync : fsync flag : default false
* safe : safe flag : default false
*
*/
class EMongoHttpSession extends CHttpSession
{
/**
* @var string Mongo DB component.
*/
public $connectionID = 'mongodb';
/**
* @var string Collection name
*/
public $collectionName = 'yiisession';
/**
* @var string id column name
*/
public $idColumn = 'id';
/**
* @var string level data name
*/
public $dataColumn = 'data';
/**
* @var string expire column name
*/
public $expireColumn = 'expire';
/**
* @var boolean forces the update to be synced to disk before returning success.
*/
public $fsync = false;
/**
* @var boolean the program will wait for the database response.
*/
public $safe = false;
/**
* @var array insert options
*/
protected $_options;
/**
* @var MongoCollection mongo Db collection
*/
protected $_collection;
/**
* Returns current MongoCollection object.
* @return MongoCollection
*/
protected function setCollection($collectionName)
{
if (!isset($this->_collection))
{
$db = Yii::app()->getComponent($this->connectionID);
if (!($db instanceof EMongoDB))
throw new EMongoException('EMongoHttpSession.connectionID is invalid');
$this->_collection = $db->getDbInstance()->selectCollection($collectionName);
}
return $this->_collection;
}
/**
* Initializes the route.
* This method is invoked after the route is created by the route manager.
*/
public function init()
{
$this->setCollection($this->collectionName);
$this->_options = array(
'fsync' => $this->fsync,
'safe' => $this->safe
);
parent::init();
}
protected function getData($id)
{
return $this->_collection->findOne(array($this->idColumn => $id), array($this->dataColumn));
}
protected function getExipireTime()
{
return time() + $this->getTimeout();
}
/**
* Returns a value indicating whether to use custom session storage.
* This method overrides the parent implementation and always returns true.
* @return boolean whether to use custom storage.
*/
public function getUseCustomStorage()
{
return true;
}
/**
* Session open handler.
* Do not call this method directly.
* @param string $savePath session save path
* @param string $sessionName session name
* @return boolean whether session is opened successfully
*/
public function openSession($savePath, $sessionName)
{
$this->gcSession(0);
}
/**
* Session read handler.
* Do not call this method directly.
* @param string $id session ID
* @return string the session data
*/
public function readSession($id)
{
$row = $this->getData($id);
return is_null($row) ? '' : $row[$this->dataColumn];
}
/**
* Session write handler.
* Do not call this method directly.
* @param string $id session ID
* @param string $data session data
* @return boolean whether session write is successful
*/
public function writeSession($id, $data)
{
$options = $this->_options;
$options['upsert'] = true;
return $this->_collection->update(
array($this->idColumn => $id), array(
$this->dataColumn => $data,
$this->expireColumn => $this->getExipireTime(),
$this->idColumn => $id
), $options
);
}
/**
* Session destroy handler.
* Do not call this method directly.
* @param string $id session ID
* @return boolean whether session is destroyed successfully
*/
public function destroySession($id)
{
return $this->_collection->remove(
array($this->idColumn => $id), $this->_options);
}
/**
* Session GC (garbage collection) handler.
* Do not call this method directly.
* @param integer $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up.
* @return boolean whether session is GCed successfully
*/
public function gcSession($maxLifetime)
{
return $this->_collection->remove(array($this->expireColumn => array('$lt' => time())), $this->_options);
}
/**
* Updates the current session id with a newly generated one.
* Please refer to {@link http://php.net/session_regenerate_id} for more details.
* @param boolean $deleteOldSession Whether to delete the old associated session file or not.
* @since 1.1.8
*/
public function regenerateID($deleteOldSession = false)
{
$oldId = session_id();
;
parent::regenerateID(false);
$newId = session_id();
$row = $this->getData($oldId);
if (is_null($row))
{
$this->_collection->insert(array(
$this->idColumn => $newId
, $this->expireColumn => $this->getExipireTime()
), $this->_options);
}
else if ($deleteOldSession)
{
$this->_collection->update(
array($this->idColumn => $oldId)
, array($this->idColumn => $newId)
, $this->_options
);
}
else
{
$row[$this->idColumn] = $newId;
unset($row['_id']);
$this->_collection->insert($row, $this->_options);
}
}
}