Client.php 10.3 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2015-2017 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
17 18 19

namespace MongoDB;

20 21
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
22
use MongoDB\Driver\Manager;
23
use MongoDB\Driver\ReadConcern;
24
use MongoDB\Driver\ReadPreference;
25
use MongoDB\Driver\Session;
26
use MongoDB\Driver\WriteConcern;
27
use MongoDB\Exception\InvalidArgumentException;
28 29
use MongoDB\Exception\UnexpectedValueException;
use MongoDB\Exception\UnsupportedException;
30 31
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
32
use MongoDB\Model\DatabaseInfoIterator;
33
use MongoDB\Operation\DropDatabase;
34
use MongoDB\Operation\ListDatabases;
35
use MongoDB\Operation\Watch;
36
use function is_array;
37 38 39

class Client
{
40
    /** @var array */
41
    private static $defaultTypeMap = [
42 43 44
        'array' => BSONArray::class,
        'document' => BSONDocument::class,
        'root' => BSONDocument::class,
45
    ];
46 47

    /** @var integer */
48
    private static $wireVersionForReadConcern = 4;
49 50

    /** @var integer */
51
    private static $wireVersionForWritableCommandWriteConcern = 5;
52

53
    /** @var Manager */
54
    private $manager;
55 56

    /** @var ReadConcern */
57
    private $readConcern;
58 59

    /** @var ReadPreference */
60
    private $readPreference;
61 62

    /** @var string */
63
    private $uri;
64 65

    /** @var array */
66
    private $typeMap;
67 68

    /** @var WriteConcern */
69
    private $writeConcern;
70 71

    /**
72
     * Constructs a new Client instance.
73
     *
74 75 76
     * This is the preferred class for connecting to a MongoDB server or
     * cluster of servers. It serves as a gateway for accessing individual
     * databases and collections.
77
     *
78 79 80 81 82 83
     * Supported driver-specific options:
     *
     *  * typeMap (array): Default type map for cursors and BSON documents.
     *
     * Other options are documented in MongoDB\Driver\Manager::__construct().
     *
84
     * @see http://docs.mongodb.org/manual/reference/connection-string/
85 86
     * @see http://php.net/manual/en/mongodb-driver-manager.construct.php
     * @see http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
87
     * @param string $uri           MongoDB connection string
88
     * @param array  $uriOptions    Additional connection string options
89
     * @param array  $driverOptions Driver-specific options
90 91 92
     * @throws InvalidArgumentException for parameter/option parsing errors
     * @throws DriverInvalidArgumentException for parameter/option parsing errors in the driver
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
93
     */
94
    public function __construct($uri = 'mongodb://127.0.0.1/', array $uriOptions = [], array $driverOptions = [])
95
    {
96
        $driverOptions += ['typeMap' => self::$defaultTypeMap];
97

98
        if (! is_array($driverOptions['typeMap'])) {
99
            throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
100 101
        }

102
        $this->uri = (string) $uri;
103
        $this->typeMap = isset($driverOptions['typeMap']) ? $driverOptions['typeMap'] : null;
104 105 106 107

        unset($driverOptions['typeMap']);

        $this->manager = new Manager($uri, $uriOptions, $driverOptions);
108 109
        $this->readConcern = $this->manager->getReadConcern();
        $this->readPreference = $this->manager->getReadPreference();
110
        $this->writeConcern = $this->manager->getWriteConcern();
111 112
    }

113 114 115 116
    /**
     * Return internal properties for debugging purposes.
     *
     * @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
117
     * @return array
118 119 120 121 122 123
     */
    public function __debugInfo()
    {
        return [
            'manager' => $this->manager,
            'uri' => $this->uri,
124
            'typeMap' => $this->typeMap,
125
            'writeConcern' => $this->writeConcern,
126 127 128
        ];
    }

129 130 131
    /**
     * Select a database.
     *
132
     * Note: databases whose names contain special characters (e.g. "-") may
133 134 135 136 137 138 139 140 141 142 143 144 145
     * be selected with complex syntax (e.g. $client->{"that-database"}) or
     * {@link selectDatabase()}.
     *
     * @see http://php.net/oop5.overloading#object.get
     * @see http://php.net/types.string#language.types.string.parsing.complex
     * @param string $databaseName Name of the database to select
     * @return Database
     */
    public function __get($databaseName)
    {
        return $this->selectDatabase($databaseName);
    }

146 147 148
    /**
     * Return the connection string (i.e. URI).
     *
149
     * @return string
150 151 152 153
     */
    public function __toString()
    {
        return $this->uri;
154 155
    }

156 157 158
    /**
     * Drop a database.
     *
159 160 161 162
     * @see DropDatabase::__construct() for supported options
     * @param string $databaseName Database name
     * @param array  $options      Additional options
     * @return array|object Command result document
163 164 165
     * @throws UnsupportedException if options are unsupported on the selected server
     * @throws InvalidArgumentException for parameter/option parsing errors
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
166
     */
167
    public function dropDatabase($databaseName, array $options = [])
168
    {
169
        if (! isset($options['typeMap'])) {
170 171 172
            $options['typeMap'] = $this->typeMap;
        }

173
        $server = select_server($this->manager, $options);
174

175
        if (! isset($options['writeConcern']) && server_supports_feature($server, self::$wireVersionForWritableCommandWriteConcern) && ! is_in_transaction($options)) {
176 177 178 179 180
            $options['writeConcern'] = $this->writeConcern;
        }

        $operation = new DropDatabase($databaseName, $options);

181
        return $operation->execute($server);
182 183
    }

184 185 186 187 188 189 190 191 192 193
    /**
     * Return the Manager.
     *
     * @return Manager
     */
    public function getManager()
    {
        return $this->manager;
    }

194
    /**
195
     * Return the read concern for this client.
196
     *
197 198
     * @see http://php.net/manual/en/mongodb-driver-readconcern.isdefault.php
     * @return ReadConcern
199 200 201
     */
    public function getReadConcern()
    {
202
        return $this->readConcern;
203 204 205
    }

    /**
206
     * Return the read preference for this client.
207 208 209 210 211
     *
     * @return ReadPreference
     */
    public function getReadPreference()
    {
212
        return $this->readPreference;
213 214 215
    }

    /**
216
     * Return the type map for this client.
217 218 219 220 221 222 223 224 225
     *
     * @return array
     */
    public function getTypeMap()
    {
        return $this->typeMap;
    }

    /**
226
     * Return the write concern for this client.
227
     *
228 229
     * @see http://php.net/manual/en/mongodb-driver-writeconcern.isdefault.php
     * @return WriteConcern
230 231 232 233 234 235
     */
    public function getWriteConcern()
    {
        return $this->writeConcern;
    }

236 237 238
    /**
     * List databases.
     *
239
     * @see ListDatabases::__construct() for supported options
240
     * @param array $options
241
     * @return DatabaseInfoIterator
242 243 244
     * @throws UnexpectedValueException if the command response was malformed
     * @throws InvalidArgumentException for parameter/option parsing errors
     * @throws DriverRuntimeException for other driver errors (e.g. connection errors)
245
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
246
    public function listDatabases(array $options = [])
247
    {
248
        $operation = new ListDatabases($options);
249
        $server = select_server($this->manager, $options);
250

251
        return $operation->execute($server);
252 253
    }

254
    /**
255
     * Select a collection.
256
     *
257
     * @see Collection::__construct() for supported options
258 259 260
     * @param string $databaseName   Name of the database containing the collection
     * @param string $collectionName Name of the collection to select
     * @param array  $options        Collection constructor options
261
     * @return Collection
262
     * @throws InvalidArgumentException for parameter/option parsing errors
263
     */
264
    public function selectCollection($databaseName, $collectionName, array $options = [])
265
    {
266 267
        $options += ['typeMap' => $this->typeMap];

268
        return new Collection($this->manager, $databaseName, $collectionName, $options);
269 270 271
    }

    /**
272
     * Select a database.
273
     *
274
     * @see Database::__construct() for supported options
275 276
     * @param string $databaseName Name of the database to select
     * @param array  $options      Database constructor options
277
     * @return Database
278
     * @throws InvalidArgumentException for parameter/option parsing errors
279
     */
280
    public function selectDatabase($databaseName, array $options = [])
281
    {
282 283
        $options += ['typeMap' => $this->typeMap];

284
        return new Database($this->manager, $databaseName, $options);
285
    }
286 287 288 289 290

    /**
     * Start a new client session.
     *
     * @see http://php.net/manual/en/mongodb-driver-manager.startsession.php
291
     * @param array $options Session options
292
     * @return Session
293 294 295 296 297
     */
    public function startSession(array $options = [])
    {
        return $this->manager->startSession($options);
    }
298 299 300 301 302 303 304 305 306 307 308 309

    /**
     * Create a change stream for watching changes to the cluster.
     *
     * @see Watch::__construct() for supported options
     * @param array $pipeline List of pipeline operations
     * @param array $options  Command options
     * @return ChangeStream
     * @throws InvalidArgumentException for parameter/option parsing errors
     */
    public function watch(array $pipeline = [], array $options = [])
    {
310
        if (! isset($options['readPreference']) && ! is_in_transaction($options)) {
311 312 313
            $options['readPreference'] = $this->readPreference;
        }

314
        $server = select_server($this->manager, $options);
315

316
        if (! isset($options['readConcern']) && server_supports_feature($server, self::$wireVersionForReadConcern) && ! is_in_transaction($options)) {
317 318 319
            $options['readConcern'] = $this->readConcern;
        }

320
        if (! isset($options['typeMap'])) {
321 322 323 324 325 326 327
            $options['typeMap'] = $this->typeMap;
        }

        $operation = new Watch($this->manager, null, null, $pipeline, $options);

        return $operation->execute($server);
    }
328
}