@@ -67,25 +67,28 @@ class MongoDbSessionHandler implements \SessionHandlerInterface
67
67
* @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
68
68
* @throws \InvalidArgumentException When "database" or "collection" not provided
69
69
*/
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 ' );
75
76
76
- if (!isset ($ options ['database ' ]) || !isset ($ options ['collection ' ])) {
77
- throw new \InvalidArgumentException ('You must provide the "database" and "collection" option for MongoDBSessionHandler ' );
78
- }
77
+ }
79
78
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
+ }
81
82
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
+ }
89
92
90
93
/**
91
94
* {@inheritdoc}
@@ -108,9 +111,16 @@ public function close()
108
111
*/
109
112
public function destroy ($ sessionId )
110
113
{
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
+ }
114
124
115
125
return true ;
116
126
}
@@ -120,9 +130,16 @@ public function destroy($sessionId)
120
130
*/
121
131
public function gc ($ maxlifetime )
122
132
{
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
+ }
126
143
127
144
return true ;
128
145
}
@@ -140,11 +157,20 @@ public function write($sessionId, $data)
140
157
$ this ->options ['expiry_field ' ] => $ expiry ,
141
158
);
142
159
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
+ }
148
174
149
175
return true ;
150
176
}
0 commit comments