|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace hiapi\Core\Http\Psr15\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use hiapi\Core\Endpoint\EndpointRepository; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
use Relay\Relay; |
|
13
|
|
|
|
|
14
|
|
|
class FallbackToLegacyApiMiddleware implements MiddlewareInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var MiddlewareInterface[] |
|
18
|
|
|
*/ |
|
19
|
|
|
private $newMiddlewares; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var MiddlewareInterface[] |
|
22
|
|
|
*/ |
|
23
|
|
|
private $legacyMiddlewares; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var EndpointRepository |
|
26
|
|
|
*/ |
|
27
|
|
|
private $endpointRepository; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* FallbackToLegacyApiMiddleware constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param EndpointRepository $endpointRepository |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(EndpointRepository $endpointRepository) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->endpointRepository = $endpointRepository; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @inheritDoc |
|
41
|
|
|
*/ |
|
42
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
43
|
|
|
{ |
|
44
|
|
|
$queue = $this->newMiddlewares; |
|
45
|
|
|
if (!$this->endpointRepository->has(trim($request->getUri()->getPath(), '/'))) { |
|
46
|
|
|
$queue = $this->legacyMiddlewares; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return (new Relay($queue))->handle($request); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param MiddlewareInterface[] $newMiddlewares |
|
54
|
|
|
* @return FallbackToLegacyApiMiddleware |
|
55
|
|
|
*/ |
|
56
|
|
|
public function newMiddlewares(array $newMiddlewares): FallbackToLegacyApiMiddleware |
|
57
|
|
|
{ |
|
58
|
|
|
$this->newMiddlewares = $newMiddlewares; |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param MiddlewareInterface[] $legacyMiddlewares |
|
64
|
|
|
* @return FallbackToLegacyApiMiddleware |
|
65
|
|
|
*/ |
|
66
|
|
|
public function legacyMiddlewares(array $legacyMiddlewares): FallbackToLegacyApiMiddleware |
|
67
|
|
|
{ |
|
68
|
|
|
$this->legacyMiddlewares = $legacyMiddlewares; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|