. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Closure; use Fisharebest\Webtrees\Database; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Throwable; /** * Middleware to wrap a request in a transaction. */ class UseTransaction { /** * @param Request $request * @param Closure $next * * @return Response * @throws Throwable */ public function handle(Request $request, Closure $next): Response { throw new Throwable('eek'); $connected = Database::isConnected(); if ($connected) { Database::beginTransaction(); } try { $response = $next($request); if ($connected) { Database::commit(); } } catch (Throwable $ex) { if ($connected) { Database::rollBack(); throw $ex; } } return $response; } }