[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
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 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 $mongo A 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 \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 $this->getCollection()->remove(array( 112 $this->options['id_field'] => $sessionId, 113 )); 114 115 return true; 116 } 117 118 /** 119 * {@inheritdoc} 120 */ 121 public function gc($maxlifetime) 122 { 123 $this->getCollection()->remove(array( 124 $this->options['expiry_field'] => array('$lt' => new \MongoDate()), 125 )); 126 127 return true; 128 } 129 130 /** 131 * {@inheritdoc} 132 */ 133 public function write($sessionId, $data) 134 { 135 $expiry = new \MongoDate(time() + (int) ini_get('session.gc_maxlifetime')); 136 137 $fields = array( 138 $this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY), 139 $this->options['time_field'] => new \MongoDate(), 140 $this->options['expiry_field'] => $expiry, 141 ); 142 143 $this->getCollection()->update( 144 array($this->options['id_field'] => $sessionId), 145 array('$set' => $fields), 146 array('upsert' => true, 'multiple' => false) 147 ); 148 149 return true; 150 } 151 152 /** 153 * {@inheritdoc} 154 */ 155 public function read($sessionId) 156 { 157 $dbData = $this->getCollection()->findOne(array( 158 $this->options['id_field'] => $sessionId, 159 $this->options['expiry_field'] => array('$gte' => new \MongoDate()), 160 )); 161 162 return null === $dbData ? '' : $dbData[$this->options['data_field']]->bin; 163 } 164 165 /** 166 * Return a "MongoCollection" instance. 167 * 168 * @return \MongoCollection 169 */ 170 private function getCollection() 171 { 172 if (null === $this->collection) { 173 $this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']); 174 } 175 176 return $this->collection; 177 } 178 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |