. */ 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; /** * Middleware to wrap a request in a transaction. */ class UseTransaction implements MiddlewareInterface { // If a transaction deadlock occurs, try again. private const DEADLOCK_RETRY_ATTEMPTS = 3; /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { DB::connection()->transaction(static function () use ($request, $handler, &$response) { $response = $handler->handle($request); }, self::DEADLOCK_RETRY_ATTEMPTS); return $response; } }