Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
MongoDbSessionHandler.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
013
014 /**
015 * MongoDB session handler
016 *
017 * @author Markus Bachmann <markus.bachmann@bachi.biz>
018 */
019 class MongoDbSessionHandler implements \SessionHandlerInterface
020 {
021 /**
022 * @var \Mongo
023 */
024 private $mongo;
025
026 /**
027 * @var \MongoCollection
028 */
029 private $collection;
030
031 /**
032 * @var array
033 */
034 private $options;
035
036 /**
037 * Constructor.
038 *
039 * List of available options:
040 * * database: The name of the database [required]
041 * * collection: The name of the collection [required]
042 * * id_field: The field name for storing the session id [default: _id]
043 * * data_field: The field name for storing the session data [default: data]
044 * * time_field: The field name for storing the timestamp [default: time]
045 *
046 * @param \Mongo|\MongoClient $mongo A MongoClient or Mongo instance
047 * @param array $options An associative array of field options
048 *
049 * @throws \InvalidArgumentException When MongoClient or Mongo instance not provided
050 * @throws \InvalidArgumentException When "database" or "collection" not provided
051 */
052 public function __construct($mongo, array $options)
053 {
054 if (!($mongo instanceof \MongoClient || $mongo instanceof \Mongo)) {
055 throw new \InvalidArgumentException('MongoClient or Mongo instance required');
056 }
057
058 if (!isset($options['database']) || !isset($options['collection'])) {
059 throw new \InvalidArgumentException('You must provide the "database" and "collection" option for MongoDBSessionHandler');
060 }
061
062 $this->mongo = $mongo;
063
064 $this->options = array_merge(array(
065 'id_field' => '_id',
066 'data_field' => 'data',
067 'time_field' => 'time',
068 ), $options);
069 }
070
071 /**
072 * {@inheritdoc}
073 */
074 public function open($savePath, $sessionName)
075 {
076 return true;
077 }
078
079 /**
080 * {@inheritdoc}
081 */
082 public function close()
083 {
084 return true;
085 }
086
087 /**
088 * {@inheritdoc}
089 */
090 public function destroy($sessionId)
091 {
092 $this->getCollection()->remove(array(
093 $this->options['id_field'] => $sessionId,
094 ));
095
096 return true;
097 }
098
099 /**
100 * {@inheritdoc}
101 */
102 public function gc($maxlifetime)
103 {
104 /* Note: MongoDB 2.2+ supports TTL collections, which may be used in
105 * place of this method by indexing the "time_field" field with an
106 * "expireAfterSeconds" option. Regardless of whether TTL collections
107 * are used, consider indexing this field to make the remove query more
108 * efficient.
109 *
110 * See: http://docs.mongodb.org/manual/tutorial/expire-data/
111 */
112 $time = new \MongoDate(time() - $maxlifetime);
113
114 $this->getCollection()->remove(array(
115 $this->options['time_field'] => array('$lt' => $time),
116 ));
117
118 return true;
119 }
120
121 /**
122 * {@inheritdoc}
123 */
124 public function write($sessionId, $data)
125 {
126 $this->getCollection()->update(
127 array($this->options['id_field'] => $sessionId),
128 array('$set' => array(
129 $this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY),
130 $this->options['time_field'] => new \MongoDate(),
131 )),
132 array('upsert' => true, 'multiple' => false)
133 );
134
135 return true;
136 }
137
138 /**
139 * {@inheritdoc}
140 */
141 public function read($sessionId)
142 {
143 $dbData = $this->getCollection()->findOne(array(
144 $this->options['id_field'] => $sessionId,
145 ));
146
147 return null === $dbData ? '' : $dbData[$this->options['data_field']]->bin;
148 }
149
150 /**
151 * Return a "MongoCollection" instance
152 *
153 * @return \MongoCollection
154 */
155 private function getCollection()
156 {
157 if (null === $this->collection) {
158 $this->collection = $this->mongo->selectCollection($this->options['database'], $this->options['collection']);
159 }
160
161 return $this->collection;
162 }
163 }
164