Skip to content

Commit

Permalink
feat: dispatch QueryExecuted event
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Sep 27, 2024
1 parent 54425bb commit 5c85a7b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ public function driver(string $driver): DriverInterface
}
}

if ($driverObject instanceof EventDispatcherAwareInterface && $this->eventDispatcher) {
$driverObject->setEventDispatcher($this->eventDispatcher);
}

return $this->drivers[$driver];
}

Expand Down
11 changes: 11 additions & 0 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Cycle\Database\Config\DriverConfig;
use Cycle\Database\Config\PDOConnectionConfig;
use Cycle\Database\Config\ProvidesSourceString;
use Cycle\Database\Event\QueryExecuted;
use Cycle\Database\EventDispatcherAwareInterface;
use Cycle\Database\EventDispatcherAwareTrait;
use Cycle\Database\Exception\DriverException;
Expand Down Expand Up @@ -500,6 +501,16 @@ protected function statement(string $query, iterable $parameters = [], bool $ret
$this->logger->info($queryString, $context);
}
}

$this->eventDispatcher?->dispatch(
new QueryExecuted(
$query,
$parameters,
$queryStart,
microtime(true),
$this
)
);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/Event/QueryExecuted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Cycle\Database\Event;

use Cycle\Database\Driver\DriverInterface;

class QueryExecuted
{
public function __construct(
public readonly string $query,
public readonly array $params,
public readonly float $queryStart,
public readonly float $queryEnd,
public readonly DriverInterface $driver,
) {
}
}

0 comments on commit 5c85a7b

Please sign in to comment.