Skip to content

Commit

Permalink
feat: add EventDispatcher to drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Sep 27, 2024
1 parent 5f3fe4f commit 54425bb
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 4 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"require": {
"php": ">=8.0",
"ext-pdo": "*",
"psr/event-dispatcher": "^1.0",
"psr/log": "1 - 3",
"spiral/core": "^2.8 || ^3.0",
"spiral/pagination": "^2.8 || ^3.0",
Expand Down
52 changes: 51 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 17 additions & 2 deletions src/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cycle\Database\Driver\DriverInterface;
use Cycle\Database\Exception\DatabaseException;
use Cycle\Database\Exception\DBALException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand All @@ -28,7 +29,7 @@
*
* echo $manager->database('runtime')->select()->from('users')->count();
*/
final class DatabaseManager implements DatabaseProviderInterface, LoggerAwareInterface
final class DatabaseManager implements DatabaseProviderInterface, LoggerAwareInterface, EventDispatcherAwareInterface
{
/** @var Database[] */
private array $databases = [];
Expand All @@ -38,7 +39,8 @@ final class DatabaseManager implements DatabaseProviderInterface, LoggerAwareInt

public function __construct(
private DatabaseConfig $config,
private ?LoggerFactoryInterface $loggerFactory = null
private ?LoggerFactoryInterface $loggerFactory = null,
private ?EventDispatcherInterface $eventDispatcher = null
) {
}

Expand All @@ -57,6 +59,19 @@ public function setLogger(LoggerInterface $logger): void
}
}

public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;

// Assign the event dispatcher to all initialized drivers
foreach ($this->drivers as $driver) {
if ($driver instanceof EventDispatcherAwareInterface) {
$driver->setEventDispatcher($this->eventDispatcher);
}
}
}


/**
* Get all databases.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Driver/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Cycle\Database\Config\DriverConfig;
use Cycle\Database\Config\PDOConnectionConfig;
use Cycle\Database\Config\ProvidesSourceString;
use Cycle\Database\EventDispatcherAwareInterface;
use Cycle\Database\EventDispatcherAwareTrait;
use Cycle\Database\Exception\DriverException;
use Cycle\Database\Exception\ReadonlyConnectionException;
use Cycle\Database\Exception\StatementException;
Expand All @@ -35,9 +37,10 @@
/**
* Provides low level abstraction at top of
*/
abstract class Driver implements DriverInterface, NamedInterface, LoggerAwareInterface
abstract class Driver implements DriverInterface, NamedInterface, LoggerAwareInterface, EventDispatcherAwareInterface
{
use LoggerAwareTrait;
use EventDispatcherAwareTrait;

/**
* DateTime format to be used to perform automatic conversion of DateTime objects.
Expand Down
17 changes: 17 additions & 0 deletions src/EventDispatcherAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Cycle\Database;

use Psr\EventDispatcher\EventDispatcherInterface;

interface EventDispatcherAwareInterface
{
/**
* Sets an event dispatcher instance on the object.
*
* @param EventDispatcherInterface $eventDispatcher
*
* @return void
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void;
}
15 changes: 15 additions & 0 deletions src/EventDispatcherAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Cycle\Database;

use Psr\EventDispatcher\EventDispatcherInterface;

trait EventDispatcherAwareTrait
{
private ?EventDispatcherInterface $eventDispatcher = null;

public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;
}
}

0 comments on commit 54425bb

Please sign in to comment.