Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a80c70

Browse files
authored
PHPORM-278 Introduce Connection::getDatabase() and getClient (#3289)
Deprecate getMongoDB and get MongoClient Replace selectDatabase with getDatabase
1 parent d49c801 commit 0a80c70

File tree

6 files changed

+96
-28
lines changed

6 files changed

+96
-28
lines changed

‎src/Concerns/ManagesTransactions.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212

1313
use function MongoDB\with_transaction;
1414

15-
/** @see https://docs.mongodb.com/manual/core/transactions/ */
15+
/**
16+
* @internal
17+
*
18+
* @see https://docs.mongodb.com/manual/core/transactions/
19+
*/
1620
trait ManagesTransactions
1721
{
1822
protected ?Session $session = null;
1923

2024
protected $transactions = 0;
2125

22-
/** @return Client */
23-
abstract public function getMongoClient();
26+
abstract public function getClient(): ?Client;
2427

2528
public function getSession(): ?Session
2629
{
@@ -30,7 +33,7 @@ public function getSession(): ?Session
3033
private function getSessionOrCreate(): Session
3134
{
3235
if ($this->session === null) {
33-
$this->session = $this->getMongoClient()->startSession();
36+
$this->session = $this->getClient()->startSession();
3437
}
3538

3639
return $this->session;

‎src/Connection.php‎

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
use function implode;
2323
use function is_array;
2424
use function preg_match;
25+
use function sprintf;
2526
use function str_contains;
27+
use function trigger_error;
2628

29+
use const E_USER_DEPRECATED;
2730
use const FILTER_FLAG_IPV6;
2831
use const FILTER_VALIDATE_IP;
2932

@@ -65,9 +68,10 @@ public function __construct(array $config)
6568

6669
// Create the connection
6770
$this->connection = $this->createConnection($dsn, $config, $options);
71+
$this->database = $this->getDefaultDatabaseName($dsn, $config);
6872

6973
// Select database
70-
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));
74+
$this->db = $this->connection->getDatabase($this->database);
7175

7276
$this->tablePrefix = $config['prefix'] ?? '';
7377

@@ -114,29 +118,53 @@ public function getSchemaBuilder()
114118
/**
115119
* Get the MongoDB database object.
116120
*
121+
* @deprecated since mongodb/laravel-mongodb:5.2, use getDatabase() instead
122+
*
117123
* @return Database
118124
*/
119125
public function getMongoDB()
120126
{
127+
trigger_error(sprintf('Since mongodb/laravel-mongodb:5.2, Method "%s()" is deprecated, use "getDatabase()" instead.', __FUNCTION__), E_USER_DEPRECATED);
128+
129+
return $this->db;
130+
}
131+
132+
/**
133+
* Get the MongoDB database object.
134+
*
135+
* @param string|null $name Name of the database, if not provided the default database will be returned.
136+
*
137+
* @return Database
138+
*/
139+
public function getDatabase(?string $name = null): Database
140+
{
141+
if ($name && $name !== $this->database) {
142+
return $this->connection->getDatabase($name);
143+
}
144+
121145
return $this->db;
122146
}
123147

124148
/**
125-
* return MongoDB object.
149+
* Return MongoDB object.
150+
*
151+
* @deprecated since mongodb/laravel-mongodb:5.2, use getClient() instead
126152
*
127153
* @return Client
128154
*/
129155
public function getMongoClient()
130156
{
131-
return $this->connection;
157+
trigger_error(sprintf('Since mongodb/laravel-mongodb:5.2, method "%s()" is deprecated, use "getClient()" instead.', __FUNCTION__), E_USER_DEPRECATED);
158+
159+
return $this->getClient();
132160
}
133161

134162
/**
135-
* {@inheritDoc}
163+
* Get the MongoDB client.
136164
*/
137-
public function getDatabaseName()
165+
public function getClient(): ?Client
138166
{
139-
return $this->getMongoDB()->getDatabaseName();
167+
return $this->connection;
140168
}
141169

142170
public function enableQueryLog()
@@ -233,7 +261,7 @@ protected function createConnection(string $dsn, array $config, array $options):
233261
*/
234262
public function ping(): void
235263
{
236-
$this->getMongoClient()->getManager()->selectServer(new ReadPreference(ReadPreference::PRIMARY_PREFERRED));
264+
$this->getClient()->getManager()->selectServer(new ReadPreference(ReadPreference::PRIMARY_PREFERRED));
237265
}
238266

239267
/** @inheritdoc */

‎src/MongoDBServiceProvider.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function register()
6767
assert($connection instanceof Connection, new InvalidArgumentException(sprintf('The database connection "%s" used for the session does not use the "mongodb" driver.', $connectionName)));
6868

6969
return new MongoDbSessionHandler(
70-
$connection->getMongoClient(),
70+
$connection->getClient(),
7171
$app->config->get('session.options', []) + [
7272
'database' => $connection->getDatabaseName(),
7373
'collection' => $app->config->get('session.table') ?: 'sessions',
@@ -132,8 +132,8 @@ private function registerFlysystemAdapter(): void
132132
throw new InvalidArgumentException(sprintf('The database connection "%s" does not use the "mongodb" driver.', $config['connection'] ?? $app['config']['database.default']));
133133
}
134134

135-
$bucket = $connection->getMongoClient()
136-
->selectDatabase($config['database'] ?? $connection->getDatabaseName())
135+
$bucket = $connection->getClient()
136+
->getDatabase($config['database'] ?? $connection->getDatabaseName())
137137
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs', 'disableMD5' => true]);
138138
}
139139

@@ -171,7 +171,7 @@ private function registerScoutEngine(): void
171171

172172
assert($connection instanceof Connection, new InvalidArgumentException(sprintf('The connection "%s" is not a MongoDB connection.', $connectionName)));
173173

174-
return new ScoutEngine($connection->getMongoDB(), $softDelete, $indexDefinitions);
174+
return new ScoutEngine($connection->getDatabase(), $softDelete, $indexDefinitions);
175175
});
176176

177177
return $engineManager;

‎src/Schema/Blueprint.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function create($options = [])
251251
{
252252
$collection = $this->collection->getCollectionName();
253253

254-
$db = $this->connection->getMongoDB();
254+
$db = $this->connection->getDatabase();
255255

256256
// Ensure the collection is created.
257257
$db->createCollection($collection, $options);

‎src/Schema/Builder.php‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function hasColumns($table, array $columns): bool
7676
*/
7777
public function hasCollection($name)
7878
{
79-
$db = $this->connection->getMongoDB();
79+
$db = $this->connection->getDatabase();
8080

8181
$collections = iterator_to_array($db->listCollections([
8282
'filter' => ['name' => $name],
@@ -139,7 +139,7 @@ public function dropAllTables()
139139

140140
public function getTables()
141141
{
142-
$db = $this->connection->getMongoDB();
142+
$db = $this->connection->getDatabase();
143143
$collections = [];
144144

145145
foreach ($db->listCollectionNames() as $collectionName) {
@@ -167,7 +167,7 @@ public function getTables()
167167

168168
public function getTableListing()
169169
{
170-
$collections = iterator_to_array($this->connection->getMongoDB()->listCollectionNames());
170+
$collections = iterator_to_array($this->connection->getDatabase()->listCollectionNames());
171171

172172
sort($collections);
173173

@@ -176,7 +176,7 @@ public function getTableListing()
176176

177177
public function getColumns($table)
178178
{
179-
$stats = $this->connection->getMongoDB()->selectCollection($table)->aggregate([
179+
$stats = $this->connection->getDatabase()->selectCollection($table)->aggregate([
180180
// Sample 1,000 documents to get a representative sample of the collection
181181
['$sample' => ['size' => 1_000]],
182182
// Convert each document to an array of fields
@@ -229,7 +229,7 @@ public function getColumns($table)
229229

230230
public function getIndexes($table)
231231
{
232-
$collection = $this->connection->getMongoDB()->selectCollection($table);
232+
$collection = $this->connection->getDatabase()->selectCollection($table);
233233
assert($collection instanceof Collection);
234234
$indexList = [];
235235

@@ -301,7 +301,7 @@ protected function createBlueprint($table, ?Closure $callback = null)
301301
*/
302302
public function getCollection($name)
303303
{
304-
$db = $this->connection->getMongoDB();
304+
$db = $this->connection->getDatabase();
305305

306306
$collections = iterator_to_array($db->listCollections([
307307
'filter' => ['name' => $name],
@@ -318,7 +318,7 @@ public function getCollection($name)
318318
protected function getAllCollections()
319319
{
320320
$collections = [];
321-
foreach ($this->connection->getMongoDB()->listCollections() as $collection) {
321+
foreach ($this->connection->getDatabase()->listCollections() as $collection) {
322322
$collections[] = $collection->getName();
323323
}
324324

‎tests/ConnectionTest.php‎

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,23 @@ public function testDisconnectAndCreateNewConnection()
4848
{
4949
$connection = DB::connection('mongodb');
5050
$this->assertInstanceOf(Connection::class, $connection);
51-
$client = $connection->getMongoClient();
51+
$client = $connection->getClient();
5252
$this->assertInstanceOf(Client::class, $client);
5353
$connection->disconnect();
54-
$client = $connection->getMongoClient();
54+
$client = $connection->getClient();
5555
$this->assertNull($client);
5656
DB::purge('mongodb');
5757
$connection = DB::connection('mongodb');
5858
$this->assertInstanceOf(Connection::class, $connection);
59-
$client = $connection->getMongoClient();
59+
$client = $connection->getClient();
6060
$this->assertInstanceOf(Client::class, $client);
6161
}
6262

6363
public function testDb()
6464
{
6565
$connection = DB::connection('mongodb');
6666
$this->assertInstanceOf(Database::class, $connection->getMongoDB());
67-
$this->assertInstanceOf(Client::class, $connection->getMongoClient());
67+
$this->assertInstanceOf(Client::class, $connection->getClient());
6868
}
6969

7070
public static function dataConnectionConfig(): Generator
@@ -196,14 +196,51 @@ public static function dataConnectionConfig(): Generator
196196
public function testConnectionConfig(string $expectedUri, string $expectedDatabaseName, array $config): void
197197
{
198198
$connection = new Connection($config);
199-
$client = $connection->getMongoClient();
199+
$client = $connection->getClient();
200200

201201
$this->assertSame($expectedUri, (string) $client);
202202
$this->assertSame($expectedDatabaseName, $connection->getMongoDB()->getDatabaseName());
203203
$this->assertSame('foo', $connection->getCollection('foo')->getCollectionName());
204204
$this->assertSame('foo', $connection->table('foo')->raw()->getCollectionName());
205205
}
206206

207+
public function testLegacyGetMongoClient(): void
208+
{
209+
$connection = DB::connection('mongodb');
210+
$expected = $connection->getClient();
211+
212+
$this->assertSame($expected, $connection->getMongoClient());
213+
}
214+
215+
public function testLegacyGetMongoDB(): void
216+
{
217+
$connection = DB::connection('mongodb');
218+
$expected = $connection->getDatabase();
219+
220+
$this->assertSame($expected, $connection->getMongoDB());
221+
}
222+
223+
public function testGetDatabase(): void
224+
{
225+
$connection = DB::connection('mongodb');
226+
$defaultName = env('MONGODB_DATABASE', 'unittest');
227+
$database = $connection->getDatabase();
228+
229+
$this->assertInstanceOf(Database::class, $database);
230+
$this->assertSame($defaultName, $database->getDatabaseName());
231+
$this->assertSame($database, $connection->getDatabase($defaultName), 'Same instance for the default database');
232+
}
233+
234+
public function testGetOtherDatabase(): void
235+
{
236+
$connection = DB::connection('mongodb');
237+
$name = 'other_random_database';
238+
$database = $connection->getDatabase($name);
239+
240+
$this->assertInstanceOf(Database::class, $database);
241+
$this->assertSame($name, $database->getDatabaseName($name));
242+
}
243+
207244
public function testConnectionWithoutConfiguredDatabase(): void
208245
{
209246
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /