Client.php 5.3 KB
Newer Older
1 2 3 4
<?php

namespace MongoDB;

5
use MongoDB\Driver\Command;
6
use MongoDB\Driver\Cursor;
7
use MongoDB\Driver\Manager;
8 9
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\WriteConcern;
10
use MongoDB\Exception\InvalidArgumentException;
11
use MongoDB\Model\DatabaseInfoIterator;
12
use MongoDB\Operation\DropDatabase;
13
use MongoDB\Operation\ListDatabases;
14 15 16

class Client
{
17 18 19 20 21 22
    private static $defaultTypeMap = [
        'array' => 'MongoDB\Model\BSONArray',
        'document' => 'MongoDB\Model\BSONDocument',
        'root' => 'MongoDB\Model\BSONDocument',
    ];

23
    private $manager;
24
    private $uri;
25
    private $typeMap;
26 27

    /**
28
     * Constructs a new Client instance.
29
     *
30 31 32
     * 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.
33
     *
34 35 36 37 38 39
     * Supported driver-specific options:
     *
     *  * typeMap (array): Default type map for cursors and BSON documents.
     *
     * Other options are documented in MongoDB\Driver\Manager::__construct().
     *
40
     * @see http://docs.mongodb.org/manual/reference/connection-string/
41 42
     * @see http://php.net/manual/en/mongodb-driver-manager.construct.php
     * @see http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps
43
     * @param string $uri           MongoDB connection string
44
     * @param array  $uriOptions    Additional connection string options
45
     * @param array  $driverOptions Driver-specific options
46
     * @throws InvalidArgumentException
47
     */
48
    public function __construct($uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
49
    {
50
        $driverOptions += ['typeMap' => self::$defaultTypeMap];
51

52
        if (isset($driverOptions['typeMap']) && ! is_array($driverOptions['typeMap'])) {
53
            throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
54 55 56
        }

        $this->manager = new Manager($uri, $uriOptions, $driverOptions);
57
        $this->uri = (string) $uri;
58
        $this->typeMap = isset($driverOptions['typeMap']) ? $driverOptions['typeMap'] : null;
59 60
    }

61 62 63 64 65 66 67 68 69 70 71
    /**
     * Return internal properties for debugging purposes.
     *
     * @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
     * @param array
     */
    public function __debugInfo()
    {
        return [
            'manager' => $this->manager,
            'uri' => $this->uri,
72
            'typeMap' => $this->typeMap,
73 74 75
        ];
    }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
    /**
     * Select a database.
     *
     * Note: collections whose names contain special characters (e.g. "-") may
     * 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);
    }

93 94 95 96 97 98 99 100
    /**
     * Return the connection string (i.e. URI).
     *
     * @param string
     */
    public function __toString()
    {
        return $this->uri;
101 102
    }

103 104 105
    /**
     * Drop a database.
     *
106 107 108 109
     * @see DropDatabase::__construct() for supported options
     * @param string $databaseName Database name
     * @param array  $options      Additional options
     * @return array|object Command result document
110
     */
111
    public function dropDatabase($databaseName, array $options = [])
112
    {
113 114 115 116 117
        if ( ! isset($options['typeMap'])) {
            $options['typeMap'] = $this->typeMap;
        }

        $operation = new DropDatabase($databaseName, $options);
118
        $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
119

120
        return $operation->execute($server);
121 122
    }

123 124 125
    /**
     * List databases.
     *
126
     * @see ListDatabases::__construct() for supported options
127
     * @return DatabaseInfoIterator
128
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
129
    public function listDatabases(array $options = [])
130
    {
131 132
        $operation = new ListDatabases($options);
        $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
133

134
        return $operation->execute($server);
135 136
    }

137
    /**
138
     * Select a collection.
139
     *
140
     * @see Collection::__construct() for supported options
141 142 143
     * @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
144
     * @return Collection
145
     */
146
    public function selectCollection($databaseName, $collectionName, array $options = [])
147
    {
148 149
        $options += ['typeMap' => $this->typeMap];

150
        return new Collection($this->manager, $databaseName, $collectionName, $options);
151 152 153
    }

    /**
154
     * Select a database.
155
     *
156
     * @see Database::__construct() for supported options
157 158
     * @param string $databaseName Name of the database to select
     * @param array  $options      Database constructor options
159
     * @return Database
160
     */
161
    public function selectDatabase($databaseName, array $options = [])
162
    {
163 164
        $options += ['typeMap' => $this->typeMap];

165
        return new Database($this->manager, $databaseName, $options);
166
    }
167
}