Connection.php 4.03 KB
Newer Older
Jens Segers's avatar
Jens Segers committed
1 2
<?php namespace Jenssegers\Mongodb;

Jens Segers's avatar
Jens Segers committed
3
use Jenssegers\Mongodb\Builder as QueryBuilder;
4 5 6
use MongoClient;

class Connection extends \Illuminate\Database\Connection {
Jens Segers's avatar
Jens Segers committed
7

Jens Segers's avatar
Jens Segers committed
8
    /**
Jens Segers's avatar
Jens Segers committed
9 10 11 12 13 14 15 16 17 18 19 20 21
     * The MongoDB database handler.
     *
     * @var resource
     */
    protected $db;

    /**
     * The MongoClient connection handler.
     *
     * @var resource
     */
    protected $connection;

Jens Segers's avatar
Jens Segers committed
22 23 24 25 26 27 28 29 30 31
    /**
     * Create a new database connection instance.
     *
     * @param  array   $config
     * @return void
     */
    public function __construct(array $config)
    {
        $this->config = $config;

32 33 34 35
        // Build the connection string
        $dsn = $this->getDsn($config);

        // You can pass options directly to the MogoClient constructor
Jens Segers's avatar
Jens Segers committed
36 37
        $options = array_get($config, 'options', array());

38 39
        // Create the connection
        $this->connection = $this->createConnection($dsn, $config, $options);
Jens Segers's avatar
Jens Segers committed
40 41

        // Select database
42
        $this->db = $this->connection->{$config['database']};
Jens Segers's avatar
Jens Segers committed
43 44
    }

Jens Segers's avatar
Jens Segers committed
45
    /**
Jens Segers's avatar
Jens Segers committed
46
     * Begin a fluent query against a database collection.
Jens Segers's avatar
Jens Segers committed
47 48 49 50 51 52 53 54 55 56 57 58
     *
     * @param  string  $collection
     * @return QueryBuilder
     */
    public function collection($collection)
    {
        $query = new QueryBuilder($this);

        return $query->from($collection);
    }

    /**
Jens Segers's avatar
Jens Segers committed
59
     * Begin a fluent query against a database collection.
Jens Segers's avatar
Jens Segers committed
60 61 62 63 64 65 66 67 68
     *
     * @param  string  $table
     * @return QueryBuilder
     */
    public function table($table)
    {
        return $this->collection($table);
    }

Jens Segers's avatar
Jens Segers committed
69
    /**
70
     * Get a MongoDB collection.
Jens Segers's avatar
Jens Segers committed
71 72
     *
     * @param  string   $name
Jens Segers's avatar
Jens Segers committed
73
     * @return MongoDB
Jens Segers's avatar
Jens Segers committed
74
     */
Jens Segers's avatar
Jens Segers committed
75 76
    public function getCollection($name)
    {
Jens Segers's avatar
Jens Segers committed
77 78 79
        return $this->db->{$name};
    }

Jens Segers's avatar
Jens Segers committed
80
    /**
81
     * Get the MongoDB database object.
Jens Segers's avatar
Jens Segers committed
82 83 84
     *
     * @return  MongoDB
     */
85
    public function getMongoDB()
Jens Segers's avatar
Jens Segers committed
86
    {
Jens Segers's avatar
Jens Segers committed
87 88 89
        return $this->db;
    }

90 91 92 93 94 95 96 97 98 99
    /**
     * return MongoClient object
     *
     * @return MongoClient
     */
    public function getMongoClient()
    {
        return $this->connection;
    }

100 101 102 103 104 105 106 107 108 109
    /**
     * Create a new MongoClient connection.
     *
     * @param  string  $dsn
     * @param  array   $config
     * @param  array   $options
     * @return MongoClient
     */
    protected function createConnection($dsn, array $config, array $options)
    {
Jens Segers's avatar
Jens Segers committed
110
        // Add credentials as options, this makes sure the connection will not fail if
111 112 113 114 115 116 117 118 119 120 121 122 123 124
        // the username or password contains strange characters.
        if (isset($config['username']) && $config['username'])
        {
            $options['username'] = $config['username'];
        }

        if (isset($config['password']) && $config['password'])
        {
            $options['password'] = $config['password'];
        }

        return new MongoClient($dsn, $options);
    }

Jens Segers's avatar
Jens Segers committed
125 126 127 128 129 130 131 132 133 134 135 136 137
    /**
     * Create a DSN string from a configuration.
     *
     * @param  array   $config
     * @return string
     */
    protected function getDsn(array $config)
    {
        // First we will create the basic DSN setup as well as the port if it is in
        // in the configuration options. This will give us the basic DSN we will
        // need to establish the MongoClient and return them back for use.
        extract($config);

138 139
        // Treat host option as array of hosts
        $hosts = is_array($config['host']) ? $config['host'] : array($config['host']);
Jens Segers's avatar
Jens Segers committed
140

Jens Segers's avatar
Jens Segers committed
141
        // Add ports to hosts
142
        foreach ($hosts as &$host)
Jens Segers's avatar
Jens Segers committed
143
        {
144 145 146 147
            if (isset($config['port']))
            {
                $host = "{$host}:{$port}";
            }
Jens Segers's avatar
Jens Segers committed
148 149
        }

150 151 152
        // The database name needs to be in the connection string, otherwise it will 
        // authenticate to the admin database, which may result in permission errors.
        return "mongodb://" . implode(',', $hosts) . "/{$database}";
Jens Segers's avatar
Jens Segers committed
153
    }
Jens Segers's avatar
Jens Segers committed
154

Jens Segers's avatar
Jens Segers committed
155 156 157 158 159 160 161 162 163
    /**
     * Dynamically pass methods to the connection.
     *
     * @param  string  $method
     * @param  array   $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
164
        return call_user_func_array(array($this->db, $method), $parameters);
Jens Segers's avatar
Jens Segers committed
165 166
    }

167
}