Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 500c71c

Browse filesBrowse files
committed
[HTTP][Session][Mongo] MongoDB driver support
1 parent ea9d6e7 commit 500c71c
Copy full SHA for 500c71c

File tree

1 file changed

+53
-27
lines changed
Filter options

1 file changed

+53
-27
lines changed

‎src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MongoDbSessionHandler.php
+53-27Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,28 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
6767
* @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
6868
* @throws \InvalidArgumentException When "database" or "collection" not provided
6969
*/
70-
public function __construct($mongo, array $options)
71-
{
72-
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
73-
throw new \InvalidArgumentException('MongoClient or Mongo instance required');
74-
}
70+
public function __construct($mongo, array $options)
71+
{
72+
if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo || $mongo instanceof \MongoDB\Client)) {
73+
throw new \InvalidArgumentException('MongoClient, Mongo or MongoDB instance required');
74+
}elseif($mongo instanceof \MongoDB\Client && !class_exists('\MongoDB\Collection')){
75+
throw new \InvalidArgumentException('\MongoDB\Collection instance not loaded');
7576

76-
if (!isset($options['database']) || !isset($options['collection'])) {
77-
throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
78-
}
77+
}
7978

80-
$this->mongo = $mongo;
79+
if (!isset($options['database']) || !isset($options['collection'])) {
80+
throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
81+
}
8182

82-
$this->options = array_merge(array(
83-
'id_field' => '_id',
84-
'data_field' => 'data',
85-
'time_field' => 'time',
86-
'expiry_field' => 'expires_at',
87-
), $options);
88-
}
83+
$this->mongo = $mongo;
84+
85+
$this->options = array_merge(array(
86+
'id_field' => '_id',
87+
'data_field' => 'data',
88+
'time_field' => 'time',
89+
'expiry_field' => 'expires_at',
90+
), $options);
91+
}
8992

9093
/**
9194
* {@inheritdoc}
@@ -108,9 +111,16 @@ public function close()
108111
*/
109112
public function destroy($sessionId)
110113
{
111-
$this->getCollection()->remove(array(
112-
$this->options['id_field'] => $sessionId,
113-
));
114+
if($this->getCollection() instanceof \MongoDB\Collection)
115+
{
116+
$this->getCollection()->deleteOne(array(
117+
$this->options['id_field'] => $sessionId,
118+
));
119+
}else{
120+
$this->getCollection()->remove(array(
121+
$this->options['id_field'] => $sessionId,
122+
));
123+
}
114124

115125
return true;
116126
}
@@ -120,9 +130,16 @@ public function destroy($sessionId)
120130
*/
121131
public function gc($maxlifetime)
122132
{
123-
$this->getCollection()->remove(array(
124-
$this->options['expiry_field'] => array('$lt' => new \MongoDate()),
125-
));
133+
if($this->getCollection() instanceof \MongoDB\Collection)
134+
{
135+
$this->getCollection()->deleteMany(array(
136+
$this->options['id_field'] => $sessionId,
137+
));
138+
}else{
139+
$this->getCollection()->remove(array(
140+
$this->options['expiry_field'] => array('$lt' => new \MongoDate()),
141+
));
142+
}
126143

127144
return true;
128145
}
@@ -140,11 +157,20 @@ public function write($sessionId, $data)
140157
$this->options['expiry_field'] => $expiry,
141158
);
142159

143-
$this->getCollection()->update(
144-
array($this->options['id_field'] => $sessionId),
145-
array('$set' => $fields),
146-
array('upsert' => true, 'multiple' => false)
147-
);
160+
if($this->getCollection() instanceof \MongoDB\Collection)
161+
{
162+
$this->getCollection()->updateOne(
163+
array($this->options['id_field'] => $sessionId),
164+
array('$set' => $fields),
165+
array('upsert' => true, 'multiple' => false)
166+
);
167+
}else{
168+
$this->getCollection()->update(
169+
array($this->options['id_field'] => $sessionId),
170+
array('$set' => $fields),
171+
array('upsert' => true, 'multiple' => false)
172+
);
173+
}
148174

149175
return true;
150176
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.