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 2b02660
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 145 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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"illuminate/database": "^11",
"illuminate/events": "^11",
"illuminate/support": "^11",
"mongodb/mongodb": "^1.15"
"mongodb/mongodb": "^1.18"
},
"require-dev": {
"mongodb/builder": "^0.2",
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.

63 changes: 63 additions & 0 deletions src/CommandSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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 Throwable;

use function get_object_vars;
use function in_array;
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 = '';
foreach (get_object_vars($startedEvent->getCommand()) as $key => $value) {
if ($key[0] !== '$' && ! in_array($key, ['lsid', 'txnNumber'])) {
$command .= ($command ? ', ' : '{') . json_encode($key) . ':';
try {
$command .= json_encode($value, JSON_THROW_ON_ERROR);
} catch (Throwable $e) {
$command .= json_encode('Invalid JSON: ' . $e->getMessage());
}
}
}

$command .= $command ? '}' : '{}';

$this->connection->logQuery($command, [], $event->getDurationMicros());
}
}
17 changes: 9 additions & 8 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 Expand Up @@ -264,12 +271,6 @@ protected function getDsn(array $config): string
throw new InvalidArgumentException('MongoDB connection configuration requires "dsn" or "host" key.');
}

/** @inheritdoc */
public function getElapsedTime($start)
{
return parent::getElapsedTime($start);
}

/** @inheritdoc */
public function getDriverName()
{
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.

Loading

0 comments on commit 2b02660

Please sign in to comment.