MongoDbSessionHandler.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  11. /**
  12. * MongoDB session handler.
  13. *
  14. * @author Markus Bachmann <markus.bachmann@bachi.biz>
  15. */
  16. class MongoDbSessionHandler implements \SessionHandlerInterface
  17. {
  18. /**
  19. * @var \Mongo|\MongoClient|\MongoDB\Client
  20. */
  21. private $mongo;
  22. /**
  23. * @var \MongoCollection
  24. */
  25. private $collection;
  26. /**
  27. * @var array
  28. */
  29. private $options;
  30. /**
  31. * Constructor.
  32. *
  33. * List of available options:
  34. * * database: The name of the database [required]
  35. * * collection: The name of the collection [required]
  36. * * id_field: The field name for storing the session id [default: _id]
  37. * * data_field: The field name for storing the session data [default: data]
  38. * * time_field: The field name for storing the timestamp [default: time]
  39. * * expiry_field: The field name for storing the expiry-timestamp [default: expires_at]
  40. *
  41. * It is strongly recommended to put an index on the `expiry_field` for
  42. * garbage-collection. Alternatively it's possible to automatically expire
  43. * the sessions in the database as described below:
  44. *
  45. * A TTL collections can be used on MongoDB 2.2+ to cleanup expired sessions
  46. * automatically. Such an index can for example look like this:
  47. *
  48. * db.<session-collection>.ensureIndex(
  49. * { "<expiry-field>": 1 },
  50. * { "expireAfterSeconds": 0 }
  51. * )
  52. *
  53. * More details on: http://docs.mongodb.org/manual/tutorial/expire-data/
  54. *
  55. * If you use such an index, you can drop `gc_probability` to 0 since
  56. * no garbage-collection is required.
  57. *
  58. * @param \Mongo|\MongoClient|\MongoDB\Client $mongo A MongoDB\Client, MongoClient or Mongo instance
  59. * @param array $options An associative array of field options
  60. *
  61. * @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
  62. * @throws \InvalidArgumentException When "database" or "collection" not provided
  63. */
  64. public function __construct($mongo, array $options)
  65. {
  66. if (!($mongo instanceof \MongoDB\Client || $mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
  67. throw new \InvalidArgumentException('MongoClient or Mongo instance required');
  68. }
  69. if (!isset($options['database']) || !isset($options['collection'])) {
  70. throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
  71. }
  72. $this->mongo = $mongo;
  73. $this->options = array_merge(array(
  74. 'id_field' => '_id',
  75. 'data_field' => 'data',
  76. 'time_field' => 'time',
  77. 'expiry_field' => 'expires_at',
  78. ), $options);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function open($savePath, $sessionName)
  84. {
  85. return true;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function close()
  91. {
  92. return true;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function destroy($sessionId)
  98. {
  99. $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove';
  100. $this->getCollection()->$methodName(array(
  101. $this->options['id_field'] => $sessionId,
  102. ));
  103. return true;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function gc($maxlifetime)
  109. {
  110. $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove';
  111. $this->getCollection()->$methodName(array(
  112. $this->options['expiry_field'] => array('$lt' => $this->createDateTime()),
  113. ));
  114. return true;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function write($sessionId, $data)
  120. {
  121. $expiry = $this->createDateTime(time() + (int) ini_get('session.gc_maxlifetime'));
  122. $fields = array(
  123. $this->options['time_field'] => $this->createDateTime(),
  124. $this->options['expiry_field'] => $expiry,
  125. );
  126. $options = array('upsert' => true);
  127. if ($this->mongo instanceof \MongoDB\Client) {
  128. $fields[$this->options['data_field']] = new \MongoDB\BSON\Binary($data, \MongoDB\BSON\Binary::TYPE_OLD_BINARY);
  129. } else {
  130. $fields[$this->options['data_field']] = new \MongoBinData($data, \MongoBinData::BYTE_ARRAY);
  131. $options['multiple'] = false;
  132. }
  133. $methodName = $this->mongo instanceof \MongoDB\Client ? 'updateOne' : 'update';
  134. $this->getCollection()->$methodName(
  135. array($this->options['id_field'] => $sessionId),
  136. array('$set' => $fields),
  137. $options
  138. );
  139. return true;
  140. }
  141. /**
  142. * {@inheritdoc}
  143. */
  144. public function read($sessionId)
  145. {
  146. $dbData = $this->getCollection()->findOne(array(
  147. $this->options['id_field'] => $sessionId,
  148. $this->options['expiry_field'] => array('$gte' => $this->createDateTime()),
  149. ));
  150. if (null === $dbData) {
  151. return '';
  152. }
  153. if ($dbData[$this->options['data_field']] instanceof \MongoDB\BSON\Binary) {
  154. return $dbData[$this->options['data_field']]->getData();
  155. }
  156. return $dbData[$this->options['data_field']]->bin;
  157. }
  158. /**
  159. * Return a "MongoCollection" instance.
  160. *
  161. * @return \MongoCollection
  162. */
  163. private function getCollection()
  164. {
  165. if (null === $this->collection) {
  166. $this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
  167. }
  168. return $this->collection;
  169. }
  170. /**
  171. * Return a Mongo instance.
  172. *
  173. * @return \Mongo|\MongoClient|\MongoDB\Client
  174. */
  175. protected function getMongo()
  176. {
  177. return $this->mongo;
  178. }
  179. /**
  180. * Create a date object using the class appropriate for the current mongo connection.
  181. *
  182. * Return an instance of a MongoDate or \MongoDB\BSON\UTCDateTime
  183. *
  184. * @param int $seconds An integer representing UTC seconds since Jan 1 1970. Defaults to now.
  185. *
  186. * @return \MongoDate|\MongoDB\BSON\UTCDateTime
  187. */
  188. private function createDateTime($seconds = null)
  189. {
  190. if (null === $seconds) {
  191. $seconds = time();
  192. }
  193. if ($this->mongo instanceof \MongoDB\Client) {
  194. return new \MongoDB\BSON\UTCDateTime($seconds * 1000);
  195. }
  196. return new \MongoDate($seconds);
  197. }
  198. }