Initial Version
This commit is contained in:
76
core/lib/Db/Client.php
Normal file
76
core/lib/Db/Client.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace KTXC\Db;
|
||||
|
||||
use MongoDB\Client as MongoClient;
|
||||
|
||||
/**
|
||||
* Wrapper for MongoDB\Client
|
||||
* Provides abstraction layer for MongoDB client operations
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
private MongoClient $client;
|
||||
|
||||
/**
|
||||
* Create a new MongoDB client
|
||||
*
|
||||
* @param string $uri Connection URI
|
||||
* @param array $uriOptions URI options
|
||||
* @param array $driverOptions Driver options
|
||||
*/
|
||||
public function __construct(string $uri = 'mongodb://localhost:27017', array $uriOptions = [], array $driverOptions = [])
|
||||
{
|
||||
$this->client = new MongoClient($uri, $uriOptions, $driverOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a database
|
||||
*
|
||||
* @param string $databaseName Database name
|
||||
* @param array $options Database options
|
||||
* @return Database
|
||||
*/
|
||||
public function selectDatabase(string $databaseName, array $options = []): Database
|
||||
{
|
||||
$mongoDatabase = $this->client->selectDatabase($databaseName, $options);
|
||||
return new Database($mongoDatabase);
|
||||
}
|
||||
|
||||
/**
|
||||
* List databases
|
||||
*/
|
||||
public function listDatabases(array $options = []): array
|
||||
{
|
||||
$databases = [];
|
||||
foreach ($this->client->listDatabases($options) as $databaseInfo) {
|
||||
$databases[] = $databaseInfo;
|
||||
}
|
||||
return $databases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop a database
|
||||
*/
|
||||
public function dropDatabase(string $databaseName, array $options = []): array|object|null
|
||||
{
|
||||
return $this->client->dropDatabase($databaseName, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the underlying MongoDB Client
|
||||
* Use sparingly - prefer using wrapper methods
|
||||
*/
|
||||
public function getMongoClient(): MongoClient
|
||||
{
|
||||
return $this->client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to access database as property
|
||||
*/
|
||||
public function __get(string $databaseName): Database
|
||||
{
|
||||
return $this->selectDatabase($databaseName);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user