1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiClients\Foundation\Middleware; |
4
|
|
|
|
5
|
|
|
use ApiClients\Foundation\Middleware\Annotation\Priority as PriorityAnnotation; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use React\Promise\CancellablePromiseInterface; |
10
|
|
|
use ReflectionMethod; |
11
|
|
|
use Throwable; |
12
|
|
|
use function React\Promise\reject; |
13
|
|
|
use function React\Promise\resolve; |
14
|
|
|
|
15
|
|
|
final class MiddlewareRunner |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $options; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var MiddlewareInterface[] |
24
|
|
|
*/ |
25
|
|
|
private $middlewares; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AnnotationReader |
29
|
|
|
*/ |
30
|
|
|
private $annotationReader; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $id; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* MiddlewareRunner constructor. |
39
|
|
|
* @param array $options |
40
|
|
|
* @param MiddlewareInterface[] $middlewares |
41
|
|
|
*/ |
42
|
2 |
|
public function __construct(array $options, MiddlewareInterface ...$middlewares) |
43
|
|
|
{ |
44
|
2 |
|
$this->options = $options; |
45
|
2 |
|
$this->id = bin2hex(random_bytes(32)); |
46
|
2 |
|
$this->middlewares = $middlewares; |
|
|
|
|
47
|
2 |
|
$this->annotationReader = new AnnotationReader(); |
48
|
2 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param RequestInterface $request |
52
|
|
|
* @return CancellablePromiseInterface |
53
|
|
|
*/ |
54
|
2 |
View Code Duplication |
public function pre( |
|
|
|
|
55
|
|
|
RequestInterface $request |
56
|
|
|
): CancellablePromiseInterface { |
57
|
2 |
|
$promise = resolve($request); |
58
|
|
|
|
59
|
2 |
|
$middlewares = $this->middlewares; |
60
|
2 |
|
$middlewares = $this->orderMiddlewares('pre', ...$middlewares); |
61
|
|
|
|
62
|
2 |
|
foreach ($middlewares as $middleware) { |
63
|
2 |
|
$requestMiddleware = $middleware; |
64
|
|
|
$promise = $promise->then(function (RequestInterface $request) use ($requestMiddleware) { |
65
|
2 |
|
return $requestMiddleware->pre($request, $this->options, $this->id); |
66
|
2 |
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
return $promise; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ResponseInterface $response |
74
|
|
|
* @return CancellablePromiseInterface |
75
|
|
|
*/ |
76
|
2 |
View Code Duplication |
public function post( |
|
|
|
|
77
|
|
|
ResponseInterface $response |
78
|
|
|
): CancellablePromiseInterface { |
79
|
2 |
|
$promise = resolve($response); |
80
|
|
|
|
81
|
2 |
|
$middlewares = $this->middlewares; |
82
|
2 |
|
$middlewares = $this->orderMiddlewares('post', ...$middlewares); |
83
|
|
|
|
84
|
2 |
|
foreach ($middlewares as $middleware) { |
85
|
2 |
|
$responseMiddleware = $middleware; |
86
|
|
|
$promise = $promise->then(function (ResponseInterface $response) use ($responseMiddleware) { |
87
|
2 |
|
return $responseMiddleware->post($response, $this->options, $this->id); |
88
|
2 |
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
return $promise; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Throwable $throwable |
96
|
|
|
* @return CancellablePromiseInterface |
97
|
|
|
*/ |
98
|
2 |
View Code Duplication |
public function error( |
|
|
|
|
99
|
|
|
Throwable $throwable |
100
|
|
|
): CancellablePromiseInterface { |
101
|
2 |
|
$promise = reject($throwable); |
102
|
|
|
|
103
|
2 |
|
$middlewares = $this->middlewares; |
104
|
2 |
|
$middlewares = $this->orderMiddlewares('error', ...$middlewares); |
105
|
|
|
|
106
|
2 |
|
foreach ($middlewares as $middleware) { |
107
|
2 |
|
$errorMiddleware = $middleware; |
108
|
|
|
$promise = $promise->then(null, function (Throwable $throwable) use ($errorMiddleware) { |
109
|
2 |
|
return reject($errorMiddleware->error($throwable, $this->options, $this->id)); |
110
|
2 |
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
2 |
|
return $promise; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Sort the middlewares by priority. |
118
|
|
|
* |
119
|
|
|
* @param string $method |
120
|
|
|
* @param MiddlewareInterface[] $middlewares |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array |
124
|
|
|
{ |
125
|
2 |
|
usort($middlewares, function (MiddlewareInterface $left, MiddlewareInterface $right) use ($method) { |
126
|
2 |
|
return $this->getPriority($method, $right) <=> $this->getPriority($method, $left); |
127
|
2 |
|
}); |
128
|
|
|
|
129
|
2 |
|
return $middlewares; |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
private function getPriority(string $method, MiddlewareInterface $middleware): int |
133
|
|
|
{ |
134
|
2 |
|
$methodReflection = new ReflectionMethod($middleware, $method); |
135
|
|
|
/** @var PriorityAnnotation $annotation */ |
136
|
2 |
|
$annotation = $this->annotationReader->getMethodAnnotation($methodReflection, PriorityAnnotation::class); |
137
|
|
|
|
138
|
2 |
|
if ($annotation !== null && |
139
|
2 |
|
get_class($annotation) === PriorityAnnotation::class |
140
|
|
|
) { |
141
|
1 |
|
return $annotation->priority(); |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
return $middleware->priority(); |
|
|
|
|
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..