|
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
|
|
|
public function __construct(array $options, MiddlewareInterface ...$middlewares) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->options = $options; |
|
45
|
|
|
$this->middlewares = $this->orderMiddlewares(...$middlewares); |
|
46
|
|
|
$this->id = bin2hex(random_bytes(32)); |
|
47
|
|
|
$this->middlewares = $middlewares; |
|
|
|
|
|
|
48
|
|
|
$this->annotationReader = new AnnotationReader(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sort the middlewares by priority |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $method |
|
55
|
|
|
* @param MiddlewareInterface[] $middlewares |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function orderMiddlewares(string $method, MiddlewareInterface ...$middlewares): array |
|
59
|
|
|
{ |
|
60
|
|
|
usort($middlewares, function (MiddlewareInterface $left, MiddlewareInterface $right) use ($method) { |
|
61
|
|
|
return $this->getPriority($method, $right) <=> $this->getPriority($method, $left); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
return $middlewares; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
private function getPriority(string $method, MiddlewareInterface $middleware): int |
|
68
|
|
|
{ |
|
69
|
|
|
$methodReflection = new ReflectionMethod($middleware, $method); |
|
70
|
|
|
/** @var PriorityAnnotation $annotation */ |
|
71
|
|
|
$annotation = $this->annotationReader->getMethodAnnotation($methodReflection, PriorityAnnotation::class); |
|
72
|
|
|
|
|
73
|
|
|
if ($annotation !== null && |
|
74
|
|
|
get_class($annotation) === PriorityAnnotation::class |
|
75
|
|
|
) { |
|
76
|
|
|
return $annotation->priority(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $middleware->priority(); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param RequestInterface $request |
|
84
|
|
|
* @return CancellablePromiseInterface |
|
85
|
|
|
*/ |
|
86
|
|
View Code Duplication |
public function pre( |
|
|
|
|
|
|
87
|
|
|
RequestInterface $request |
|
88
|
|
|
): CancellablePromiseInterface { |
|
89
|
|
|
$promise = resolve($request); |
|
90
|
|
|
|
|
91
|
|
|
$middlewares = $this->middlewares; |
|
92
|
|
|
$middlewares = $this->orderMiddlewares('pre', ...$middlewares); |
|
93
|
|
|
|
|
94
|
|
|
foreach ($middlewares as $middleware) { |
|
95
|
|
|
$requestMiddleware = $middleware; |
|
96
|
|
|
$promise = $promise->then(function (RequestInterface $request) use ($requestMiddleware) { |
|
97
|
|
|
return $requestMiddleware->pre($request, $this->options, $this->id); |
|
98
|
|
|
}); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $promise; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param ResponseInterface $response |
|
106
|
|
|
* @return CancellablePromiseInterface |
|
107
|
|
|
*/ |
|
108
|
|
View Code Duplication |
public function post( |
|
|
|
|
|
|
109
|
|
|
ResponseInterface $response |
|
110
|
|
|
): CancellablePromiseInterface { |
|
111
|
|
|
$promise = resolve($response); |
|
112
|
|
|
|
|
113
|
|
|
$middlewares = $this->middlewares; |
|
114
|
|
|
$middlewares = $this->orderMiddlewares('post', ...$middlewares); |
|
115
|
|
|
|
|
116
|
|
|
foreach ($middlewares as $middleware) { |
|
117
|
|
|
$responseMiddleware = $middleware; |
|
118
|
|
|
$promise = $promise->then(function (ResponseInterface $response) use ($responseMiddleware) { |
|
119
|
|
|
return $responseMiddleware->post($response, $this->options, $this->id); |
|
120
|
|
|
}); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $promise; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param Throwable $throwable |
|
128
|
|
|
* @return CancellablePromiseInterface |
|
129
|
|
|
*/ |
|
130
|
|
View Code Duplication |
public function error( |
|
|
|
|
|
|
131
|
|
|
Throwable $throwable |
|
132
|
|
|
): CancellablePromiseInterface { |
|
133
|
|
|
$promise = reject($throwable); |
|
134
|
|
|
|
|
135
|
|
|
$middlewares = $this->middlewares; |
|
136
|
|
|
$middlewares = $this->orderMiddlewares('error', ...$middlewares); |
|
137
|
|
|
|
|
138
|
|
|
foreach ($middlewares as $middleware) { |
|
139
|
|
|
$errorMiddleware = $middleware; |
|
140
|
|
|
$promise = $promise->then(null, function (Throwable $throwable) use ($errorMiddleware) { |
|
141
|
|
|
return reject($errorMiddleware->error($throwable, $this->options, $this->id)); |
|
142
|
|
|
}); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $promise; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
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..