1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lit\Nimo\Traits; |
6
|
|
|
|
7
|
|
|
use Lit\Nimo\Interfaces\ExceptionHandlerInterface; |
8
|
|
|
use Lit\Nimo\Interfaces\RequestPredictionInterface; |
9
|
|
|
use Lit\Nimo\Middlewares\CatchMiddleware; |
10
|
|
|
use Lit\Nimo\Middlewares\MiddlewarePipe; |
11
|
|
|
use Lit\Nimo\Middlewares\PredictionWrapperMiddleware; |
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Inludes some wrapping and compositing method about middleware. |
16
|
|
|
*/ |
17
|
|
|
trait MiddlewareCompositeTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* append $middleware after this one, return the new $middlewareStack |
21
|
|
|
* |
22
|
|
|
* @param MiddlewareInterface $middleware The middleware. |
23
|
|
|
* @return MiddlewarePipe |
24
|
|
|
*/ |
25
|
1 |
|
public function append(MiddlewareInterface $middleware): MiddlewarePipe |
26
|
|
|
{ |
27
|
1 |
|
$stack = new MiddlewarePipe(); |
28
|
|
|
|
29
|
|
|
/** @var MiddlewareInterface $this */ |
30
|
|
|
return $stack |
31
|
1 |
|
->append($this) |
32
|
1 |
|
->append($middleware); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* prepend $middleware before this one, return the new $middlewareStack |
37
|
|
|
* |
38
|
|
|
* @param MiddlewareInterface $middleware The middleware. |
39
|
|
|
* @return MiddlewarePipe |
40
|
|
|
*/ |
41
|
1 |
|
public function prepend(MiddlewareInterface $middleware): MiddlewarePipe |
42
|
|
|
{ |
43
|
1 |
|
$stack = new MiddlewarePipe(); |
44
|
|
|
|
45
|
|
|
/** @var MiddlewareInterface $this */ |
46
|
|
|
return $stack |
47
|
1 |
|
->prepend($this) |
48
|
1 |
|
->prepend($middleware); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* return a new middleware that only take effect when the prediction is passed. |
53
|
|
|
* |
54
|
|
|
* @param RequestPredictionInterface $requestPrediction The prediction. |
55
|
|
|
* @return PredictionWrapperMiddleware |
56
|
|
|
*/ |
57
|
1 |
|
public function when(RequestPredictionInterface $requestPrediction): PredictionWrapperMiddleware |
58
|
|
|
{ |
59
|
|
|
/** @var MiddlewareInterface $this */ |
60
|
1 |
|
return new PredictionWrapperMiddleware($this, $requestPrediction); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* return a new middleware that only take effect when the prediction is NOT passed. |
65
|
|
|
* |
66
|
|
|
* @param RequestPredictionInterface $requestPrediction The prediction. |
67
|
|
|
* @return PredictionWrapperMiddleware |
68
|
|
|
*/ |
69
|
1 |
|
public function unless(RequestPredictionInterface $requestPrediction): PredictionWrapperMiddleware |
70
|
|
|
{ |
71
|
|
|
/** @var MiddlewareInterface $this */ |
72
|
1 |
|
return new PredictionWrapperMiddleware($this, $requestPrediction, true); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* wraps this middleware by try catch. |
77
|
|
|
* |
78
|
|
|
* @param ExceptionHandlerInterface $catcher The exception handler. |
79
|
|
|
* @param string $catchClass The exception type to be caught. |
80
|
|
|
* @return CatchMiddleware |
81
|
|
|
*/ |
82
|
1 |
|
public function catch(ExceptionHandlerInterface $catcher, string $catchClass = \Throwable::class): CatchMiddleware |
83
|
|
|
{ |
84
|
1 |
|
return new CatchMiddleware($this, $catcher, $catchClass); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|