Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Connection extends BaseConnection
*/
protected $connection;

private ?CommandSubscriber $commandSubscriber;
private ?CommandSubscriber $commandSubscriber = null;

/**
* Create a new database connection instance.
Expand All @@ -65,8 +65,6 @@ 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 @@ -141,6 +139,40 @@ public function getDatabaseName()
return $this->getMongoDB()->getDatabaseName();
}

public function enableQueryLog()
{
parent::enableQueryLog();

if (! $this->commandSubscriber) {
$this->commandSubscriber = new CommandSubscriber($this);
$this->connection->addSubscriber($this->commandSubscriber);
}
}

public function disableQueryLog()
{
parent::disableQueryLog();

if ($this->commandSubscriber) {
$this->connection->removeSubscriber($this->commandSubscriber);
$this->commandSubscriber = null;
}
}

protected function withFreshQueryLog($callback)
{
try {
return parent::withFreshQueryLog($callback);
} finally {
// The parent method enable query log using enableQueryLog()
// but disables it by setting $loggingQueries to false. We need to
// remove the subscriber for performance.
if (! $this->loggingQueries) {
$this->disableQueryLog();
}
}
}

/**
* Get the name of the default database based on db config or try to detect it from dsn.
*
Expand Down Expand Up @@ -203,8 +235,7 @@ public function ping(): void
/** @inheritdoc */
public function disconnect()
{
$this->connection?->removeSubscriber($this->commandSubscriber);
$this->commandSubscriber = null;
$this->disableQueryLog();
$this->connection = null;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,31 @@ public function testQueryLog()
}
}

public function testDisableQueryLog()
{
// Disabled by default
DB::table('items')->get();
$this->assertCount(0, DB::getQueryLog());

DB::enableQueryLog();
DB::table('items')->get();
$this->assertCount(1, DB::getQueryLog());

// Enable twice should only log once
DB::enableQueryLog();
DB::table('items')->get();
$this->assertCount(2, DB::getQueryLog());

DB::disableQueryLog();
DB::table('items')->get();
$this->assertCount(2, DB::getQueryLog());

// Disable twice should not log
DB::disableQueryLog();
DB::table('items')->get();
$this->assertCount(2, DB::getQueryLog());
}

public function testSchemaBuilder()
{
$schema = DB::connection('mongodb')->getSchemaBuilder();
Expand Down
Loading