Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Session / SaveHandler / MongoDB.php
blob87ee33522e0d7ae5989f12c0697cfd0b3293711d
1 <?php
3 /**
4 * Zend Framework (http://framework.zend.com/)
6 * @link http://github.com/zendframework/zf2 for the canonical source repository
7 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
8 * @license http://framework.zend.com/license/new-bsd New BSD License
9 */
11 namespace Zend\Session\SaveHandler;
13 use Mongo;
14 use MongoDate;
15 use Zend\Session\Exception\InvalidArgumentException;
17 /**
18 * MongoDB session save handler
20 class MongoDB implements SaveHandlerInterface
22 /**
23 * MongoCollection instance
25 * @var MongoCollection
27 protected $mongoCollection;
29 /**
30 * Session name
32 * @var string
34 protected $sessionName;
36 /**
37 * Session lifetime
39 * @var int
41 protected $lifetime;
43 /**
44 * MongoDB session save handler options
45 * @var MongoDBOptions
47 protected $options;
49 /**
50 * Constructor
52 * @param Mongo|MongoClient $mongo
53 * @param MongoDBOptions $options
54 * @throws Zend\Session\Exception\InvalidArgumentException
56 public function __construct($mongo, MongoDBOptions $options)
58 if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
59 throw new InvalidArgumentException(
60 'Parameter of type %s is invalid; must be MongoClient or Mongo',
61 (is_object($mongo) ? get_class($mongo) : gettype($mongo))
65 if (null === ($database = $options->getDatabase())) {
66 throw new InvalidArgumentException('The database option cannot be emtpy');
69 if (null === ($collection = $options->getCollection())) {
70 throw new InvalidArgumentException('The collection option cannot be emtpy');
73 $this->mongoCollection = $mongo->selectCollection($database, $collection);
74 $this->options = $options;
77 /**
78 * Open session
80 * @param string $savePath
81 * @param string $name
82 * @return bool
84 public function open($savePath, $name)
86 // Note: session save path is not used
87 $this->sessionName = $name;
88 $this->lifetime = ini_get('session.gc_maxlifetime');
90 return true;
93 /**
94 * Close session
96 * @return bool
98 public function close()
100 return true;
104 * Read session data
106 * @param string $id
107 * @return string
109 public function read($id)
111 $session = $this->mongoCollection->findOne(array(
112 '_id' => $id,
113 $this->options->getNameField() => $this->sessionName,
116 if (null !== $session) {
117 if ($session[$this->options->getModifiedField()] instanceof MongoDate &&
118 $session[$this->options->getModifiedField()]->sec +
119 $session[$this->options->getLifetimeField()] > time()) {
120 return $session[$this->options->getDataField()];
122 $this->destroy($id);
125 return '';
129 * Write session data
131 * @param string $id
132 * @param string $data
133 * @return bool
135 public function write($id, $data)
137 $saveOptions = array_replace(
138 $this->options->getSaveOptions(),
139 array('upsert' => true, 'multiple' => false)
142 $criteria = array(
143 '_id' => $id,
144 $this->options->getNameField() => $this->sessionName,
147 $newObj = array('$set' => array(
148 $this->options->getDataField() => (string) $data,
149 $this->options->getLifetimeField() => $this->lifetime,
150 $this->options->getModifiedField() => new MongoDate(),
153 /* Note: a MongoCursorException will be thrown if a record with this ID
154 * already exists with a different session name, since the upsert query
155 * cannot insert a new document with the same ID and new session name.
156 * This should only happen if ID's are not unique or if the session name
157 * is altered mid-process.
159 $result = $this->mongoCollection->update($criteria, $newObj, $saveOptions);
161 return (bool) (isset($result['ok']) ? $result['ok'] : $result);
165 * Destroy session
167 * @param string $id
168 * @return bool
170 public function destroy($id)
172 $result = $this->mongoCollection->remove(array(
173 '_id' => $id,
174 $this->options->getNameField() => $this->sessionName,
175 ), $this->options->getSaveOptions());
177 return (bool) (isset($result['ok']) ? $result['ok'] : $result);
181 * Garbage collection
183 * Note: MongoDB 2.2+ supports TTL collections, which may be used in place
184 * of this method by indexing the "modified" field with an
185 * "expireAfterSeconds" option. Regardless of whether TTL collections are
186 * used, consider indexing this field to make the remove query more
187 * efficient.
189 * @see http://docs.mongodb.org/manual/tutorial/expire-data/
190 * @param int $maxlifetime
191 * @return bool
193 public function gc($maxlifetime)
195 /* Note: unlike DbTableGateway, we do not use the lifetime field in
196 * each document. Doing so would require a $where query to work with the
197 * computed value (modified + lifetime) and be very inefficient.
199 $result = $this->mongoCollection->remove(array(
200 $this->options->getModifiedField() => array('$lt' => new MongoDate(time() - $maxlifetime)),
201 ), $this->options->getSaveOptions());
203 return (bool) (isset($result['ok']) ? $result['ok'] : $result);