Client.php 3.6 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\Model\DatabaseInfoIterator;
11
use MongoDB\Operation\DropDatabase;
12
use MongoDB\Operation\ListDatabases;
13 14 15 16 17 18

class Client
{
    private $manager;

    /**
19
     * Constructs a new Client instance.
20
     *
21 22 23
     * 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.
24
     *
25 26 27 28
     * @see http://docs.mongodb.org/manual/reference/connection-string/
     * @param string $uri           MongoDB connection string
     * @param array  $options       Additional connection string options
     * @param array  $driverOptions Driver-specific options
29
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
30
    public function __construct($uri, array $options = [], array $driverOptions = [])
31 32 33 34
    {
        $this->manager = new Manager($uri, $options, $driverOptions);
    }

35 36 37 38
    /**
     * Drop a database.
     *
     * @param string $databaseName
39
     * @return object Command result document
40 41 42
     */
    public function dropDatabase($databaseName)
    {
43 44
        $operation = new DropDatabase($databaseName);
        $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
45

46
        return $operation->execute($server);
47 48
    }

49 50 51
    /**
     * List databases.
     *
52
     * @see ListDatabases::__construct() for supported options
53
     * @return DatabaseInfoIterator
54
     */
Jeremy Mikola's avatar
Jeremy Mikola committed
55
    public function listDatabases(array $options = [])
56
    {
57 58
        $operation = new ListDatabases($options);
        $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
59

60
        return $operation->execute($server);
61 62
    }

63
    /**
64
     * Select a collection.
65
     *
66 67
     * If a write concern or read preference is not specified, the write concern
     * or read preference of the Client will be applied, respectively.
68
     *
69 70
     * @param string         $databaseName   Name of the database containing the collection
     * @param string         $collectionName Name of the collection to select
71 72
     * @param WriteConcern   $writeConcern   Default write concern to apply
     * @param ReadPreference $readPreference Default read preference to apply
73
     * @return Collection
74
     */
75
    public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
76
    {
77
        $namespace = $databaseName . '.' . $collectionName;
78 79
        $writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
        $readPreference = $readPreference ?: $this->manager->getReadPreference();
80

81
        return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
82 83 84
    }

    /**
85
     * Select a database.
86
     *
87 88
     * If a write concern or read preference is not specified, the write concern
     * or read preference of the Client will be applied, respectively.
89
     *
90
     * @param string         $databaseName   Name of the database to select
91 92
     * @param WriteConcern   $writeConcern   Default write concern to apply
     * @param ReadPreference $readPreference Default read preference to apply
93
     * @return Database
94
     */
95
    public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
96
    {
97 98
        $writeConcern = $writeConcern ?: $this->manager->getWriteConcern();
        $readPreference = $readPreference ?: $this->manager->getReadPreference();
99

100
        return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
101
    }
102
}