MongoClient::__construct PHP Метод

__construct() публичный Метод

Creates a new database connection object
public __construct ( string $server = 'mongodb://localhost:27017', array $options = ['connect' => true] )
$server string - The server name. server should have the form: mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db
$options array - An array of options for the connection.
    public function __construct($server = 'mongodb://localhost:27017', array $options = ['connect' => true])
    {
        if (!$server || strpos($server, 'mongodb://') !== 0) {
            throw new MongoConnectionException('malformed uri: ' . $server);
        }
        $this->uri = $server;
        $uri = substr($server, 10);
        $serverPart = '';
        $nsPart = null;
        $optionsPart = '';
        $idx = strrpos($uri, '/');
        if ($idx === false) {
            if (strpos($uri, '?') !== false) {
                throw new MongoConnectionException('malformed uri: ' . $server);
            }
            $serverPart = $uri;
            $nsPart = null;
            $optionsPart = '';
        } else {
            $serverPart = substr($uri, 0, $idx);
            $nsPart = substr($uri, $idx + 1);
            $idx = strrpos($nsPart, '?');
            if ($idx !== false) {
                $optionsPart = substr($nsPart, $idx + 1);
                $nsPart = substr($nsPart, 0, $idx);
            } else {
                $optionsPart = '';
            }
        }
        // username,password,hosts
        $idx = strrpos($serverPart, '@');
        if ($idx !== false) {
            $authPart = substr($serverPart, 0, $idx);
            $serverPart = substr($serverPart, $idx + 1);
            $idx = strrpos($authPart, ':');
            if ($idx === false) {
                $this->username = urldecode($authPart);
                $this->password = '';
            } else {
                $this->username = urldecode(substr($authPart, 0, $idx));
                $this->password = urldecode(substr($authPart, $idx + 1));
            }
        }
        if (strlen($serverPart) === 0) {
            throw new MongoConnectionException('malformed uri: ' . $server);
        }
        foreach (explode(',', $serverPart) as $host_str) {
            $host_parts = $this->parseHostString($host_str);
            $this->hosts[$host_parts['hash']] = $host_parts;
        }
        if ($nsPart != null && strlen($nsPart) != 0) {
            // database
            $this->database = $nsPart;
        }
        $uri_options = [];
        $split_options = preg_split('/[&;]+/', $optionsPart);
        foreach ($split_options as $part) {
            $idx = strrpos($part, '=');
            if ($idx !== false) {
                $key = substr($part, 0, $idx);
                $value = substr($part, $idx + 1);
                $uri_options[$key] = $value;
            }
        }
        if (!$options) {
            $options = ['connect' => true];
        }
        $this->options = array_replace($uri_options, $options);
        // handle legacy settings
        if (array_key_exists('timeout', $this->options) && !array_key_exists('connectTimeoutMS', $this->options)) {
            $this->options['connectTimeoutMS'] = $this->options['timeout'];
            unset($this->options['timeout']);
        }
        if (array_key_exists('wtimeout', $this->options) && !array_key_exists('wtimeoutms', $this->options)) {
            $this->options['wtimeoutms'] = $this->options['wtimeout'];
            unset($this->options['wtimeout']);
        }
        if (isset($this->options['connectTimeoutMS'])) {
            $this->connectTimeoutMS = $this->options['connectTimeoutMS'];
        } else {
            $this->connectTimeoutMS = self::DEFAULT_CONNECT_TIMEOUT_MS;
        }
        if (array_key_exists('username', $this->options)) {
            $this->username = $this->options['username'];
        }
        if (array_key_exists('password', $this->options)) {
            $this->password = $this->options['password'];
        }
        if (array_key_exists('db', $this->options)) {
            $this->database = $this->options['db'];
        }
        if ($this->database == null && $this->username != null) {
            $this->database = self::DEFAULT_DATABASE;
        }
        if (isset($this->options['replicaSet'])) {
            $this->replSet = $this->options['replicaSet'];
        }
        if (isset($this->options['readPreference'])) {
            $tags = isset($this->options['readPreferenceTags']) ? $this->options['readPreferenceTags'] : null;
            $this->setReadPreference($this->options['readPreference'], $tags);
        }
        if (!isset($this->options['connect']) || $this->options['connect'] === true) {
            $this->connect();
        }
    }

Usage Example

Пример #1
0
 public function __construct($config = array(), $options = array('connect' => true))
 {
     try {
         $this->_config($config);
         // настраиваем тестовое окружение
         $connection = defined('TESTING_IS_GOING_ON') && TESTING_IS_GOING_ON && !empty($this->_config['test']) ? $this->_config['test'] : $this->_config['server'];
         $server = '';
         if (!empty($connection['user'])) {
             $server .= $connection['user'];
         }
         if (!empty($connection['password'])) {
             $server .= ":{$connection['password']}";
         }
         if (!empty($server)) {
             $server .= "@";
         }
         $server .= "{$connection['host']}:{$connection['port']}";
         $server = "mongodb://{$server}";
         // соединяемся
         parent::__construct($server, $options);
         if (!empty($connection['db'])) {
             $this->_Db = new \MongoDb($this, $connection['db']);
             if (!empty($this->_config['collection'])) {
                 $this->_Collection = new \MongoCollection($this->_Db, $this->_config['collection']);
             }
         }
     } catch (\MongoException $e) {
         throw new Exception($e->getMessage());
     }
 }
All Usage Examples Of MongoClient::__construct