Commit b85b91fb authored by Jeremy Mikola's avatar Jeremy Mikola

PHPLIB-73: Inherit default write concern and read preferences

Inheriting from the Client's URI options is not implemented, as it depends on PHPC-196.

This commit also revises the documentation for selectDatabase(), selectCollection(), and the core class constructors.
parent 24d34dcc
...@@ -5,27 +5,29 @@ namespace MongoDB; ...@@ -5,27 +5,29 @@ namespace MongoDB;
use MongoDB\Collection; use MongoDB\Collection;
use MongoDB\Database; use MongoDB\Database;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result; use MongoDB\Driver\Result;
use MongoDB\Driver\WriteConcern;
class Client class Client
{ {
private $manager; private $manager;
private $wc; private $readPreference;
private $rp; private $writeConcern;
/** /**
* Constructs new Client instance * Constructs a new Client instance.
* *
* This is the suggested main entry point using phongo. * This is the preferred class for connecting to a MongoDB server or
* It acts as a bridge to access individual databases and collection tools * cluster of servers. It serves as a gateway for accessing individual
* which are provided in this namespace. * databases and collections.
* *
* @param Manager $uri The MongoDB URI to connect to * @see http://docs.mongodb.org/manual/reference/connection-string/
* @param WriteConcern $options URI Options * @param string $uri MongoDB connection string
* @param ReadPreference $driverOptions Driver specific options * @param array $options Additional connection string options
* @param array $driverOptions Driver-specific options
*/ */
public function __construct($uri, $options, $driverOptions) public function __construct($uri, array $options = array(), array $driverOptions = array())
{ {
$this->manager = new Manager($uri, $options, $driverOptions); $this->manager = new Manager($uri, $options, $driverOptions);
} }
...@@ -42,33 +44,44 @@ class Client ...@@ -42,33 +44,44 @@ class Client
} }
/** /**
* Select a database * Select a database.
* *
* It acts as a bridge to access specific database commands * If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
* *
* @param string $databaseName The database to select * @param string $databaseName Name of the database to select
* @param WriteConcern $writeConcern Default Write Concern to apply * @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply * @param ReadPreference $readPreference Default read preference to apply
* @return Database
*/ */
public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null) public function selectDatabase($databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
return new Database($this->manager, $databaseName, $writeConcern, $readPreferences); // TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;
return new Database($this->manager, $databaseName, $writeConcern, $readPreference);
} }
/** /**
* Select a specific collection in a database * Select a collection.
* *
* It acts as a bridge to access specific collection commands * If a write concern or read preference is not specified, the write concern
* or read preference of the Client will be applied, respectively.
* *
* @param string $databaseName The database where the $collectionName exists * @param string $databaseName Name of the database containing the collection
* @param string $collectionName The collection to select * @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default Write Concern to apply * @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply * @param ReadPreference $readPreference Default read preference to apply
* @return Collection
*/ */
public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null) public function selectCollection($databaseName, $collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
return new Collection($this->manager, "{$databaseName}.{$collectionName}", $writeConcern, $readPreferences); $namespace = $databaseName . '.' . $collectionName;
} // TODO: inherit from Manager options once PHPC-196 is implemented
$writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
}
} }
...@@ -43,25 +43,24 @@ class Collection ...@@ -43,25 +43,24 @@ class Collection
/** /**
* Constructs new Collection instance * Constructs new Collection instance.
* *
* This is the suggested CRUD interface when using phongo. * This class provides methods for collection-specific operations, such as
* It implements the MongoDB CRUD specification which is an interface all MongoDB * CRUD (i.e. create, read, update, and delete) and index management.
* supported drivers follow.
* *
* @param Manager $manager The phongo Manager instance * @param Manager $manager Manager instance from the driver
* @param string $ns Fully Qualified Namespace (dbname.collname) * @param string $namespace Collection namespace (e.g. "db.collection")
* @param WriteConcern $wc The WriteConcern to apply to writes * @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $rp The ReadPreferences to apply to reads * @param ReadPreference $readPreference Default read preference to apply
*/ */
public function __construct(Manager $manager, $ns, WriteConcern $wc = null, ReadPreference $rp = null) public function __construct(Manager $manager, $namespace, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
$this->manager = $manager; $this->manager = $manager;
$this->ns = $ns; $this->ns = $namespace;
$this->wc = $wc; $this->wc = $writeConcern;
$this->rp = $rp; $this->rp = $readPreference;
list($this->dbname, $this->collname) = explode(".", $ns, 2); list($this->dbname, $this->collname) = explode(".", $namespace, 2);
} }
/** /**
......
...@@ -4,33 +4,34 @@ namespace MongoDB; ...@@ -4,33 +4,34 @@ namespace MongoDB;
use MongoDB\Collection; use MongoDB\Collection;
use MongoDB\Driver\Manager; use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result; use MongoDB\Driver\Result;
use MongoDB\Driver\WriteConcern;
class Database class Database
{ {
private $databaseName;
private $manager; private $manager;
private $ns; private $readPreference;
private $wc; private $writeConcern;
private $rp;
private $dbname;
/** /**
* Constructs new Database instance * Constructs new Database instance.
* *
* It acts as a bridge for database specific operations. * This class provides methods for database-specific operations and serves
* as a gateway for accessing collections.
* *
* @param Manager $manager The phongo Manager instance * @param Manager $manager Manager instance from the driver
* @param string $dbname Fully Qualified database name * @param string $databaseName Database name
* @param WriteConcern $wc The WriteConcern to apply to writes * @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $rp The ReadPreferences to apply to reads * @param ReadPreference $readPreference Default read preference to apply
*/ */
public function __construct(Manager $manager, $databaseName, WriteConcern $wc = null, ReadPreference $rp = null) public function __construct(Manager $manager, $databaseName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
$this->manager = $manager; $this->manager = $manager;
$this->dbname = $dbname; $this->databaseName = $databaseName;
$this->wc = $wc; $this->writeConcern = $writeConcern;
$this->rp = $rp; $this->readPreference = $readPreference;
} }
/** /**
...@@ -81,19 +82,22 @@ class Database ...@@ -81,19 +82,22 @@ class Database
} }
/** /**
* Select a specific collection in this database * Select a collection within this database.
* *
* It acts as a bridge to access specific collection commands * If a write concern or read preference is not specified, the write concern
* or read preference of the Database will be applied, respectively.
* *
* @param string $collectionName The collection to select * @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default Write Concern to apply * @param WriteConcern $writeConcern Default write concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply * @param ReadPreference $readPreference Default read preference to apply
* @return Collection
*/ */
public function selectCollection($collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreferences = null) public function selectCollection($collectionName, WriteConcern $writeConcern = null, ReadPreference $readPreference = null)
{ {
return new Collection($this->manager, "{$this->dbname}.{$collectionName}", $writeConcern, $readPreferences); $namespace = $this->databaseName . '.' . $collectionName;
} $writeConcern = $writeConcern ?: $this->writeConcern;
$readPreference = $readPreference ?: $this->readPreference;
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment