. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Illuminate\Database\Capsule\Manager as DB; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Throwable; /** * Middleware to wrap a request in a transaction. */ class UseTransaction implements MiddlewareInterface { /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface * @throws Throwable */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { DB::connection()->beginTransaction(); $response = $handler->handle($request); DB::connection()->commit(); return $response; } catch (Throwable $exception) { DB::connection()->rollBack(); throw $exception; } } }