1ccb0284cSGreg Roach<?php 23976b470SGreg Roach 3ccb0284cSGreg Roach/** 4ccb0284cSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6ccb0284cSGreg Roach * This program is free software: you can redistribute it and/or modify 7ccb0284cSGreg Roach * it under the terms of the GNU General Public License as published by 8ccb0284cSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9ccb0284cSGreg Roach * (at your option) any later version. 10ccb0284cSGreg Roach * This program is distributed in the hope that it will be useful, 11ccb0284cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12ccb0284cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13ccb0284cSGreg Roach * GNU General Public License for more details. 14ccb0284cSGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16ccb0284cSGreg Roach */ 17fcfa147eSGreg Roach 18ccb0284cSGreg Roachdeclare(strict_types=1); 19ccb0284cSGreg Roach 20ccb0284cSGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 21ccb0284cSGreg Roach 22*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB; 236ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 246ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 256ccdf4f0SGreg Roachuse Psr\Http\Server\MiddlewareInterface; 266ccdf4f0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 27ccb0284cSGreg Roach 28ccb0284cSGreg Roach/** 29ccb0284cSGreg Roach * Middleware to wrap a request in a transaction. 30ccb0284cSGreg Roach */ 31c1010edaSGreg Roachclass UseTransaction implements MiddlewareInterface 32c1010edaSGreg Roach{ 33d3b2e31dSGreg Roach // If a transaction deadlock occurs, try again. 34d3b2e31dSGreg Roach private const DEADLOCK_RETRY_ATTEMPTS = 3; 35d3b2e31dSGreg Roach 36ccb0284cSGreg Roach /** 376ccdf4f0SGreg Roach * @param ServerRequestInterface $request 386ccdf4f0SGreg Roach * @param RequestHandlerInterface $handler 39ccb0284cSGreg Roach * 406ccdf4f0SGreg Roach * @return ResponseInterface 41ccb0284cSGreg Roach */ 426ccdf4f0SGreg Roach public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface 43c1010edaSGreg Roach { 44d3b2e31dSGreg Roach DB::connection()->transaction(static function () use ($request, $handler, &$response) { 456ccdf4f0SGreg Roach $response = $handler->handle($request); 46d3b2e31dSGreg Roach }, self::DEADLOCK_RETRY_ATTEMPTS); 47ccb0284cSGreg Roach 48ccb0284cSGreg Roach return $response; 49ccb0284cSGreg Roach } 50ccb0284cSGreg Roach} 51