4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Session\Storage\Handler
;
15 * @author Markus Bachmann <markus.bachmann@bachi.biz>
17 class MongoDbSessionHandler
implements \SessionHandlerInterface
22 * @var \MongoCollection
34 * List of available options:
35 * * database: The name of the database [required]
36 * * collection: The name of the collection [required]
37 * * id_field: The field name for storing the session id [default: _id]
38 * * data_field: The field name for storing the session data [default: data]
39 * * time_field: The field name for storing the timestamp [default: time]
40 * * expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
42 * It is strongly recommended to put an index on the `expiry_field` for
43 * garbage-collection. Alternatively it's possible to automatically expire
44 * the sessions in the database as described below:
46 * A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions
47 * automatically. Such an index can for example look like this:
49 * db.<session-collection>.ensureIndex(
50 * { "<expiry-field>": 1 },
51 * { "expireAfterSeconds": 0 }
54 * More details on: http://docs.mongodb.org/manual/tutorial/expire-data/
56 * If you use such an index, you can drop `gc_probability` to 0 since
57 * no garbage-collection is required.
59 * @param \Mongo|\MongoClient|\MongoDB\Client $mongo A MongoDB\Client, MongoClient or Mongo instance
60 * @param array $options An associative array of field options
62 * @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
63 * @throws \InvalidArgumentException When "database" or "collection" not provided
65 public function __construct($mongo, array $options)
67 if (!($mongo instanceof \MongoDB\Client ||
$mongo instanceof \MongoClient ||
$mongo instanceof \Mongo
)) {
68 throw new \
InvalidArgumentException('MongoClient or Mongo instance required');
71 if (!isset($options['database']) ||
!isset($options['collection'])) {
72 throw new \
InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
75 $this->mongo
= $mongo;
77 $this->options
= array_merge(array(
79 'data_field' => 'data',
80 'time_field' => 'time',
81 'expiry_field' => 'expires_at',
88 public function open($savePath, $sessionName)
96 public function close()
104 public function destroy($sessionId)
106 $methodName = $this->mongo
instanceof \MongoDB\Client ?
'deleteOne' : 'remove';
108 $this->getCollection()->$methodName(array(
109 $this->options
['id_field'] => $sessionId,
118 public function gc($maxlifetime)
120 $methodName = $this->mongo
instanceof \MongoDB\Client ?
'deleteMany' : 'remove';
122 $this->getCollection()->$methodName(array(
123 $this->options
['expiry_field'] => array('$lt' => $this->createDateTime()),
132 public function write($sessionId, $data)
134 $expiry = $this->createDateTime(time() +
(int) ini_get('session.gc_maxlifetime'));
137 $this->options
['time_field'] => $this->createDateTime(),
138 $this->options
['expiry_field'] => $expiry,
141 $options = array('upsert' => true);
143 if ($this->mongo
instanceof \MongoDB\Client
) {
144 $fields[$this->options
['data_field']] = new \MongoDB\BSON\
Binary($data, \MongoDB\BSON\Binary
::TYPE_OLD_BINARY
);
146 $fields[$this->options
['data_field']] = new \
MongoBinData($data, \MongoBinData
::BYTE_ARRAY
);
147 $options['multiple'] = false;
150 $methodName = $this->mongo
instanceof \MongoDB\Client ?
'updateOne' : 'update';
152 $this->getCollection()->$methodName(
153 array($this->options
['id_field'] => $sessionId),
154 array('$set' => $fields),
164 public function read($sessionId)
166 $dbData = $this->getCollection()->findOne(array(
167 $this->options
['id_field'] => $sessionId,
168 $this->options
['expiry_field'] => array('$gte' => $this->createDateTime()),
171 if (null === $dbData) {
175 if ($dbData[$this->options
['data_field']] instanceof \MongoDB\BSON\Binary
) {
176 return $dbData[$this->options
['data_field']]->getData();
179 return $dbData[$this->options
['data_field']]->bin
;
183 * Return a "MongoCollection" instance.
185 * @return \MongoCollection
187 private function getCollection()
189 if (null === $this->collection
) {
190 $this->collection
= $this->mongo
->selectCollection($this->options
['database'], $this->options
['collection']);
193 return $this->collection
;
197 * Return a Mongo instance.
199 * @return \Mongo|\MongoClient|\MongoDB\Client
201 protected function getMongo()
207 * Create a date object using the class appropriate for the current mongo connection.
209 * Return an instance of a MongoDate or \MongoDB\BSON\UTCDateTime
211 * @param int $seconds An integer representing UTC seconds since Jan 1 1970. Defaults to now.
213 * @return \MongoDate|\MongoDB\BSON\UTCDateTime
215 private function createDateTime($seconds = null)
217 if (null === $seconds) {
221 if ($this->mongo
instanceof \MongoDB\Client
) {
222 return new \MongoDB\BSON\
UTCDateTime($seconds * 1000);
225 return new \
MongoDate($seconds);