|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hiapi\Http\Psr15; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
8
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class SkipExceptionsIn |
|
12
|
|
|
* |
|
13
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class SkipExceptionsIn implements MiddlewareInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var MiddlewareInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $middleware; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(MiddlewareInterface $middleware) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->middleware = $middleware; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Process an incoming server request and return a response, optionally delegating |
|
29
|
|
|
* response creation to a handler. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
32
|
|
|
{ |
|
33
|
|
|
$isExecutingNextHandler = false; |
|
34
|
|
|
$next = new class($isExecutingNextHandler, $handler) implements RequestHandlerInterface { |
|
35
|
|
|
/** |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $isExecutingNextHandler; |
|
39
|
|
|
/** |
|
40
|
|
|
* @var RequestHandlerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $nextHandler; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(bool &$isExecutingNextHandler, RequestHandlerInterface $realHandler) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->isExecutingNextHandler = &$isExecutingNextHandler; |
|
47
|
|
|
$this->nextHandler = $realHandler; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Handle the request and return a response. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
54
|
|
|
{ |
|
55
|
|
|
$this->isExecutingNextHandler = true; |
|
56
|
|
|
$result = $this->nextHandler->handle($request); |
|
57
|
|
|
$this->isExecutingNextHandler = false; |
|
58
|
|
|
|
|
59
|
|
|
return $result; |
|
60
|
|
|
} |
|
61
|
|
|
}; |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
|
|
return $this->middleware->process($request, $next); |
|
65
|
|
|
} catch (\Throwable $throwable) { |
|
66
|
|
|
if ($isExecutingNextHandler) { |
|
67
|
|
|
throw $throwable; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $handler->handle($request); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|