From e998af3ad55364ac846da5abe452412f1cb77692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= <me@jiripudil.cz> Date: Wed, 5 Feb 2025 09:49:53 +0100 Subject: [PATCH] make TableManager's connection configurable for blocks of code --- src/TableManager.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/TableManager.php b/src/TableManager.php index e17e9ad..98ab150 100644 --- a/src/TableManager.php +++ b/src/TableManager.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Grifart\Tables; -use Dibi\Connection; +use Dibi\IConnection; use Dibi\UniqueConstraintViolationException; use Grifart\Tables\Conditions\Composite; use Grifart\Tables\Conditions\Condition; @@ -21,7 +21,7 @@ final class TableManager { public function __construct( - private Connection $connection, + private IConnection $connection, ) {} /** @@ -242,4 +242,24 @@ final class TableManager // UPDATE: $this->update($table, $changes); } + + /** + * @template T + * @param \Closure(): T $block + * @return T + */ + public function withConnection( + IConnection $connection, + \Closure $block, + ): mixed + { + $previousConnection = $this->connection; + $this->connection = $connection; + + try { + return $block(); + } finally { + $this->connection = $previousConnection; + } + } } -- GitLab