Example #1
0
 /**
  * Class constructor
  *
  * List of available options for `$config` array:
  *
  * * `group`:       Mango config group name
  * * `collection`:  The name of the collection
  * * `gc`:          Number of requests before gc is invoked
  * * `fields`:      Custom field names, array of:
  *   * `session_id`:   Session identifier
  *   * `last_active`:  Timestamp of the last activity
  *   * `contents`:     Serialized session data
  *   * `hostname`:     Host name
  *   * `user_id`:      The used ID
  *
  * [!!] Note: Sessions can only be created using the [Session::instance] method.
  *
  * @param  array   $config  Configuration array [Optional]
  * @param  string  $id      Session id [Optional]
  */
 public function __construct(array $config = NULL, $id = NULL)
 {
     if (!isset($config['group'])) {
         // Use the default group
         $config['group'] = Client::$default;
     }
     // Create Mango instance
     $this->_db = Client::instance($config['group']);
     if (isset($config['collection'])) {
         // Save the collection name for later use
         $this->_collection = (string) $config['collection'];
     }
     if (isset($config['gc'])) {
         // Set the gc chance
         $this->_gc = (int) $config['gc'];
     }
     if (isset($config['columns'])) {
         // Overload column names
         $this->_columns = $config['columns'];
     }
     // Load the collection
     $this->_collection = $this->_db->selectCollection($this->_collection);
     parent::__construct($config, $id);
     if (mt_rand(0, $this->_gc) === $this->_gc) {
         // Run garbage collection
         // This will average out to run once every X requests
         $this->_gc();
     }
 }