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