comparison vendor/symfony/http-foundation/Session/Storage/Handler/MongoDbSessionHandler.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
10 */ 10 */
11 11
12 namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; 12 namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
13 13
14 /** 14 /**
15 * MongoDB session handler. 15 * Session handler using the mongodb/mongodb package and MongoDB driver extension.
16 * 16 *
17 * @author Markus Bachmann <markus.bachmann@bachi.biz> 17 * @author Markus Bachmann <markus.bachmann@bachi.biz>
18 *
19 * @see https://packagist.org/packages/mongodb/mongodb
20 * @see http://php.net/manual/en/set.mongodb.php
18 */ 21 */
19 class MongoDbSessionHandler implements \SessionHandlerInterface 22 class MongoDbSessionHandler extends AbstractSessionHandler
20 { 23 {
21 /**
22 * @var \Mongo|\MongoClient|\MongoDB\Client
23 */
24 private $mongo; 24 private $mongo;
25 25
26 /** 26 /**
27 * @var \MongoCollection 27 * @var \MongoCollection
28 */ 28 */
40 * * database: The name of the database [required] 40 * * database: The name of the database [required]
41 * * collection: The name of the collection [required] 41 * * collection: The name of the collection [required]
42 * * id_field: The field name for storing the session id [default: _id] 42 * * id_field: The field name for storing the session id [default: _id]
43 * * data_field: The field name for storing the session data [default: data] 43 * * data_field: The field name for storing the session data [default: data]
44 * * time_field: The field name for storing the timestamp [default: time] 44 * * time_field: The field name for storing the timestamp [default: time]
45 * * expiry_field: The field name for storing the expiry-timestamp [default: expires_at] 45 * * expiry_field: The field name for storing the expiry-timestamp [default: expires_at].
46 * 46 *
47 * It is strongly recommended to put an index on the `expiry_field` for 47 * It is strongly recommended to put an index on the `expiry_field` for
48 * garbage-collection. Alternatively it's possible to automatically expire 48 * garbage-collection. Alternatively it's possible to automatically expire
49 * the sessions in the database as described below: 49 * the sessions in the database as described below:
50 * 50 *
59 * More details on: http://docs.mongodb.org/manual/tutorial/expire-data/ 59 * More details on: http://docs.mongodb.org/manual/tutorial/expire-data/
60 * 60 *
61 * If you use such an index, you can drop `gc_probability` to 0 since 61 * If you use such an index, you can drop `gc_probability` to 0 since
62 * no garbage-collection is required. 62 * no garbage-collection is required.
63 * 63 *
64 * @param \Mongo|\MongoClient|\MongoDB\Client $mongo A MongoDB\Client, MongoClient or Mongo instance 64 * @param \MongoDB\Client $mongo A MongoDB\Client instance
65 * @param array $options An associative array of field options 65 * @param array $options An associative array of field options
66 * 66 *
67 * @throws \InvalidArgumentException When MongoClient or Mongo instance not provided 67 * @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
68 * @throws \InvalidArgumentException When "database" or "collection" not provided 68 * @throws \InvalidArgumentException When "database" or "collection" not provided
69 */ 69 */
70 public function __construct($mongo, array $options) 70 public function __construct($mongo, array $options)
71 { 71 {
72 if ($mongo instanceof \MongoClient || $mongo instanceof \Mongo) {
73 @trigger_error(sprintf('Using %s with the legacy mongo extension is deprecated as of 3.4 and will be removed in 4.0. Use it with the mongodb/mongodb package and ext-mongodb instead.', __CLASS__), E_USER_DEPRECATED);
74 }
75
72 if (!($mongo instanceof \MongoDB\Client || $mongo instanceof \MongoClient || $mongo instanceof \Mongo)) { 76 if (!($mongo instanceof \MongoDB\Client || $mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
73 throw new \InvalidArgumentException('MongoClient or Mongo instance required'); 77 throw new \InvalidArgumentException('MongoClient or Mongo instance required');
74 } 78 }
75 79
76 if (!isset($options['database']) || !isset($options['collection'])) { 80 if (!isset($options['database']) || !isset($options['collection'])) {
88 } 92 }
89 93
90 /** 94 /**
91 * {@inheritdoc} 95 * {@inheritdoc}
92 */ 96 */
93 public function open($savePath, $sessionName)
94 {
95 return true;
96 }
97
98 /**
99 * {@inheritdoc}
100 */
101 public function close() 97 public function close()
102 { 98 {
103 return true; 99 return true;
104 } 100 }
105 101
106 /** 102 /**
107 * {@inheritdoc} 103 * {@inheritdoc}
108 */ 104 */
109 public function destroy($sessionId) 105 protected function doDestroy($sessionId)
110 { 106 {
111 $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove'; 107 $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove';
112 108
113 $this->getCollection()->$methodName(array( 109 $this->getCollection()->$methodName(array(
114 $this->options['id_field'] => $sessionId, 110 $this->options['id_field'] => $sessionId,
120 /** 116 /**
121 * {@inheritdoc} 117 * {@inheritdoc}
122 */ 118 */
123 public function gc($maxlifetime) 119 public function gc($maxlifetime)
124 { 120 {
125 $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteOne' : 'remove'; 121 $methodName = $this->mongo instanceof \MongoDB\Client ? 'deleteMany' : 'remove';
126 122
127 $this->getCollection()->$methodName(array( 123 $this->getCollection()->$methodName(array(
128 $this->options['expiry_field'] => array('$lt' => $this->createDateTime()), 124 $this->options['expiry_field'] => array('$lt' => $this->createDateTime()),
129 )); 125 ));
130 126
132 } 128 }
133 129
134 /** 130 /**
135 * {@inheritdoc} 131 * {@inheritdoc}
136 */ 132 */
137 public function write($sessionId, $data) 133 protected function doWrite($sessionId, $data)
138 { 134 {
139 $expiry = $this->createDateTime(time() + (int) ini_get('session.gc_maxlifetime')); 135 $expiry = $this->createDateTime(time() + (int) ini_get('session.gc_maxlifetime'));
140 136
141 $fields = array( 137 $fields = array(
142 $this->options['time_field'] => $this->createDateTime(), 138 $this->options['time_field'] => $this->createDateTime(),
164 } 160 }
165 161
166 /** 162 /**
167 * {@inheritdoc} 163 * {@inheritdoc}
168 */ 164 */
169 public function read($sessionId) 165 public function updateTimestamp($sessionId, $data)
166 {
167 $expiry = $this->createDateTime(time() + (int) ini_get('session.gc_maxlifetime'));
168
169 if ($this->mongo instanceof \MongoDB\Client) {
170 $methodName = 'updateOne';
171 $options = array();
172 } else {
173 $methodName = 'update';
174 $options = array('multiple' => false);
175 }
176
177 $this->getCollection()->$methodName(
178 array($this->options['id_field'] => $sessionId),
179 array('$set' => array(
180 $this->options['time_field'] => $this->createDateTime(),
181 $this->options['expiry_field'] => $expiry,
182 )),
183 $options
184 );
185
186 return true;
187 }
188
189 /**
190 * {@inheritdoc}
191 */
192 protected function doRead($sessionId)
170 { 193 {
171 $dbData = $this->getCollection()->findOne(array( 194 $dbData = $this->getCollection()->findOne(array(
172 $this->options['id_field'] => $sessionId, 195 $this->options['id_field'] => $sessionId,
173 $this->options['expiry_field'] => array('$gte' => $this->createDateTime()), 196 $this->options['expiry_field'] => array('$gte' => $this->createDateTime()),
174 )); 197 ));