Skip to content

Commit

Permalink
Merge pull request #106 from indjeto/fix-for-dbal-versions
Browse files Browse the repository at this point in the history
Fix for ConnectionMiddleware to support DBAL 3 and DBAL 4 connection …
  • Loading branch information
indjeto authored Aug 13, 2024
2 parents 0a70c9e + c7b55e6 commit 090a78e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"symfony/phpunit-bridge": "^6.4"
},
"autoload": {
"files": [
"src/aliases.php"
],
"psr-4": {
"DataDog\\AuditBundle\\": "src"
}
Expand Down
35 changes: 35 additions & 0 deletions src/DBAL/Middleware/ConnectionMiddlewareForV3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace DataDog\AuditBundle\DBAL\Middleware;

use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;

class ConnectionMiddlewareForV3 extends AbstractConnectionMiddleware
{
public function __construct(
Connection $wrappedConnection,
private readonly AuditFlushMiddleware $auditFlushMiddleware
)
{
parent::__construct($wrappedConnection);
}

/**
* Override of the commit method
*
* @throws Exception
*/
public function commit()
{
// Call the flusher callback if it's available.
if ($this->auditFlushMiddleware->flushHandler !== null) {
($this->auditFlushMiddleware->flushHandler)();
$this->auditFlushMiddleware->flushHandler = null;
}

// Call the parent's commit method
return parent::commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;

class ConnectionMiddleware extends AbstractConnectionMiddleware
class ConnectionMiddlewareForV4 extends AbstractConnectionMiddleware
{
public function __construct(
Connection $wrappedConnection,
Expand Down
18 changes: 18 additions & 0 deletions src/aliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace DataDog\AuditBundle;

use DataDog\AuditBundle\DBAL\Middleware\ConnectionMiddleware;
use DataDog\AuditBundle\DBAL\Middleware\ConnectionMiddlewareForV3;
use DataDog\AuditBundle\DBAL\Middleware\ConnectionMiddlewareForV4;
use Doctrine\DBAL\VersionAwarePlatformDriver;

if (!class_exists(ConnectionMiddleware::class)) {
if (!interface_exists(VersionAwarePlatformDriver::class)) {
class_alias(ConnectionMiddlewareForV4::class, ConnectionMiddleware::class);
} else {
class_alias(ConnectionMiddlewareForV3::class, ConnectionMiddleware::class);
}
}

0 comments on commit 090a78e

Please sign in to comment.