Skip to content

Commit

Permalink
Issue thephpleague#29 [+] Added driver options constructor parameter,…
Browse files Browse the repository at this point in the history
… fixed PSR-2
  • Loading branch information
acim committed Oct 8, 2015
1 parent 780f47d commit 4ee06b0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
40 changes: 22 additions & 18 deletions src/League/Monga.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@
* @author Frank de Jonge
* @license MIT License
* @copyright 2011 - 2015 Frank de Jonge
* @link http://github.com/thephpleague/monga
* @see http://github.com/thephpleague/monga
*/

namespace League;

use League\Monga\Connection;
use MongoBinData;
use MongoCode;
use MongoConnectionException;
use MongoDate;
use MongoDB;
use MongoId;
use MongoRegex;

/**
* Class Monga
*/
class Monga
{
/**
* Returns a MongoBinData object
*
* @param string $data data
* @param int $type data type
* @param int $type data type
*
* @return object MongoBinData
* @return MongoBinData
*/
public static function data($data, $type = null)
{
Expand All @@ -42,7 +45,7 @@ public static function data($data, $type = null)
*
* @param string $id id string
*
* @return object MongoId
* @return MongoId
*/
public static function id($id)
{
Expand All @@ -52,23 +55,23 @@ public static function id($id)
/**
* Create a MongoCode object
*
* @param string $code javascript string
* @param array $scope function scope
* @param string $code javascript string
* @param array $scope function scope
*
* @return object MongoCode
* @return MongoCode
*/
public static function code($code, $scope = [])
public static function code($code, array $scope = [])
{
return new MongoCode($code, $scope);
}

/**
* Create a MongoDate object
*
* @param int $sec timestamp
* @param int $sec timestamp
* @param int $usec
*
* @return object MongoDate
* @return MongoDate
*/
public static function date($sec = null, $usec = 0)
{
Expand All @@ -82,7 +85,7 @@ public static function date($sec = null, $usec = 0)
*
* @param string $regex regex
*
* @return object MongoRegex
* @return MongoRegex
*/
public static function regex($regex)
{
Expand All @@ -92,13 +95,14 @@ public static function regex($regex)
/**
* Create a Monga\Connection object
*
* @param string $server server dns
* @param array $options connection options
*
* @return object Monga\Connection
* @param string $server server dns
* @param array $options connection options
* @param array $driverOptions [optional] driver options
* @throws MongoConnectionException
* @return Connection
*/
public static function connection($server = null, array $options = [])
public static function connection($server = null, array $options = [], array $driverOptions = [])
{
return new Connection($server, $options);
return new Connection($server, $options, $driverOptions);
}
}
35 changes: 20 additions & 15 deletions src/League/Monga/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
* @author Frank de Jonge
* @license MIT License
* @copyright 2011 - 2015 Frank de Jonge
* @link http://github.com/thephpleague/monga
* @see http://github.com/thephpleague/monga
*/

namespace League\Monga;

use MongoClient;
use MongoConnectionException;
use MongoDB;

class Connection
{
/**
* @var Mongo $connection MongoDB Connection instance
* @var MongoDB $connection MongoDB Connection instance
*/
protected $connection;

Expand All @@ -29,10 +31,12 @@ class Connection
/**
* Establishes a MongoDB connection
*
* @param string $server mongo dns
* @param array $options connection options
* @param string $server mongo dns
* @param array $options [optional] connection options
* @param array $driverOptions [optional] driver options
* @throws MongoConnectionException
*/
public function __construct($server = null, array $options = [])
public function __construct($server = null, array $options = [], array $driverOptions = [])
{
if ($server instanceof MongoClient) {
$this->connection = $server;
Expand All @@ -43,20 +47,20 @@ public function __construct($server = null, array $options = [])
}

// Mimic the default mongo connect settings.
if (! isset($options['connect'])) {
if (!isset($options['connect'])) {
$options['connect'] = true;
}

$this->connection = new MongoClient($server ?: 'mongodb://localhost:27017', $options);
$this->connection = new MongoClient($server ?: 'mongodb://localhost:27017', $options, $driverOptions);
}
}

/**
* Connection injector
*
* @param object $connection MongoClient instance
* @param MongoClient $connection MongoClient instance
*
* @return object $this
* @return $this
*/
public function setConnection(MongoClient $connection)
{
Expand All @@ -68,7 +72,7 @@ public function setConnection(MongoClient $connection)
/**
* Retrieve the MongoConnection.
*
* @return object Mongo instance
* @return MongoClient
*/
public function getConnection()
{
Expand All @@ -79,6 +83,7 @@ public function getConnection()
* Connect to the database.
*
* @return boolean connection result
* @throws MongoConnectionException
*/
public function connect()
{
Expand Down Expand Up @@ -128,16 +133,16 @@ public function dropDatabase($database)
{
$result = $this->connection->{$database}->command(['dropDatabase' => 1]);

return (bool) $result['ok'];
return (bool)$result['ok'];
}

/**
* Retrieve a database object from a connection
*
* @param string $database database name
* @param boolean $wrap whether to wrap in a Database object
* @param string $database database name
* @param boolean $wrap whether to wrap in a Database object
*
* @return object MongoDB or Monga\Database instance
* @return Database|MongoDB
*/
public function database($database, $wrap = true)
{
Expand All @@ -155,7 +160,7 @@ public function database($database, $wrap = true)
*/
public function hasDatabase($name)
{
return in_array($name, $this->listDatabases(false));
return in_array($name, $this->listDatabases(false), true);
}

/**
Expand Down

0 comments on commit 4ee06b0

Please sign in to comment.