Skip to content

Commit 74f219c

Browse files
authored
PHPORM-56 Replace Collection proxy class with Driver monitoring (#3137)
1 parent e424861 commit 74f219c

18 files changed

+97
-148
lines changed

CHANGELOG.md

+1
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

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"illuminate/database": "^11",
3131
"illuminate/events": "^11",
3232
"illuminate/support": "^11",
33-
"mongodb/mongodb": "^1.15"
33+
"mongodb/mongodb": "^1.18"
3434
},
3535
"require-dev": {
3636
"mongodb/builder": "^0.2",

docs/includes/query-builder/QueryBuilderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Illuminate\Support\Facades\DB;
1010
use MongoDB\BSON\ObjectId;
1111
use MongoDB\BSON\Regex;
12-
use MongoDB\Laravel\Collection;
12+
use MongoDB\Collection;
1313
use MongoDB\Laravel\Tests\TestCase;
1414

1515
use function file_get_contents;

src/Bus/MongoBatchRepository.php

+1-1
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

+1-1
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

+1-1
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

-80
This file was deleted.

src/CommandSubscriber.php

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

src/Connection.php

+9-8
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 new Collection($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

+2-3
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\Collection as MongoDBCollection;
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

+1-1
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

src/Schema/Blueprint.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use Illuminate\Database\Connection;
88
use Illuminate\Database\Schema\Blueprint as SchemaBlueprint;
9-
use MongoDB\Laravel\Collection;
9+
use MongoDB\Collection;
1010

1111
use function array_flip;
1212
use function implode;
@@ -21,14 +21,14 @@ class Blueprint extends SchemaBlueprint
2121
/**
2222
* The MongoConnection object for this blueprint.
2323
*
24-
* @var \MongoDB\Laravel\Connection
24+
* @var Connection
2525
*/
2626
protected $connection;
2727

2828
/**
29-
* The MongoCollection object for this blueprint.
29+
* The Collection object for this blueprint.
3030
*
31-
* @var Collection|\MongoDB\Collection
31+
* @var Collection
3232
*/
3333
protected $collection;
3434

tests/Cache/MongoLockTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Illuminate\Support\Facades\DB;
99
use InvalidArgumentException;
1010
use MongoDB\BSON\UTCDateTime;
11+
use MongoDB\Collection;
1112
use MongoDB\Laravel\Cache\MongoLock;
12-
use MongoDB\Laravel\Collection;
1313
use MongoDB\Laravel\Tests\TestCase;
1414
use PHPUnit\Framework\Attributes\TestWith;
1515

tests/Casts/DecimalTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use MongoDB\BSON\Int64;
1111
use MongoDB\BSON\Javascript;
1212
use MongoDB\BSON\UTCDateTime;
13-
use MongoDB\Laravel\Collection;
13+
use MongoDB\Collection;
1414
use MongoDB\Laravel\Tests\Models\Casting;
1515
use MongoDB\Laravel\Tests\TestCase;
1616

tests/CollectionTest.php

-36
This file was deleted.

0 commit comments

Comments
 (0)