1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Lit\Nimo\Traits; |
6
|
|
|
|
7
|
|
|
use Lit\Nimo\Interfaces\RequestPredictionInterface; |
8
|
|
|
use Lit\Nimo\MiddlewarePipe; |
9
|
|
|
use Lit\Nimo\Middlewares\CatchMiddleware; |
10
|
|
|
use Lit\Nimo\Middlewares\PredictionWrapperMiddleware; |
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
12
|
|
|
|
13
|
|
|
trait MiddlewareCompositeTrait |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* append $middleware after this one, return the new $middlewareStack |
17
|
|
|
* |
18
|
|
|
* @param $middleware |
19
|
|
|
* @return MiddlewarePipe |
20
|
|
|
*/ |
21
|
|
|
public function append(MiddlewareInterface $middleware): MiddlewarePipe |
22
|
|
|
{ |
23
|
|
|
$stack = new MiddlewarePipe(); |
24
|
|
|
|
25
|
|
|
/** @noinspection PhpParamsInspection */ |
26
|
|
|
return $stack |
27
|
|
|
->append($this) |
|
|
|
|
28
|
|
|
->append($middleware); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* prepend $middleware before this one, return the new $middlewareStack |
33
|
|
|
* |
34
|
|
|
* @param $middleware |
35
|
|
|
* @return MiddlewarePipe |
36
|
|
|
*/ |
37
|
|
|
public function prepend(MiddlewareInterface $middleware): MiddlewarePipe |
38
|
|
|
{ |
39
|
|
|
$stack = new MiddlewarePipe(); |
40
|
|
|
|
41
|
|
|
/** @noinspection PhpParamsInspection */ |
42
|
|
|
return $stack |
43
|
|
|
->prepend($this) |
|
|
|
|
44
|
|
|
->prepend($middleware); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function when(RequestPredictionInterface $requestPrediction): MiddlewareInterface |
48
|
|
|
{ |
49
|
|
|
/** @noinspection PhpParamsInspection */ |
50
|
|
|
return new PredictionWrapperMiddleware($this, $requestPrediction); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function unless(RequestPredictionInterface $requestPrediction): MiddlewareInterface |
54
|
|
|
{ |
55
|
|
|
/** @noinspection PhpParamsInspection */ |
56
|
|
|
return new PredictionWrapperMiddleware($this, $requestPrediction, true); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function catch(callable $catcher, string $catchClass = \Throwable::class): MiddlewareInterface |
|
|
|
|
60
|
|
|
{ |
61
|
1 |
|
return new CatchMiddleware($this, $catcher, $catchClass); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|