Skip to content

Commit

Permalink
PHPORM-56 Replace Collection proxy class with Driver monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Sep 5, 2024
1 parent 7551f76 commit 6db1052
Show file tree
Hide file tree
Showing 17 changed files with 93 additions and 138 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
* Remove `MongoFailedJobProvider`, replaced by Laravel `DatabaseFailedJobProvider` by @GromNaN in [#3122](https://github.com/mongodb/laravel-mongodb/pull/3122)
* Remove custom `PasswordResetServiceProvider`, use the default `DatabaseTokenRepository` by @GromNaN in [#3124](https://github.com/mongodb/laravel-mongodb/pull/3124)
* Remove `Blueprint::background()` method by @GromNaN in [#3132](https://github.com/mongodb/laravel-mongodb/pull/3132)
* Replace `Collection` proxy class with Driver monitoring by @GromNaN in [#3137]((https://github.com/mongodb/laravel-mongodb/pull/3137)

## [4.8.0] - 2024-08-27

Expand Down
2 changes: 1 addition & 1 deletion docs/includes/query-builder/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Facades\DB;
use MongoDB\BSON\Regex;
use MongoDB\Laravel\Collection;
use MongoDB\Collection;
use MongoDB\Laravel\Tests\TestCase;

use function file_get_contents;
Expand Down
2 changes: 1 addition & 1 deletion src/Bus/MongoBatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use Illuminate\Support\Carbon;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Collection;
use MongoDB\Driver\ReadPreference;
use MongoDB\Laravel\Collection;
use MongoDB\Laravel\Connection;
use MongoDB\Operation\FindOneAndUpdate;
use Override;
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/MongoLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Carbon;
use InvalidArgumentException;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Collection;
use MongoDB\Collection;
use MongoDB\Operation\FindOneAndUpdate;
use Override;

Expand Down
2 changes: 1 addition & 1 deletion src/Cache/MongoStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\Carbon;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Collection;
use MongoDB\Collection;
use MongoDB\Laravel\Connection;
use MongoDB\Operation\FindOneAndUpdate;
use Override;
Expand Down
80 changes: 0 additions & 80 deletions src/Collection.php

This file was deleted.

55 changes: 55 additions & 0 deletions src/CommandSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace MongoDB\Laravel;

use MongoDB\Driver\Monitoring\CommandFailedEvent;
use MongoDB\Driver\Monitoring\CommandStartedEvent;
use MongoDB\Driver\Monitoring\CommandSubscriber as CommandSubscriberInterface;
use MongoDB\Driver\Monitoring\CommandSucceededEvent;

use function get_object_vars;
use function json_encode;

use const JSON_THROW_ON_ERROR;

/** @internal */
final class CommandSubscriber implements CommandSubscriberInterface
{
/** @var array<string, CommandStartedEvent> */
private array $commands = [];

public function __construct(private Connection $connection)
{
}

public function commandStarted(CommandStartedEvent $event)
{
$this->commands[$event->getOperationId()] = $event;
}

public function commandFailed(CommandFailedEvent $event)
{
$this->logQuery($event);
}

public function commandSucceeded(CommandSucceededEvent $event)
{
$this->logQuery($event);
}

private function logQuery(CommandSucceededEvent|CommandFailedEvent $event): void
{
$startedEvent = $this->commands[$event->getOperationId()];
unset($this->commands[$event->getOperationId()]);

$command = clone $startedEvent->getCommand();
unset($command->lsid, $command->txnNumber);
foreach (get_object_vars($command) as $key => $value) {
if ($key[0] === '$') {
unset($command->{$key});
}
}

$this->connection->logQuery(json_encode($command, JSON_THROW_ON_ERROR), [], $event->getDurationMicros());
}
}
11 changes: 9 additions & 2 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Connection as BaseConnection;
use InvalidArgumentException;
use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Exception\AuthenticationException;
use MongoDB\Driver\Exception\ConnectionException;
Expand Down Expand Up @@ -47,6 +48,8 @@ class Connection extends BaseConnection
*/
protected $connection;

private ?CommandSubscriber $commandSubscriber;

/**
* Create a new database connection instance.
*/
Expand All @@ -62,6 +65,8 @@ public function __construct(array $config)

// Create the connection
$this->connection = $this->createConnection($dsn, $config, $options);
$this->commandSubscriber = new CommandSubscriber($this);
$this->connection->addSubscriber($this->commandSubscriber);

// Select database
$this->db = $this->connection->selectDatabase($this->getDefaultDatabaseName($dsn, $config));
Expand Down Expand Up @@ -97,9 +102,9 @@ public function table($table, $as = null)
*
* @return Collection
*/
public function getCollection($name)
public function getCollection($name): Collection
{
return new Collection($this, $this->db->selectCollection($this->tablePrefix . $name));
return $this->db->selectCollection($this->tablePrefix . $name);
}

/** @inheritdoc */
Expand Down Expand Up @@ -198,6 +203,8 @@ public function ping(): void
/** @inheritdoc */
public function disconnect()
{
$this->connection->removeSubscriber($this->commandSubscriber);
$this->commandSubscriber = null;
$this->connection = null;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Query/AggregationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
use Iterator;
use MongoDB\Builder\BuilderEncoder;
use MongoDB\Builder\Stage\FluentFactoryTrait;
use MongoDB\Collection as MongoDBCollection;
use MongoDB\Collection;
use MongoDB\Driver\CursorInterface;
use MongoDB\Laravel\Collection as LaravelMongoDBCollection;

use function array_replace;
use function collect;
Expand All @@ -24,7 +23,7 @@ class AggregationBuilder
use FluentFactoryTrait;

public function __construct(
private MongoDBCollection|LaravelMongoDBCollection $collection,
private Collection $collection,
private readonly array $options = [],
) {
}
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Builder extends BaseBuilder
/**
* The database collection.
*
* @var \MongoDB\Laravel\Collection
* @var \MongoDB\Collection
*/
protected $collection;

Expand Down
6 changes: 3 additions & 3 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Blueprint as SchemaBlueprint;
use MongoDB\Laravel\Collection;
use MongoDB\Collection;

use function array_flip;
use function implode;
Expand All @@ -21,14 +21,14 @@ class Blueprint extends SchemaBlueprint
/**
* The MongoConnection object for this blueprint.
*
* @var \MongoDB\Laravel\Connection
* @var Connection
*/
protected $connection;

/**
* The MongoCollection object for this blueprint.
*
* @var Collection|\MongoDB\Collection
* @var Collection|Collection
*/
protected $collection;

Expand Down
2 changes: 1 addition & 1 deletion tests/Cache/MongoLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Collection;
use MongoDB\Laravel\Cache\MongoLock;
use MongoDB\Laravel\Collection;
use MongoDB\Laravel\Tests\TestCase;
use PHPUnit\Framework\Attributes\TestWith;

Expand Down
2 changes: 1 addition & 1 deletion tests/Casts/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use MongoDB\BSON\Int64;
use MongoDB\BSON\Javascript;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Collection;
use MongoDB\Collection;
use MongoDB\Laravel\Tests\Models\Casting;
use MongoDB\Laravel\Tests\TestCase;

Expand Down
36 changes: 0 additions & 36 deletions tests/CollectionTest.php

This file was deleted.

19 changes: 14 additions & 5 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use MongoDB\Client;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Driver\Exception\ConnectionTimeoutException;
use MongoDB\Laravel\Collection;
use MongoDB\Laravel\Connection;
use MongoDB\Laravel\Query\Builder;
use MongoDB\Laravel\Schema\Builder as SchemaBuilder;
Expand Down Expand Up @@ -225,9 +226,6 @@ public function testCollection()

$collection = DB::connection('mongodb')->table('unittests');
$this->assertInstanceOf(Builder::class, $collection);

$collection = DB::connection('mongodb')->table('unittests');
$this->assertInstanceOf(Builder::class, $collection);
}

public function testPrefix()
Expand All @@ -251,7 +249,8 @@ public function testQueryLog()
$this->assertCount(0, DB::getQueryLog());

DB::table('items')->get();
$this->assertCount(1, DB::getQueryLog());
$this->assertCount(1, $logs = DB::getQueryLog());
$this->assertJsonStringEqualsJsonString('{"find":"items","filter":{}}', $logs[0]['query']);

DB::table('items')->insert(['name' => 'test']);
$this->assertCount(2, DB::getQueryLog());
Expand All @@ -264,6 +263,16 @@ public function testQueryLog()

DB::table('items')->where('name', 'test')->delete();
$this->assertCount(5, DB::getQueryLog());

// Error
try {
DB::table('items')->where('name', 'test')->update(
['$set' => ['embed' => ['foo' => 'bar']], '$unset' => ['embed' => ['foo']]],
);
self::fail('Expected BulkWriteException');
} catch (BulkWriteException) {
$this->assertCount(6, $logs = DB::getQueryLog());
}
}

public function testSchemaBuilder()
Expand Down
2 changes: 1 addition & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Collection;
use MongoDB\Collection;
use MongoDB\Laravel\Connection;
use MongoDB\Laravel\Eloquent\Model;
use MongoDB\Laravel\Tests\Models\Book;
Expand Down
Loading

0 comments on commit 6db1052

Please sign in to comment.