Skip to content

Commit

Permalink
feat: dispatch transactions events
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Sep 27, 2024
1 parent 5c85a7b commit 03903f3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Cycle\Database\Config\PDOConnectionConfig;
use Cycle\Database\Config\ProvidesSourceString;
use Cycle\Database\Event\QueryExecuted;
use Cycle\Database\Event\TransactionBeginning;
use Cycle\Database\Event\TransactionCommited;
use Cycle\Database\Event\TransactionRolledBack;
use Cycle\Database\EventDispatcherAwareInterface;
use Cycle\Database\EventDispatcherAwareTrait;
use Cycle\Database\Exception\DriverException;
Expand Down Expand Up @@ -358,6 +361,8 @@ public function beginTransaction(string $isolationLevel = null): bool

$this->createSavepoint($this->transactionLevel);

$this->eventDispatcher?->dispatch(new TransactionBeginning($this));

return true;
}

Expand Down Expand Up @@ -399,6 +404,8 @@ public function commitTransaction(): bool

$this->releaseSavepoint($this->transactionLevel + 1);

$this->eventDispatcher?->dispatch(new TransactionCommited($this));

return true;
}

Expand Down Expand Up @@ -436,6 +443,8 @@ public function rollbackTransaction(): bool

$this->rollbackSavepoint($this->transactionLevel + 1);

$this->eventDispatcher?->dispatch(new TransactionRolledBack($this));

return true;
}

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

namespace Cycle\Database\Event;

use Cycle\Database\Driver\DriverInterface;

abstract class AbstractDriverEvent
{
public function __construct(
public readonly DriverInterface $driver,
) {
}
}
5 changes: 3 additions & 2 deletions src/Event/QueryExecuted.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use Cycle\Database\Driver\DriverInterface;

class QueryExecuted
final class QueryExecuted extends AbstractDriverEvent
{
public function __construct(
public readonly string $query,
public readonly array $params,
public readonly float $queryStart,
public readonly float $queryEnd,
public readonly DriverInterface $driver,
DriverInterface $driver,
) {
parent::__construct($driver);
}
}
10 changes: 10 additions & 0 deletions src/Event/TransactionBeginning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cycle\Database\Event;

use Cycle\Database\Event\AbstractDriverEvent;

class TransactionBeginning extends AbstractDriverEvent
{

}
8 changes: 8 additions & 0 deletions src/Event/TransactionCommited.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cycle\Database\Event;

class TransactionCommited extends AbstractDriverEvent
{

}
8 changes: 8 additions & 0 deletions src/Event/TransactionRolledBack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cycle\Database\Event;

class TransactionRolledBack extends AbstractDriverEvent
{

}

0 comments on commit 03903f3

Please sign in to comment.