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 a187abc

Browse files
committed
PHPORM-56 Replace Collection proxy class with Driver monitoring
1 parent 7551f76 commit a187abc

File tree

17 files changed

+101
-144
lines changed

17 files changed

+101
-144
lines changed

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
1010
* Remove `MongoFailedJobProvider`, replaced by Laravel `DatabaseFailedJobProvider` by @GromNaN in [#3122](https://github.com/mongodb/laravel-mongodb/pull/3122)
1111
* Remove custom `PasswordResetServiceProvider`, use the default `DatabaseTokenRepository` by @GromNaN in [#3124](https://github.com/mongodb/laravel-mongodb/pull/3124)
1212
* Remove `Blueprint::background()` method by @GromNaN in [#3132](https://github.com/mongodb/laravel-mongodb/pull/3132)
13+
* Replace `Collection` proxy class with Driver monitoring by @GromNaN in [#3137]((https://github.com/mongodb/laravel-mongodb/pull/3137)
1314

1415
## [4.8.0] - 2024年08月27日
1516

‎docs/includes/query-builder/QueryBuilderTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Pagination\AbstractPaginator;
99
use Illuminate\Support\Facades\DB;
1010
use MongoDB\BSON\Regex;
11-
use MongoDB\Laravel\Collection;
11+
use MongoDB\Collection;
1212
use MongoDB\Laravel\Tests\TestCase;
1313

1414
use function file_get_contents;

‎src/Bus/MongoBatchRepository.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use Illuminate\Support\Carbon;
1515
use MongoDB\BSON\ObjectId;
1616
use MongoDB\BSON\UTCDateTime;
17+
use MongoDB\Collection;
1718
use MongoDB\Driver\ReadPreference;
18-
use MongoDB\Laravel\Collection;
1919
use MongoDB\Laravel\Connection;
2020
use MongoDB\Operation\FindOneAndUpdate;
2121
use Override;

‎src/Cache/MongoLock.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Support\Carbon;
77
use InvalidArgumentException;
88
use MongoDB\BSON\UTCDateTime;
9-
use MongoDB\Laravel\Collection;
9+
use MongoDB\Collection;
1010
use MongoDB\Operation\FindOneAndUpdate;
1111
use Override;
1212

‎src/Cache/MongoStore.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Illuminate\Contracts\Cache\Store;
88
use Illuminate\Support\Carbon;
99
use MongoDB\BSON\UTCDateTime;
10-
use MongoDB\Laravel\Collection;
10+
use MongoDB\Collection;
1111
use MongoDB\Laravel\Connection;
1212
use MongoDB\Operation\FindOneAndUpdate;
1313
use Override;

‎src/Collection.php‎

Lines changed: 0 additions & 80 deletions
This file was deleted.

‎src/CommandSubscriber.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel;
4+
5+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
6+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
7+
use MongoDB\Driver\Monitoring\CommandSubscriber as CommandSubscriberInterface;
8+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
9+
use Throwable;
10+
11+
use function get_object_vars;
12+
use function in_array;
13+
use function json_encode;
14+
15+
use const JSON_THROW_ON_ERROR;
16+
17+
/** @internal */
18+
final class CommandSubscriber implements CommandSubscriberInterface
19+
{
20+
/** @var array<string, CommandStartedEvent> */
21+
private array $commands = [];
22+
23+
public function __construct(private Connection $connection)
24+
{
25+
}
26+
27+
public function commandStarted(CommandStartedEvent $event)
28+
{
29+
$this->commands[$event->getOperationId()] = $event;
30+
}
31+
32+
public function commandFailed(CommandFailedEvent $event)
33+
{
34+
$this->logQuery($event);
35+
}
36+
37+
public function commandSucceeded(CommandSucceededEvent $event)
38+
{
39+
$this->logQuery($event);
40+
}
41+
42+
private function logQuery(CommandSucceededEvent|CommandFailedEvent $event): void
43+
{
44+
$startedEvent = $this->commands[$event->getOperationId()];
45+
unset($this->commands[$event->getOperationId()]);
46+
47+
$command = '';
48+
foreach (get_object_vars($startedEvent->getCommand()) as $key => $value) {
49+
if ($key[0] !== '$' && ! in_array($key, ['lsid', 'txnNumber'])) {
50+
$command .= ($command ? ', ' : '{') . json_encode($key) . ':';
51+
try {
52+
$command .= json_encode($value, JSON_THROW_ON_ERROR);
53+
} catch (Throwable $e) {
54+
$command .= json_encode('Invalid JSON: ' . $e->getMessage());
55+
}
56+
}
57+
}
58+
59+
$command .= $command ? '}' : '{}';
60+
61+
$this->connection->logQuery($command, [], $event->getDurationMicros());
62+
}
63+
}

‎src/Connection.php‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Connection as BaseConnection;
99
use InvalidArgumentException;
1010
use MongoDB\Client;
11+
use MongoDB\Collection;
1112
use MongoDB\Database;
1213
use MongoDB\Driver\Exception\AuthenticationException;
1314
use MongoDB\Driver\Exception\ConnectionException;
@@ -47,6 +48,8 @@ class Connection extends BaseConnection
4748
*/
4849
protected $connection;
4950

51+
private ?CommandSubscriber $commandSubscriber;
52+
5053
/**
5154
* Create a new database connection instance.
5255
*/
@@ -62,6 +65,8 @@ public function __construct(array $config)
6265

6366
// Create the connection
6467
$this->connection = $this->createConnection($dsn, $config, $options);
68+
$this->commandSubscriber = new CommandSubscriber($this);
69+
$this->connection->addSubscriber($this->commandSubscriber);
6570

6671
// Select database
6772
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));
@@ -97,9 +102,9 @@ public function table($table, $as = null)
97102
*
98103
* @return Collection
99104
*/
100-
public function getCollection($name)
105+
public function getCollection($name): Collection
101106
{
102-
return newCollection($this, $this->db->selectCollection($this->tablePrefix . $name));
107+
return $this->db->selectCollection($this->tablePrefix . $name);
103108
}
104109

105110
/** @inheritdoc */
@@ -198,6 +203,8 @@ public function ping(): void
198203
/** @inheritdoc */
199204
public function disconnect()
200205
{
206+
$this->connection?->removeSubscriber($this->commandSubscriber);
207+
$this->commandSubscriber = null;
201208
$this->connection = null;
202209
}
203210

@@ -264,12 +271,6 @@ protected function getDsn(array $config): string
264271
throw new InvalidArgumentException('MongoDB connection configuration requires "dsn" or "host" key.');
265272
}
266273

267-
/** @inheritdoc */
268-
public function getElapsedTime($start)
269-
{
270-
return parent::getElapsedTime($start);
271-
}
272-
273274
/** @inheritdoc */
274275
public function getDriverName()
275276
{

‎src/Query/AggregationBuilder.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
use Iterator;
1111
use MongoDB\Builder\BuilderEncoder;
1212
use MongoDB\Builder\Stage\FluentFactoryTrait;
13-
use MongoDB\CollectionasMongoDBCollection;
13+
use MongoDB\Collection;
1414
use MongoDB\Driver\CursorInterface;
15-
use MongoDB\Laravel\Collection as LaravelMongoDBCollection;
1615

1716
use function array_replace;
1817
use function collect;
@@ -24,7 +23,7 @@ class AggregationBuilder
2423
use FluentFactoryTrait;
2524

2625
public function __construct(
27-
private MongoDBCollection|LaravelMongoDBCollection $collection,
26+
private Collection $collection,
2827
private readonly array $options = [],
2928
) {
3029
}

‎src/Query/Builder.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Builder extends BaseBuilder
8282
/**
8383
* The database collection.
8484
*
85-
* @var \MongoDB\Laravel\Collection
85+
* @var \MongoDB\Collection
8686
*/
8787
protected $collection;
8888

0 commit comments

Comments
(0)

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