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;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
use MongoDB\Driver\WriteConcern;
class Client
{
private $manager;
private $wc;
private $rp;
private $readPreference;
private $writeConcern;
/**
* Constructs new Client instance
* Constructs a new Client instance.
*
* This is the suggested main entry point using phongo.
* It acts as a bridge to access individual databases and collection tools
* which are provided in this namespace.
* 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.
*
* @param Manager $uri The MongoDB URI to connect to
* @param WriteConcern $options URI Options
* @param ReadPreference $driverOptions Driver specific options
* @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
*/
public function __construct($uri, $options, $driverOptions)
public function __construct($uri, array $options = array(), array $driverOptions = array())
{
$this->manager = new Manager($uri, $options, $driverOptions);
}
......@@ -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 WriteConcern $writeConcern Default Write Concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply
* @param string $databaseName Name of the database to select
* @param WriteConcern $writeConcern Default write concern 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 $collectionName The collection to select
* @param WriteConcern $writeConcern Default Write Concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply
* @param string $databaseName Name of the database containing the collection
* @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default write concern 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
/**
* Constructs new Collection instance
* Constructs new Collection instance.
*
* This is the suggested CRUD interface when using phongo.
* It implements the MongoDB CRUD specification which is an interface all MongoDB
* supported drivers follow.
* This class provides methods for collection-specific operations, such as
* CRUD (i.e. create, read, update, and delete) and index management.
*
* @param Manager $manager The phongo Manager instance
* @param string $ns Fully Qualified Namespace (dbname.collname)
* @param WriteConcern $wc The WriteConcern to apply to writes
* @param ReadPreference $rp The ReadPreferences to apply to reads
* @param Manager $manager Manager instance from the driver
* @param string $namespace Collection namespace (e.g. "db.collection")
* @param WriteConcern $writeConcern Default write concern to apply
* @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->ns = $ns;
$this->wc = $wc;
$this->rp = $rp;
$this->ns = $namespace;
$this->wc = $writeConcern;
$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;
use MongoDB\Collection;
use MongoDB\Driver\Manager;
use MongoDB\Driver\ReadPreference;
use MongoDB\Driver\Result;
use MongoDB\Driver\WriteConcern;
class Database
{
private $databaseName;
private $manager;
private $ns;
private $wc;
private $rp;
private $dbname;
private $readPreference;
private $writeConcern;
/**
* 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 string $dbname Fully Qualified database name
* @param WriteConcern $wc The WriteConcern to apply to writes
* @param ReadPreference $rp The ReadPreferences to apply to reads
* @param Manager $manager Manager instance from the driver
* @param string $databaseName Database name
* @param WriteConcern $writeConcern Default write concern to apply
* @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->dbname = $dbname;
$this->wc = $wc;
$this->rp = $rp;
$this->databaseName = $databaseName;
$this->writeConcern = $writeConcern;
$this->readPreference = $readPreference;
}
/**
......@@ -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 WriteConcern $writeConcern Default Write Concern to apply
* @param ReadPreference $readPreferences Default Read Preferences to apply
* @param string $collectionName Name of the collection to select
* @param WriteConcern $writeConcern Default write concern 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