Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
}
},
"require": {
"php": ">=7.1",
"php": ">=7.2.0",
"psr/log": "~1.0",
"ext-mongodb": "^1.2.0",
"psr/simple-cache": "^1.0",
"jmikola/geojson": "~1.0",
"symfony/event-dispatcher": ">=2.0 <5.0",
"alcaeus/mongo-php-adapter": "^1.1"
"alcaeus/mongo-php-adapter": "^1.1",
"psr/event-dispatcher": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^7.0",
Expand All @@ -39,6 +39,7 @@
"sokil/php-mongo-bundle": "Symfony Bundle",
"phpmongokit/laravel-mongo-pdm": "PHPMongo ODM Adapter for Laravel",
"sokil/php-mongo-yii": "Yii Framework component",
"symfony/event-dispatcher": "Add support for event dispatcher",
"phpmongokit/yii2-mongo-odm": "Yii2 Framework component"
},
"scripts": {
Expand Down
100 changes: 76 additions & 24 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

namespace Sokil\Mongo;

use \Psr\EventDispatcher\EventDispatcherInterface;
use \Psr\Log\LoggerInterface;
use \Sokil\Mongo\EventFactory\EventFactoryInterface;

/**
* Connection manager and factory to get database and collection instances.
Expand All @@ -35,7 +37,7 @@ class Client
* @var array
*/
private $connectOptions = array();

/**
*
* @var \MongoClient
Expand All @@ -46,7 +48,7 @@ class Client
* @var array
*/
private $databasePool = array();

/**
* @var array Database to class mapping
*/
Expand All @@ -57,6 +59,16 @@ class Client
*/
private $logger;

/**
* @var EventDispatcherInterface|null
*/
private $eventDispatcher;

/**
* @var EventFactoryInterface|null
*/
private $eventFactory;

/**
* @var string
*/
Expand Down Expand Up @@ -86,7 +98,7 @@ public function __construct(
if (!empty($dsn)) {
$this->setDsn($dsn);
}

if (!empty($connectOptions)) {
$this->setConnectOptions($connectOptions);
}
Expand All @@ -101,7 +113,7 @@ public static function isEmulationMode() : bool
{
return class_exists('\MongoDB\Driver\Manager');
}

/**
* Set credentials to auth on db, specified in connect options or dsn.
* If not specified - auth on admin db
Expand All @@ -115,15 +127,15 @@ public function setCredentials(string $username, string $password) : Client
{
$this->connectOptions['username'] = $username;
$this->connectOptions['password'] = $password;

return $this;
}

public function __get($name)
{
return $this->getDatabase($name);
}

/**
*
* @return string Version of PHP driver
Expand All @@ -146,22 +158,22 @@ public function getDbVersion() : string
$buildInfo = $this
->getDatabase('admin')
->executeCommand(array('buildinfo' => 1));

$this->dbVersion = $buildInfo['version'];
return $this->dbVersion;
}

public function setDsn($dsn) : Client
{
$this->dsn = $dsn;
return $this;
}

public function getDsn() : string
{
return $this->dsn;
}

/**
* Set connect options
*
Expand All @@ -180,7 +192,7 @@ public function getConnectOptions() : array
{
return $this->connectOptions;
}

/**
* Set mongo's client
*
Expand All @@ -194,7 +206,7 @@ public function setMongoClient(\MongoClient $client) : Client

return $this;
}

/**
* Get mongo connection instance
*
Expand All @@ -210,10 +222,10 @@ public function getMongoClient()
$this->dsn,
$this->connectOptions
);

return $this->mongoClient;
}

/**
* Get list of all active connections through this client
*
Expand All @@ -223,7 +235,7 @@ public function getConnections()
{
return $this->mongoClient->getConnections();
}

/**
* Map database and collection name to class.
*
Expand All @@ -240,7 +252,7 @@ public function getConnections()
public function map(array $mapping) : Client
{
$this->mapping = $mapping;

return $this;
}

Expand Down Expand Up @@ -269,10 +281,10 @@ public function getDatabase($name = null) : Database
// configure db
$this->databasePool[$name] = $database;
}

return $this->databasePool[$name];
}

/**
* Select database
*
Expand Down Expand Up @@ -301,7 +313,7 @@ public function getCurrentDatabaseName() : string

return $this->currentDatabaseName;
}

/**
* Get collection from previously selected database by self::useDatabase()
*
Expand Down Expand Up @@ -395,7 +407,7 @@ public function setLogger(LoggerInterface $logger) : Client

return $this;
}

/**
*
* @return \Psr\Log\LoggerInterface
Expand Down Expand Up @@ -437,6 +449,46 @@ public function debug($enabled = true)
return $this;
}

/**
* @param \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher
* @return Client
*/
public function setEventDispatcher(\Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher) : Client
{
$this->eventDispatcher = $eventDispatcher;

return $this;
}

/**
*
* @return \Psr\EventDispatcher\EventDispatcherInterface|NULL
*/
public function getEventDispatcher()
{
return $this->eventDispatcher;
}

/**
* @param \Sokil\Mongo\EventFactory\EventFactoryInterface $eventDispatcher
* @return Client
*/
public function setEventFactory(EventFactoryInterface $eventFactory) : Client
{
$this->eventFactory = $eventFactory;

return $this;
}

/**
*
* @return \Sokil\Mongo\EventFactory\EventFactoryInterface
*/
public function getEventFactory()
{
return $this->eventFactory;
}

/**
* Check state of debug mode
*/
Expand All @@ -460,10 +512,10 @@ public function setWriteConcern($w, int $timeout = 10000)
if (!$this->getMongoClient()->setWriteConcern($w, $timeout)) {
throw new Exception('Error setting write concern');
}

return $this;
}

/**
* Define unacknowledged write concern on whole requests
*
Expand All @@ -475,7 +527,7 @@ public function setUnacknowledgedWriteConcern($timeout = 10000)
$this->setWriteConcern(0, (int) $timeout);
return $this;
}

/**
* Define majority write concern on whole requests
*
Expand Down
Loading