[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/Session/Storage/Handler/ -> MongoDbSessionHandler.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1